2013-05-07 127 views
0

我是VBA的新手,所以如果我不清楚我的问题,请道歉。在VBA编译错误

我有代码,其中包含下面的一些循环,我得到一个编译错误“下一个没有For”在165行“下一个计数器”。

但是我有第121行的“For counter =”。所以在我的无知中,我感到莫名其妙。

对不起,如果这段代码太业余,但它是我能做的最好的。

Sub Macro9() 

Dim cardtype, drcr, sheetname, wbkname, merchant As String 
Dim counter, prd As Integer 
Dim row1, row2, row3, row4, row5 As Integer 
Dim col1, col2, col3, col4, col5 As Integer 
Dim val1, val2, val3, val4, NFile As Long 
Dim rng As Range 
Dim wks As Worksheet 
Dim WorkBk As Workbook 
Dim FolderPath, FileName As String 
Dim SelectedFiles() As Variant 



prd = InputBox("Input the Period number") 

FolderPath = "C:\My Files\Reports\Statements\" 

ChDrive FolderPath 
ChDir FolderPath 


SelectedFiles = Application.GetOpenFilename(_ 
    filefilter:="Excel Files (*.xl*), *.xl*", MultiSelect:=True) 


For NFile = LBound(SelectedFiles) To UBound(SelectedFiles) 

    FileName = SelectedFiles(NFile) 

    Set WorkBk = Workbooks.Open(FileName) 

    sheetname = ActiveSheet.name 
    wbkname = ActiveWorkbook.name 
    merchant = Left(wbkname, 7) 

    Cells.Select 
    Selection.Find(What:="Trans Value", After:=ActiveCell, LookIn:= _ 
    xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:= _ 
    xlNext, MatchCase:=False, SearchFormat:=False).Activate 

col1 = ActiveCell.Column 

Cells.Select 
Selection.Find(What:="Quantity", After:=ActiveCell, LookIn:= _ 
    xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:= _ 
    xlNext, MatchCase:=False, SearchFormat:=False).Activate 

col2 = ActiveCell.Column 

Cells.Select 
Selection.Find(What:="charge Desc", After:=ActiveCell, LookIn:= _ 
    xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:= _ 
    xlNext, MatchCase:=False, SearchFormat:=False).Activate 

col3 = ActiveCell.Column 

ActiveCell.Offset(1, 0).Range("A1:A200").Select 
Selection.SpecialCells(xlCellTypeConstants, 2).Select 

row1 = ActiveCell.row 

Cells.Select 
Selection.Find(What:="Cr/Dr Trans Flag", After:=ActiveCell, LookIn:= _ 
    xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:= _ 
    xlNext, MatchCase:=False, SearchFormat:=False).Activate 

col4 = ActiveCell.Column 


Cells.Select 
Selection.Find(What:="Balance from last month", After:=ActiveCell, LookIn:= _ 
    xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:= _ 
    xlNext, MatchCase:=False, SearchFormat:=False).Activate 

row2 = ActiveCell.row - 1 

Cells.Select 
Selection.Find(What:="charge ($)", After:=ActiveCell, LookIn:= _ 
    xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:= _ 
    xlNext, MatchCase:=False, SearchFormat:=False).Activate 

col4 = ActiveCell.Column 

ActiveCell.Offset(1, 0).Range("A1:A200").Select 
Selection.SpecialCells(xlCellTypeConstants, 1).Select 

row4 = ActiveCell.row 



Cells.Select 
Selection.Find(What:="Store1 Credit", After:=ActiveCell, LookIn:= _ 
    xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:= _ 
    xlNext, MatchCase:=False, SearchFormat:=False).Activate 
ActiveCell.Select 
ActiveCell.FormulaR1C1 = "Web1 Credit" 

Cells.Select 
Selection.Find(What:="Store2 Credit", After:=ActiveCell, LookIn:=xlFormulas _ 
    , LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ 
    MatchCase:=False, SearchFormat:=False).Activate 
ActiveCell.Select 
ActiveCell.FormulaR1C1 = "Web2 Credit" 

For Each wks In Worksheets 

Set rng = wks.Cells.Find("Store Refund") 
If Not rng Is Nothing Then 
rng = "Web Refund" 
Exit Sub 

End If 
Next wks 



For counter = row1 To row2 

Rows(counter).Columns(col3).Select 
cardtype = ActiveCell.Value 
row3 = ActiveCell.row 
drcr = Rows(counter).Columns(col4).Value 
val1 = Rows(counter).Columns(col2).Value 

