编译错误:结束如果没有阻止If(有更多问题)

时间:2015-05-05 00:57:13

标签: vba compilation

遇到以下代码问题。它说我运行它时编译错误,但我认为我遇到了更大的问题。我想要完成的任务:

  1. 打开最近的"手头报告"
  2. 回到" Master KB-PFEP"工作表
  3. 如果工作表上有过滤器,请清除过滤器
  4. 执行VLOOKUP以更新现有报告
  5. 上的现有列

    非常感谢您的帮助

        Dim curDate As String, Fname As String
        curDate = Format(Date, "yyyy-mm-dd")
        Dim wba As Workbook
        Fname = "Y:\Consumables\Company\ABC\ABC - Planning & Materials\On Hand Reports\ABC Site\" & curDate & "_INV_R12_ABC_Onhandreport.xlsx"
        Set wba = Workbooks.Open(Filename:=Fname, UpdateLinks:=False, Notify:=False)
        Dim wb As Workbook
        For Each wb In Application.Workbooks
        If wb.Name Like "*Master KB-PFEP" Then wb.Activate
        wb.Worksheets("Master").AutoFilter.Sort.SortFields.Clear
        If (Worksheets("Master Data").AutoFilterMode And Worksheets("Master Data").FilterMode) Or Worksheets("Master Data").FilterMode Then
        Worksheets("Master Data").ShowAllData
        End If
        Range("AL8:A" & Cells(Rows.Count, "A").End(xlUp).Row).Formula = "=SUMIF('Inv Report'!C[-36],RC[-36],'Inv Report'!C[-21])"
    End If
    End Sub
    

2 个答案:

答案 0 :(得分:1)

当条件代码位于同一行时,您不需要End If

If wb.Name Like "*Master KB-PFEP" Then wb.Activate

这意味着你底部有一个额外的。解决之后,您需要在Next之前的某处添加缺少的End Sub

答案 1 :(得分:0)

我想你应该拥有的是什么(没有检查你的R1C1值):

  1. wb.Activate
  2. 上的新行
  3. 使用FormulaR1C1代替Formula
  4. Next
  5. 之前添加了End Sub
    Sub SO_30042563()
        Dim curDate As String, Fname As String
        curDate = Format(Date, "yyyy-mm-dd")
        Dim wba As Workbook, wb As Workbook
        Fname = "Y:\Consumables\Company\ABC\ABC - Planning & Materials\On Hand Reports\ABC Site\" & curDate & "_INV_R12_ABC_Onhandreport.xlsx"
        Set wba = Workbooks.Open(Filename:=Fname, UpdateLinks:=False, Notify:=False)
        For Each wb In Application.Workbooks
            If wb.Name Like "*Master KB-PFEP" Then
                wb.Activate
                wb.Worksheets("Master").AutoFilter.Sort.SortFields.Clear
                If (Worksheets("Master Data").AutoFilterMode And Worksheets("Master Data").FilterMode) Or Worksheets("Master Data").FilterMode Then
                    Worksheets("Master Data").ShowAllData
                End If
                Range("AL8:A" & Cells(Rows.Count, "A").End(xlUp).Row).FormulaR1C1 = "=SUMIF('Inv Report'!C[-36],RC[-36],'Inv Report'!C[-21])"
            End If
        Next
    End Sub