2013-05-07 3 views
0

Я новичок в VBA, поэтому извиняюсь, если я не понимаю в своем вопросе.Ошибка компиляции в VBA

У меня есть код, который включен ниже с несколькими циклами, и я получаю ошибку компиляции «next without For» в строке 165 «Следующий счетчик».

Однако у меня есть «Для счетчика =» на линии 121. Так что в моем невежестве я озадачен.

Извините, если этот код слишком любительский, но это лучшее, что я могу сделать.

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

+0

@ t.thielemans Спасибо, что ответили. К сожалению, у меня нет опции отладки. Я получаю всплывающее сообщение Compile Error «next without For», и маркер переходит прямо к строке 165, где «Следующий счетчик» появляется в коде выше – 60John

ответ

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

Brilliant! Кажется, это разобралось. Я знаю, что мне нужно прекратить использовать select. Спасибо за ссылку. – 60John

+0

Отлично, рад, что это помогло! И в качестве чистого предпочтения я предпочитаю Select Case над ElseIf Statements (http://msdn.microsoft.com/en-us/library/office/gg278454.aspx). Мне легче их читать. – sous2817

Смежные вопросы