I have a macro that adds hundreds of rows of data to an Excel spreadsheet. I call a procedure from a loop that inserts each row of data. I apply formatting to a string every time I insert this data. However, during my testing, I found that I can insert all the data about 3/4 second faster (3.3 s versus 4.11 seconds) when I do not apply line-by-line formatting, but all at once. The problem I'm trying to overcome is that not every line has the same formatting; however, there is a predictable picture. Two lines of one formatting and one line of different formatting. Is there a way without a loop to apply these two different formats on one that would allow me to maintain the performance gain that I get (users would like to get a second second answer,so this can be a big win).
I am currently using the following code (application settings such as pop-ups, calculations, and events are disabled 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
source
share