Vinci2504
New Member
- Joined
- Jan 13, 2020
- Messages
- 7
- Office Version
- 365
- 2019
- 2016
- Platform
- Windows
- Mobile
- Web
Hi All,
I am trying to find method to speed up below macro.
Do you think is possible to paste .Interior.ColorIndex to sheet from Array instead of loop ?
Code like below.
I am trying to find method to speed up below macro.
Do you think is possible to paste .Interior.ColorIndex to sheet from Array instead of loop ?
Code like below.
VBA Code:
Sub test012502()
Dim colorArr As Variant
Dim DataSheet As Worksheet
Dim SheetName As String
SheetName = ActiveWorkbook.ActiveSheet.Name
Set DataSheet = ThisWorkbook.Worksheets(SheetName)
Dim maxRows As Double: maxRows = 3000
Dim maxCol As Double: maxCol = 24
Dim numCells As Double: numCells = maxRows * maxCol
Call TurnOffStuff
ReDim colorArr(1 To maxCol, 1 To maxRows)
DataSheet.Cells.Interior.ColorIndex = xlNone
DataSheet.Cells.Clear
Dim StartTime As Double: StartTime = Timer
With DataSheet
For col = LBound(colorArr, 1) To UBound(colorArr, 1)
For Row = LBound(colorArr, 2) To UBound(colorArr, 2)
.Cells(Row, col).Value = Round(Rnd * 10, 0)
colorArr(col, Row) = .Cells(Row, col)
Next Row
Next col
End With
With DataSheet
' is possible to paste .Interior.ColorIndex to sheet from Array instead of loop ?
.Range(.Cells(1, maxCol + 1), .Cells(UBound(colorArr, 2), 2 * maxCol)) = Application.Transpose(colorArr)
' col by col, row by row is so slowly
For col = maxCol + 1 To 2 * maxCol
For Row = LBound(colorArr, 2) To UBound(colorArr, 2)
.Cells(Row, col).Interior.ColorIndex = colorArr(col - maxCol, Row)
Next Row
Next col
End With
Call TurnOnStuff
'MsgBox "Done: " & Round(Timer - StartTime, 5) & "s", vbInformation, "Done"
Debug.Print "Done " & Round(Timer - StartTime, 3) & "second" & " - " & numCells & "cells"
End Sub
Public Function TurnOffStuff()
With Application
.Calculation = xlCalculationManual
.ScreenUpdating = False
.EnableEvents = False
.DisplayAlerts = False
End With
End Function
Public Function TurnOnStuff()
With Application
.Calculation = xlCalculationAutomatic
.ScreenUpdating = True
.EnableEvents = True
.DisplayAlerts = True
.CalculateBeforeSave = True
End With
End Function