2015-10-02 89 views
1

所以我知道我昨天問了一個非常類似的問題,它實際上大約是相同的代碼。前面的問題可以發現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 
+2

你剛剛刪除了'行(i)'那裏的範圍是。 – Rory

+0

你可以在循環的開始嘗試刪除嗎?或者減少i並繼續迭代 –

+1

如果聲明'sht'工作表變量並將其設置爲'Sheets(「Comparative Performance1」)'',那麼您的代碼將更短且更易於調試。你也在這裏管理很多不必要的變量:如果你聲明一個變量並且只使用它*一次*那麼你可能不需要它。 'sht.Range(「U」&i).Formula =「= D」+ CStr(i)+「-M」+ CStr(i)'更容易管理 –

回答

1

我覺得你的問題可能是上寫着Rows(i).entirerow.delete行你If語句中。您基本上刪除Row(i)以及分配給YTD的範圍等於Range("T"& i)。您可能需要

  1. 到您的變量分配值之前刪除的行

  2. 添加下面的刪除一行之後:

    i=i-1「這將重新做您刪除

    Next i'這會讓你回到最上面。

  3. 做一些錯誤處理,在遇到該錯誤後返回頂部。

0

如果您在FOR語句中刪除行時出現問題,而FOR語句在每次傳遞時遞增,並最終出現行差異。

下面的代碼可能會使用你的...

Sub Offset(Optional rows1 As Long) 
    Dim sh As Worksheet: Set sh = Sheets("Comparative Performance1") 

    Dim HeaderRow As Long: HeaderRow = 1 
    Dim LastRow As Long: LastRow = sh.Cells.Find("*", _ 
     SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row 
    If rows1 > HeaderRow Then LastRow = rows1 

    Dim i As Long, k As Long, Counter As Long: Counter = 0 

    With sh 
     'Puts in the metric names on the top row. Can be adjusted for 2nd row if need be 
     Dim Headers() As String: Headers = _ 
      Split("YTD,yr1,yr3,yr5,yr7,yr10,SI,QTR,YTD_2,yr1,yr3,yr5,yr7,yr10,SI", ",") 
     For i = 0 To UBound(Headers) 
      .Cells(HeaderRow, 20 + i) = Headers(i) 'Starts at Col T 
     Next i 

     For i = LastRow To HeaderRow + 2 Step -1 
      If .Cells(i, 10).Value = "" Then 
       .Range(.Cells(i, 2), .Cells(i, 10)).Copy 
       .Range(.Cells(i, 2), .Cells(i, 10)).Offset(-1, 9).PasteSpecial xlPasteValues 
       .Rows(i).EntireRow.Delete 
       Counter = Counter + 1 
      End If 
     Next i 

     For i = HeaderRow + 2 To LastRow - Counter 
      For k = 1 To 7 'Metrics on YTD to SI 
       .Cells(i, k + 19).FormulaR1C1 = "=RC[-17]-RC[-8]" 
       .Cells(i, k + 27).FormulaR1C1 = "=RC[-8]/RC[-25]" 
      Next k 
      .Cells(i, 27).FormulaR1C1 = "=RC[-8]/RC[-25]" 'Metric on QTR 
     Next i 

    End With 
End Sub 

哦 - 我也假設你有以下行錯誤:

YTD_2s = "=S" + CStr(i) + "/B" + CStr(i) 

如果我猜它應該是:

YTD_2s = "=T" + CStr(i) + "/C" + CStr(i) 
+0

YTD_2s的好處。作爲VBA相當新手的人,你的代碼比我的代碼複雜得多。我知道,在所有可變設置中,我都是相當多餘的,但我寧願它緩慢而且工作,而不是快速並犯錯誤。 你的代碼能做到我正在做的事嗎? –

+0

你爲什麼不嘗試看看?從本質上講,它應該完成問題中的代碼 - 但是代碼是否適合您的使用或需要更改。沒有看到電子表格,我不得不猜測使用情況。代碼本身被分成3塊;第一個將你的標題分割成數組,然後將每個數組的值粘貼到所需的單元格中。第二部分解析丟失的數據並刪除不符合的行(如果有錯誤,則可能在此處)。第三個只是在使用相對公式的公式中加入。它爲我工作,但你需要測試它... – Tragamor