所以我知道我昨天問了一個非常類似的問題,它實際上大約是相同的代碼。前面的問題可以發現here.在VBA循環中重置範圍變量導致424錯誤
它的99%完成,但在循環中導致它失敗的運行時錯誤。我不明白的是,它運行一次,做所需的一切,然後重置範圍變量YTD使其在YTD.Formula = YTDs停止。下面的代碼。
Sub offset(rows1 As Long)
Dim sh As Worksheet
'Integers
Dim i As Long
Dim k As Long
'Movers
Dim current As Range
Dim first As Range
'Metrics
Dim QTRA As Range
Dim YTD As Range
Dim yr1 As Range
Dim yr3 As Range
Dim yr7 As Range
Dim yr5 As Range
Dim yr10 As Range
Dim SI As Range
Dim QTR As Range
Dim YTD_2 As Range
Dim yr1_2 As Range
Dim yr3_2 As Range
Dim yr5_2 As Range
Dim yr7_2 As Range
Dim yr10_2 As Range
Dim SI_2 As Range
'Strings
Dim QTRAs As String
Dim YTDs As String
Dim yr1s As String
Dim yr3s As String
Dim yr7s As String
Dim yr5s As String
Dim yr10s As String
Dim SIs As String
Dim QTRs As String
Dim YTD_2s As String
Dim yr1_2s As String
Dim yr3_2s As String
Dim yr5_2s As String
Dim yr7_2s As String
Dim yr10_2s As String
Dim SI_2s As String
'Puts in the metric names on the top row. Can be adjusted for 2nd row if need be
Sheets("Comparative Performance1").Range("T1").Formula = "YTD"
Sheets("Comparative Performance1").Range("U1").Formula = "yr1"
Sheets("Comparative Performance1").Range("V1").Formula = "yr3"
Sheets("Comparative Performance1").Range("W1").Formula = "yr5"
Sheets("Comparative Performance1").Range("Y1").Formula = "yr7"
Sheets("Comparative Performance1").Range("X1").Formula = "yr10"
Sheets("Comparative Performance1").Range("Z1").Formula = "SI"
Sheets("Comparative Performance1").Range("AA1").Formula = "QTR"
Sheets("Comparative Performance1").Range("AB1").Formula = "YTD_2"
Sheets("Comparative Performance1").Range("AC1").Formula = "yr1"
Sheets("Comparative Performance1").Range("AD1").Formula = "yr3"
Sheets("Comparative Performance1").Range("AE1").Formula = "yr5"
Sheets("Comparative Performance1").Range("AF1").Formula = "yr7"
Sheets("Comparative Performance1").Range("AG1").Formula = "yr10"
Sheets("Comparative Performance1").Range("AH1").Formula = "SI"
'Finds the length of the data
'Dim rn As Range
'Set sh = ThisWorkbook.Sheets("Comparative Performance1")
'Set rn = sh.UsedRange
'k = rn.Rows.Count + rn.Row - 1
k = rows1
For i = 3 To k
'Setting vari ables for each respective data column
Set current = Sheets("Comparative Performance1").Range("J" & i)
Set first = Sheets("Comparative Performance1").Range("B" & i)
Set QTRA = Sheets("Comparative Performance1").Range("S" & i)
Set YTD = Sheets("Comparative Performance1").Range("T" & i)
Set yr1 = Sheets("Comparative Performance1").Range("U" & i)
Set yr3 = Sheets("Comparative Performance1").Range("V" & i)
Set yr5 = Sheets("Comparative Performance1").Range("W" & i)
Set yr7 = Sheets("Comparative Performance1").Range("Y" & i)
Set yr10 = Sheets("Comparative Performance1").Range("X" & i)
Set SI = Sheets("Comparative Performance1").Range("Z" & i)
Set QTR = Sheets("Comparative Performance1").Range("AA" & i)
Set YTD_2 = Sheets("Comparative Performance1").Range("AB" & i)
Set yr1_2 = Sheets("Comparative Performance1").Range("AC" & i)
Set yr3_2 = Sheets("Comparative Performance1").Range("AD" & i)
Set yr5_2 = Sheets("Comparative Performance1").Range("AE" & i)
Set yr7_2 = Sheets("Comparative Performance1").Range("AF" & i)
Set yr10_2 = Sheets("Comparative Performance1").Range("AG" & i)
Set SI_2 = Sheets("Comparative Performance1").Range("AH" & i)
'Moves the benchmarks if it is missing a creation date
If current = "" Then
Range(first, current).Select
Selection.Copy
Range(first, current).offset(-1, 9).Select
ActiveSheet.Paste
'I have it deleting the entire row, which may remove necessary data, not sure yet
rows(i).EntireRow.Delete
End If
'First we have to create strings for all of the formulas using the variable i
YTDs = "=C" + CStr(i) + "-L" + CStr(i)
yr1s = "=D" + CStr(i) + "-M" + CStr(i)
yr3s = "=E" + CStr(i) + "-N" + CStr(i)
yr5s = "=F" + CStr(i) + "-O" + CStr(i)
yr7s = "=G" + CStr(i) + "-P" + CStr(i)
yr10s = "=H" + CStr(i) + "-Q" + CStr(i)
SIs = "=I" + CStr(i) + "-R" + CStr(i)
QTRs = "=S" + CStr(i) + "/B" + CStr(i)
YTD_2s = "=S" + CStr(i) + "/B" + CStr(i)
yr1_2s = "=U" + CStr(i) + "/D" + CStr(i)
yr3_2s = "=V" + CStr(i) + "/E" + CStr(i)
yr5_2s = "=W" + CStr(i) + "/F" + CStr(i)
yr7_2s = "=X" + CStr(i) + "/G" + CStr(i)
yr10_2s = "=Y" + CStr(i) + "/H" + CStr(i)
SI_2s = "=Z" + CStr(i) + "/I" + CStr(i)
'This should assign all of the metrics using the correct variables
YTD.Formula = YTDs ********** THIS IS WHERE IT FAILS ************
yr1.Formula = yr1s
yr3.Formula = yr3s
yr5.Formula = yr5s
yr7.Formula = yr7s
yr10.Formula = yr10s
SI.Formula = SIs
QTR.Formula = QTRs
YTD_2.Formula = YTD_2s
yr1_2.Formula = yr1_2s
yr3_2.Formula = yr3_2s
yr5_2.Formula = yr5_2s
yr7_2.Formula = yr7_2s
yr10_2.Formula = yr10_2s
SI_2.Formula = SI_2s
Next i
End Sub
你剛剛刪除了'行(i)'那裏的範圍是。 – Rory
你可以在循環的開始嘗試刪除嗎?或者減少i並繼續迭代 –
如果聲明'sht'工作表變量並將其設置爲'Sheets(「Comparative Performance1」)'',那麼您的代碼將更短且更易於調試。你也在這裏管理很多不必要的變量:如果你聲明一個變量並且只使用它*一次*那麼你可能不需要它。 'sht.Range(「U」&i).Formula =「= D」+ CStr(i)+「-M」+ CStr(i)'更容易管理 –