结束如果没有块If - double For Each循环

时间:2018-04-19 09:09:24

标签: vba for-loop each

我正在尝试将一些代码放在一起进行一些简单的计算,但我很难通过If语句,它们似乎对我没有意义,但我的格式与我读过的解决方案相同类似的问题。

我在最后结束时收到“结束如果没有阻止如果”如果以下内容:

Sub SAP1C()

Dim i1, j1 As Range
Dim Material, Customer, Price As String
Dim MaterialStart, CustomerStart, PriceStart As Object
Dim Counter As Integer

Set i1 = Sheets("Invoice Prices").Range("B1:RL1")
Set j1 = Sheets("Invoice Prices").Range("A6:A500")

Set PriceStart = Sheets("SAP 1C").Range("A3")
Set MaterialStart = Sheets("SAP 1C").Range("J3")
Set CustomerStart = Sheets("SAP 1C").Range("I3")

Counter = 0

For Each i In i1

    If i = "" Then Exit Sub Else

        For Each j In j1

            If j <> "" Then

                Price = Cells(Application.WorksheetFunction.Row(j), Application.WorksheetFunction.Column(i)).Value
                Material = Application.WorksheetFunction.VLookup(j, Sheets("BTS").Range("F:G"), 2, 0)
                Customer = Application.WorksheetFunction.Text(Application.WorksheetFunction.VLookup(i, Sheets("Customer Hub").Range("A:G"), 7, 0), "0000000000")

                PriceStart.Offset(Counter, 0) = Price
                MaterialStart.Offset(Counter, 0) = Material
                CustomerStart.Offset(Counter, 0) = Customer

                Counter = Counter + 1

            End If

        Next j

    End If

Next i

End Sub  

我玩了一些代码并且无法弄明白 - 任何想法?
谢谢!

编辑:回答如下 - 非常感谢!

我已经稍微改变了代码并摆弄它以尝试让它超越原始问题,但我遇到了错误。你能看看下面的内容吗?:

Sub SAP1C()

Dim i1, j1 As Range
Dim Material, Customer, Price As String
Dim MaterialStart, CustomerStart, PriceStart As Object
Dim Counter As Integer

Set i1 = Sheets("Invoice Prices").Range("B1:RL1")
Set j1 = Sheets("Invoice Prices").Range("A6:A300")

Set PriceStart = Sheets("SAP 1C").Range("A3")
Set MaterialStart = Sheets("SAP 1C").Range("J3")
Set CustomerStart = Sheets("SAP 1C").Range("I3")

Counter = 0

For Each i In i1

    If i = "" Then
        Exit Sub
    Else

        For Each j In j1

            If j <> "" Then

                If Application.WorksheetFunction.IsText(Sheets("Invoice Prices").Cells(j.Row, i.Column)) Then
                    Price = "POA"
                ElseIf Application.WorksheetFunction.IsErr(Sheets("Invoice Prices").Cells(j.Row, i.Column)) Then
                    Price = "POA"
                Else
                    Price = Round(Sheets("Invoice Prices").Cells(j.Row, i.Column).Value, 1)

                Material = Application.WorksheetFunction.VLookup(j, Sheets("BTS").Range("F:G"), 2, 0)
                Customer = "00" & Application.WorksheetFunction.VLookup(i, Sheets("Customer Hub").Range("A:G"), 7, 0)

                PriceStart.Offset(Counter, 0) = Price
                MaterialStart.Offset(Counter, 0) = Material
                CustomerStart.Offset(Counter, 0) = Customer

                Counter = Counter + 1

            End If

        Next j

    End If

Next i

End Sub  
“下一个j”上的“下一个没有For”?这是如何工作的?我的Ifs都有End Ifs或似乎都以某种方式关闭,而且我的For Each都有Next。一切似乎都在逻辑上流动所以我真的很困惑我在这里缺少什么。谢谢!

3 个答案:

答案 0 :(得分:0)

如果像这样

,将你的第一行分开
    If i = "" Then
      Exit Sub
    Else
    'other code

VBA脚本环境将您的第一个if语句视为内联If,这意味着它会尝试将else语句计算为行中剩余的内容(无)。

答案 1 :(得分:0)

在更新后的代码中,您在命令End If

后缺少Price = ...

If的语法是:

  • 使用单行声明
  • 或者您必须使用End If

示例:

If i = "" Then Exit Sub    ' no end if needed

这相当于

If i = "" Then
    Exit Sub 
End if

使用Else(和ElseIf

时也是如此
If i = "" Then Debug.print "empty" Else Debug.print i    

If i = "" Then
    Debug.print "empty"
Else
    Debug.print i 
End if

答案 2 :(得分:0)

如下所示。

请注意,我已更改了声明语句,因此您不会隐式声明变体。例如:

Dim i1, j1 As Range

i1是Variant,只有j1是Range。

我还在单元格上使用了IsEmpty测试,并使用了Worksheets集合而不是Sheets

TODO:

Application.WorksheetFunction.VLookup添加错误处理而不返回匹配项。

Option Explicit

Public Sub SAP1C()

    Dim i1 As Range, j1 As Range, i As Range, j As Range
    Dim Material As String, Customer As String, Price As String
    Dim MaterialStart As Object, CustomerStart As Object, PriceStart As Object
    Dim Counter As Long

    Set i1 = Sheets("Invoice Prices").Range("B1:RL1")
    Set j1 = Sheets("Invoice Prices").Range("A6:A300")

    Set PriceStart = Sheets("SAP 1C").Range("A3")
    Set MaterialStart = Sheets("SAP 1C").Range("J3")
    Set CustomerStart = Sheets("SAP 1C").Range("I3")

    Counter = 0

    For Each i In i1

        If IsEmpty(i) Then
            Exit Sub
        Else

            For Each j In j1

                If Not IsEmpty(j) Then

                    If Application.WorksheetFunction.IsText(Worksheets("Invoice Prices").Cells(j.Row, i.Column)) Then
                        Price = "POA"
                    ElseIf Application.WorksheetFunction.IsErr(Worksheets("Invoice Prices").Cells(j.Row, i.Column)) Then
                        Price = "POA"
                    Else
                        Price = Round(Worksheets("Invoice Prices").Cells(j.Row, i.Column).Value, 1)

                        Material = Application.WorksheetFunction.VLookup(j, Worksheets("BTS").Range("F:G"), 2, 0)
                        Customer = "00" & Application.WorksheetFunction.VLookup(i, Worksheets("Customer Hub").Range("A:G"), 7, 0)

                        PriceStart.Offset(Counter, 0) = Price
                        MaterialStart.Offset(Counter, 0) = Material
                        CustomerStart.Offset(Counter, 0) = Customer

                        Counter = Counter + 1
                    End If
                End If
            Next j
        End If
    Next i
End Sub