I tried to write the formula into columns and have the following naive method that does the work:
sh.Range("R3:R" & lRow).Formula = "Q2"
sh.Range("S3:S" & lRow).Formula = "R2"
sh.Range("T3:T" & lRow).Formula = "S2"
sh.Range("U3:U" & lRow).Formula = "T2"
sh.Range("V3:V" & lRow).Formula = "U2"
sh.Range("W3:W" & lRow).Formula = "V2"
sh.Range("X3:X" & lRow).Formula = "W2"
sh.Range("Y3:Y" & lRow).Formula = "X2"
sh.Range("Z3:Z" & lRow).Formula = "Y2"
sh.Range("AA3:AA" & lRow).Formula = "Z2"
sh.Range("AB3:AB" & lRow).Formula = "AA2"
sh.Range("AC3:AC" & lRow).Formula = "AB2"
I felt that I could use the array to make these shorter or more efficient, so I searched on the site and tried the following code based on my understanding:
Dim wb As Workbook: Set wb = Workbooks("A.xlsx")
Dim sh As Worksheet
Dim lRow As Long
Dim i As Integer
Dim ColArray As Variant
Dim BaseArray As Variant
For Each sh In wb.Worksheets
lRow = sh.Cells(sh.Rows.Count,1).End(xlUp).Row
ColArray = Array("R","S","T","U","V","W","X","Y","Z","AA","AB","AC")
BaseArray = Array("Q","R","S","T","U","V","W","X","Y","Z","AA","AB")
For i = 1 To 12
sh.Range("ColArray(i)3:ColArray(i)"&lRow).Formula = "=BaseArray(i)2"
Next i
Next sh
I got titlementioned error message after running the code, can someone point out how to fix the code? Many thanks in advance.
ColArray(i) & "3:" & ColArray(i) & lrow"=" & BaseArray(i) & 2You also need to change your iterator toFor i = 0 To 11arrays are 0 indexed, unless they come from a range.