title: How can I optimize this vba loop code?
body: <p>Hi I wrote this code. but this code is very slow. How can I optimize this code?</p>
  
  <pre><code>Private Sub printItem(r, lastCol, objStream)
      FirstCol = 1
  
      Dim strFirst As String
  
      strFirst = CStr(ActiveSheet.Cells(r, 1).Value)
  
      If strFirst = "" Then
          Exit Sub
      End If
  
          objStream.WriteText "    &lt;"
          objStream.WriteText "TagName"
          objStream.WriteText " "
  
          For c = FirstCol To lastCol
  
              data = CStr(ActiveSheet.Cells(r, c).Value)
  
              If LenB(Trim$(data)) &gt; 0 Then
                  objStream.WriteText g_AttributeName(c)
                  objStream.WriteText "="""
                  objStream.WriteText data
                  objStream.WriteText """ "
              End If
  
          Next c
  
          objStream.WriteText "/&gt;"
  
      objStream.WriteText vbNewLine
  End Sub
  </code></pre>