I have a macro that add hundreds of lines of data to an excel spreadsheet. I call a procedure from a loop which inserts each line of data. I have been applying the formatting for the row each time that I insert that data. However, during my testing I have found that I can insert all of the data about 3/4 of second faster (3.3 sec vs. 4.11 sec) when I don’t apply the formatting line by line but all at once. The issue that I am trying to overcome is that not every row has the same formatting; however, there is a predictable pattern. Two rows of one formatting and one row of different formatting. Is there a way without looping to apply these two different formats all at one that would allow me to keep the performance gains that I am getting (users would like to see a sub 2 second response so this could be a big gain).
I am currently using the following code (application settings such as screenupdating, calculations, and events are all turned off during this)
Private Sub BuildTerminalSummary(ByRef terminals, ByVal timeFrame)
Dim terminal As clsTerminal
Dim curSheet As Worksheet
Dim breakLoop As Boolean
Dim terminalCode As String
Dim rowNumber As Long
Set terminal = New clsTerminal
Set curSheet = Sheets("Terminal Summary")
rowNumber = 7
'Remove all content, borders, and tint
ClearPage curSheet, rowNumber
For Each terminal In terminals
AddDetailData curSheet, terminal.InfoArray, rowNumber
AddDetailData curSheet, terminal.PriorInfoArray, rowNumber + 1
AddDiffPercentFormulas curSheet, terminal.DiffPercentInfoArray, rowNumber + 2
rowNumber = rowNumber + 2
Next terminal
'Make sure the columns are wide enough to display the numbers
curSheet.Cells.EntireColumn.AutoFit
End Sub
Private Sub AddDetailData(ByRef curSheet, ByRef data, ByVal rowNumber)
With curSheet
With .Cells(rowNumber, 3).Resize(1, 16)
.value = data
.Style = "Comma"
.NumberFormat = "_(* #,##0_);_(* (#,##0);_(* ""-""??_);_(@_)"
End With
'This overides the formatting in the revenue columns with currency instead of comma style
With .Cells(rowNumber, 5).Resize(1, 2)
.Style = "Currency"
.NumberFormat = "_($* #,##0_);_($* (#,##0);_($* ""-""??_);_(@_)"
End With
With .Cells(rowNumber, 13).Resize(1, 6)
.Style = "Currency"
End With
End With
End Sub
Private Sub AddDiffPercentFormulas(ByRef curSheet, ByRef data, ByVal rowNumber)
With curSheet.Cells(rowNumber, 3).Resize(1, 16)
.value = data
.NumberFormat = "0.00%"
End With
End Sub