2015-12-29 99 views
2

基本上,我試圖從Excel文件到該工作表(AUTO_UPDATE子)提取數據和代碼描述如下:「無效下一個控制變量參考」錯誤在VBA Excel 2003中

Sub Auto_Update() 
Dim filename As String 
Dim r As Integer 
Dim i As Double 
Dim t As Integer 
Dim DPR As Object 
Dim new_DPR As Object 
Dim well As Object 
Dim x As Integer 

If IsEmpty(ThisWorkbook.Sheets("SD-28P").Cells(1, 35)) = True Then 
ThisWorkbook.Sheets("SD-28P").Cells(1, 35) = Date - 2 
End If 
Excel.Application.Visible = False 
For i = Date - ThisWorkbook.Sheets("SD-28P").Cells(1, 35) To 1 Step -1 
    filename = "Z:\DPR\DPR_" + Format(Date - i, "yyyymmdd") + ".xls" 
    Set DPR = Excel.Application.Workbooks.Open(filename) 
    Set new_DPR = DPR.Worksheets("Daily Production Report") 
    For x = 247 To 272 Step 1 
     If Trim(new_DPR.Cells(x, 2).Value) = "SD-01PST" Then t = x 
     Exit For 
    For r = t To t + 35 Step 1 
     Set well = ThisWorkbook.Worksheets(Trim(new_DPR.Cells(r, 2).Value)) 
     f = First_Empty(well, 4) 
     If new_DPR.Cells(r, 6).Value = Date - i Then 
       new_DPR.Cells(r, 6).Copy 
       well.Cells(f, 1).PasteSpecial (xlPasteValues) 
       new_DPR.Cells(r, 8).Copy 
       well.Cells(f, 3).PasteSpecial (xlPasteValues) 
       new_DPR.Cells(r, 10).Copy 
       well.Cells(f, 4).PasteSpecial (xlPasteValues) 
       new_DPR.Range(new_DPR.Cells(r, 12), new_DPR.Cells(r, 17)).Copy 
       well.Range(well.Cells(f, 5), well.Cells(f, 10)).PasteSpecial (xlPasteValues) 
       new_DPR.Range(new_DPR.Cells(r, 20), new_DPR.Cells(r, 26)).Copy 
       well.Range(well.Cells(f, 11), well.Cells(f, 17)).PasteSpecial (xlPasteValues) 
       new_DPR.Range(new_DPR.Cells(r, 28), new_DPR.Cells(r, 30)).Copy 
       well.Range(well.Cells(f, 18), well.Cells(f, 20)).PasteSpecial (xlPasteValues) 
       well.Range(well.Cells(f - 1, 2), well.Cells(f - 1, 22)).Copy 
       well.Range(well.Cells(f, 2), well.Cells(f, 22)).PasteSpecial (xlPasteFormats) 
       well.Cells(f - 1, 1).Copy 
       well.Cells(f, 1).PasteSpecial (xlPasteFormulasAndNumberFormats) 
     End If 
    Next r 
    Excel.Application.CutCopyMode = False 
    DPR.Saved = True 
    DPR.Close 
    ThisWorkbook.Application.CutCopyMode = False 
Next i 
ThisWorkbook.Sheets("SD-28P").Cells(1, 35) = Date 
ThisWorkbook.Save 
Excel.Application.Visible = True 
ThisWorkbook.Sheets(4).Activate 

但然後,該代碼將在該行返回一個錯誤:Next i(下一個控件變量引用無效)。我仔細檢查了變量和For ... Next循環的syntacx,但是,我找不到任何可能的錯誤。請幫助!非常感謝你提前。

回答

0

你從來沒有關閉與

For x = 247 To 272 Step 1 
     If Trim(new_DPR.Cells(x, 2).Value) = "SD-01PST" Then t = x 
     Exit For 

開始循環你需要一個next x的地方,你可以使用一個next i之前。