If drcr = CR Then 
val2 = -Rows(counter).Columns(col1).Value 
Else 
val2 = Rows(counter).Columns(col1).Value 
End If 

Windows("Stores 2013.xlsm").Activate 

ActiveWorkbook.sheets(prd).Select 

Columns("B:B").Select 
Selection.Find(What:=cardtype, After:=ActiveCell, _ 
    LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, _ 
    SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).Activate 

If merchant = 1234567 Then 
ActiveCell.Offset(0, 4).Select 
val3 = ActiveCell.Value 
Else 
If merchant = 7654321 Then 
ActiveCell.Offset(0, 8).Select 
val3 = ActiveCell.Value 
Else 
If merchant = 1122334 Then 
ActiveCell.Offset(0, 12).Select 
val3 = ActiveCell.Value 
End If 

ActiveCell.FormulaR1C1 = val1 + val3 
ActiveCell.Offset(0, 1).Select 
val4 = ActiveCell.Value 
ActiveCell.FormulaR1C1 = val2 + val4 

Windows(wbkname).Activate 

Next counter 

Windows("Stores 2013.xlsm").Activate 

ActiveWorkbook.sheets(prd).Select 

Columns("B:B").Select 
Selection.Find(What:="Total $", After:=ActiveCell, _ 
    LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, _ 
    SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).Activate 

If merchant = 1234567 Then 
ActiveCell.Offset(0, 6).Select 
Else 
If merchant = 7654321 Then 
ActiveCell.Offset(0, 10).Select 
Else 
If merchant = 1122334 Then 
ActiveCell.Offset(0, 14).Select 
End If 

ActiveCell.FormulaR1C1 = "='[wbkname]" & sheetname & "'!R" & row4 & "C" & col4 

WorkBk.Close savechanges:=False 

Next NFile 

End Sub  

在此先感谢

+0

当您收到错误时进行调试。当前位置的价值可能与其余的不同,导致其失败。 – CustomX 2013-05-07 12:23:24

+0

@ t.thielemans感谢您的回复。不幸的是,我没有得到调试选项。我弹出编译错误“next for For”,荧光笔直接转到第165行,其中“Next counter”出现在上面的代码中 – 60John 2013-05-07 12:34:39

回答

4
Else 
If 

是不一样的

ElseIf 

试试这个:

Sub Macro9() 

Dim cardtype, drcr, sheetname, wbkname, merchant As String 
Dim counter, prd As Integer 
Dim row1, row2, row3, row4, row5 As Integer 
Dim col1, col2, col3, col4, col5 As Integer 
Dim val1, val2, val3, val4, NFile As Long 
Dim rng As Range 
Dim wks As Worksheet 
Dim WorkBk As Workbook 
Dim FolderPath, FileName As String 
Dim SelectedFiles() As Variant 



prd = InputBox("Input the Period number") 

FolderPath = "C:\My Files\Reports\Statements\" 

ChDrive FolderPath 
ChDir FolderPath 


SelectedFiles = Application.GetOpenFilename(_ 
    filefilter:="Excel Files (*.xl*), *.xl*", MultiSelect:=True) 


