robertgama
Board Regular
- Joined
- May 4, 2011
- Messages
- 189
I have a sheet that will have between 50,000 to 80,000 rows.
I started writing a macro but it's very slow and I've only processed a few columns of data so far.
I'm thinking that perhaps instead of writing the values one at a time I should read them in one column at at time, or put formulas in the top cell, select the whole range down then copy the formula for each column? Maybe copy/paste values only after I process each column?
Any suggestions would be great. Here is a bit of the code. (aaRegexReplace & aaRemoveInBrackets are just a couple of UDF's that are quick enough). It's mainly the reading and writing that I imagine is taking up all the time.
Thanks,
Rob.
I started writing a macro but it's very slow and I've only processed a few columns of data so far.
I'm thinking that perhaps instead of writing the values one at a time I should read them in one column at at time, or put formulas in the top cell, select the whole range down then copy the formula for each column? Maybe copy/paste values only after I process each column?
Any suggestions would be great. Here is a bit of the code. (aaRegexReplace & aaRemoveInBrackets are just a couple of UDF's that are quick enough). It's mainly the reading and writing that I imagine is taking up all the time.
Code:
Sub btnRegen_Click()
' Save the current state of Excel settings.
screenUpdateState = Application.ScreenUpdating
statusBarState = Application.DisplayStatusBar
calcState = Application.Calculation
eventsState = Application.EnableEvents
displayPageBreakState = ActiveSheet.DisplayPageBreaks
Application.ScreenUpdating = False
Application.DisplayStatusBar = False
Application.Calculation = xlCalculationManual
Application.EnableEvents = False
ActiveSheet.DisplayPageBreaks = False
Dim numRecords As Long
Dim i As Long
Dim curRow As Long
Dim curCol As Integer
Dim startRow As Integer
Dim lastRow As Long
Dim outputRow As Long
Dim numColumns As Integer
Dim ds As Worksheet
Set ds = Worksheets(Range("dataSheetName").Value)
numColumns = Range("numberOfColumns").Value
numRecords = Range("numberOfRecords").Value
startRow = Range("startRow").Value
lastRow = Range("lastRow").Value
For i = 1 To numRecords
curRow = i + 1
outputRow = startRow + i - 1
Cells(outputRow, 1).Value = ds.Cells(curRow, 1).Value
Cells(outputRow, 2).Value = "ADS_Client_" + CStr(Cells(outputRow, 1).Value)
If Len(ds.Cells(curRow, 2).Value) > 70 Then
Cells(outputRow, 3).Value = Left(aaRegexReplace(aaRemoveInBrackets(ds.Cells(curRow, 2).Value), "(.*)( formerly.*)", "\1"), 70)
Else
Cells(outputRow, 3).Value = ds.Cells(curRow, 2).Value
End If
Cells(outputRow, 4).Value = Cells(outputRow, 3).Value + " [FROM ADS]"
Cells(outputRow, 5).Value = aaProvinceStateFix(ds.Cells(curRow, 8).Value)
Cells(outputRow, 7).Value = aaCountryFix(ds.Cells(curRow, 7).Value)
Next i
' Restore Excel settings to original state.
Application.ScreenUpdating = screenUpdateState
Application.DisplayStatusBar = statusBarState
Application.Calculation = calcState
Application.EnableEvents = eventsState
ActiveSheet.DisplayPageBreaks = displayPageBreaksState
End Sub
Thanks,
Rob.