For NFile = LBound(SelectedFiles) To UBound(SelectedFiles) 

     FileName = SelectedFiles(NFile) 

     Set WorkBk = Workbooks.Open(FileName) 

     sheetname = ActiveSheet.Name 
     wbkname = ActiveWorkbook.Name 
     merchant = Left(wbkname, 7) 

     Cells.Find(What:="Trans Value", After:=ActiveCell, LookIn:= _ 
     xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:= _ 
     xlNext, MatchCase:=False, SearchFormat:=False).Activate 

    col1 = ActiveCell.Column 

    Cells.Find(What:="Quantity", After:=ActiveCell, LookIn:= _ 
     xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:= _ 
     xlNext, MatchCase:=False, SearchFormat:=False).Activate 

    col2 = ActiveCell.Column 

    Cells.Find(What:="charge Desc", After:=ActiveCell, LookIn:= _ 
     xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:= _ 
     xlNext, MatchCase:=False, SearchFormat:=False).Activate 

    col3 = ActiveCell.Column 

    ActiveCell.Offset(1, 0).Range("A1:A200").SpecialCells(xlCellTypeConstants, 2).Select 

    row1 = ActiveCell.Row 

    Cells.Find(What:="Cr/Dr Trans Flag", After:=ActiveCell, LookIn:= _ 
     xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:= _ 
     xlNext, MatchCase:=False, SearchFormat:=False).Activate 

    col4 = ActiveCell.Column 


    Cells.Find(What:="Balance from last month", After:=ActiveCell, LookIn:= _ 
     xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:= _ 
     xlNext, MatchCase:=False, SearchFormat:=False).Activate 

    row2 = ActiveCell.Row - 1 

    Cells.Find(What:="charge ($)", After:=ActiveCell, LookIn:= _ 
     xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:= _ 
     xlNext, MatchCase:=False, SearchFormat:=False).Activate 

    col4 = ActiveCell.Column 

    ActiveCell.Offset(1, 0).Range("A1:A200").SpecialCells(xlCellTypeConstants, 1).Select 

    row4 = ActiveCell.Row 



    Cells.Find(What:="Store1 Credit", After:=ActiveCell, LookIn:= _ 
     xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:= _ 
     xlNext, MatchCase:=False, SearchFormat:=False).Activate 
    ActiveCell.Select 
    ActiveCell.FormulaR1C1 = "Web1 Credit" 

    Cells.Find(What:="Store2 Credit", After:=ActiveCell, LookIn:=xlFormulas _ 
     , LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ 
     MatchCase:=False, SearchFormat:=False).Activate 
    ActiveCell.Select 
    ActiveCell.FormulaR1C1 = "Web2 Credit" 

    For Each wks In Worksheets 

     Set rng = wks.Cells.Find("Store Refund") 
     If Not rng Is Nothing Then 
     rng = "Web Refund" 
     Exit Sub 

     End If 
    Next wks 



    For counter = row1 To row2 

     Rows(counter).Columns(col3).Select 
     cardtype = ActiveCell.Value 
     row3 = ActiveCell.Row 
     drcr = Rows(counter).Columns(col4).Value 
     val1 = Rows(counter).Columns(col2).Value 

     If drcr = CR Then 
     val2 = -Rows(counter).Columns(col1).Value 
     Else 
     val2 = Rows(counter).Columns(col1).Value 
     End If 

     Windows("Stores 2013.xlsm").Activate 

     ActiveWorkbook.Sheets(prd).Select 

     Columns("B:B").Find(What:=cardtype, After:=ActiveCell, _ 
      LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, _ 
      SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).Activate 

     If merchant = 1234567 Then 
      ActiveCell.Offset(0, 4).Select 
      val3 = ActiveCell.Value 
     ElseIf merchant = 7654321 Then 
      ActiveCell.Offset(0, 8).Select 
      val3 = ActiveCell.Value 
     ElseIf merchant = 1122334 Then 
      ActiveCell.Offset(0, 12).Select 
      val3 = ActiveCell.Value 
     End If 

     ActiveCell.FormulaR1C1 = val1 + val3 
     ActiveCell.Offset(0, 1).Select 
     val4 = ActiveCell.Value 
     ActiveCell.FormulaR1C1 = val2 + val4 

     Windows(wbkname).Activate 

    Next counter 

    Windows("Stores 2013.xlsm").Activate 

    ActiveWorkbook.Sheets(prd).Select 

    Columns("B:B").Find(What:="Total $", After:=ActiveCell, _ 
     LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, _ 
     SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).Activate 

    If merchant = 1234567 Then 
    ActiveCell.Offset(0, 6).Select 
    ElseIf merchant = 7654321 Then 
    ActiveCell.Offset(0, 10).Select 
    ElseIf merchant = 1122334 Then 
    ActiveCell.Offset(0, 14).Select 
    End If 

    ActiveCell.FormulaR1C1 = "='[wbkname]" & sheetname & "'!R" & row4 & "C" & col4 

    WorkBk.Close savechanges:=False 

Next NFile 

End Sub 

而且,在被清理了很多你的代码。请仔细阅读:How to avoid using Select in Excel VBA macros

+0

辉煌!这似乎已经整理出来了。我知道我需要停止使用select。感谢您的链接。 – 60John 2013-05-07 12:39:39

+0

太棒了,很高兴它帮助!作为纯偏好的问题,我更喜欢Select Case over ElseIf Statements(http://msdn.microsoft.com/zh-cn/library/office/gg278454.aspx)。我发现他们更容易阅读。 – sous2817 2013-05-07 12:41:49