Elseif条件下的错误1004

时间:2018-02-14 15:01:03

标签: excel vba excel-vba

我有一张excel表,看起来像这样:

excel sheet

对于每个单元格,我想找到单独工作表中的行和列标题(" Obj")的纬度和经度(每个" Obj"类型为1个工作表)。我把我的代码放在了下面。

目前,它在第一行工作正常,直到它达到" ObjB1"。当我在"调试模式"中运行时,它到达 停止前If WorksheetFunction.IsNumber(WorksheetFunction.Search("ObjA", Sheets("Distancier").Range("A1:P20000").Cells(1, j))) = True Then

  

错误1004

没有进入" Elseif"条件。

子距离()     昏昏欲睡     Dim j As Long     Dim Lat1 As Variant     Dim Lat2 As Variant     Dim Long1 As Variant     Dim Long2 As Variant     Dim Dist As Variant

'Going through the empty cells
    For i = 2 To Sheets("Distancier").Range("A" & Rows.count).End(xlUp).Row
    For j = 2 To Sheets("Distancier").Range("A" & Rows.count).End(xlUp).Row

    ' If ObjA is part of the string in the row header (Column A), then lat1 and long1 take the values of the latitude and longitude of the text in the row header

        If WorksheetFunction.IsNumber(InStr(1, Sheets("Distancier").Range("A1:P20000").Cells(i, 1), "ObjA", vbTextCompare)) = True Then
            Lat1 = WorksheetFunction.VLookup(Sheets("Distancier").Range("A1:P20000").Cells(i, 1).Text, Sheets("ObjetsA").Range("A2:P2000"), 3, False)
            Long1 = WorksheetFunction.VLookup(Sheets("Distancier").Range("A1:P20000").Cells(i, 1).Text, Sheets("ObjetsA").Range("A2:P2000"), 4, False)
        ' Same here but ObjB is part of the string
        ElseIf WorksheetFunction.IsNumber(InStr(1, Sheets("Distancier").Range("A1:P20000").Cells(i, 1), "ObjB", vbTextCompare)) = True Then
            Lat1 = WorksheetFunction.VLookup(Sheets("Distancier").Range("A1:P20000").Cells(i, 1).Text, Sheets("ObjetsB").Range("A2:P2000"), 3, False)
            Long1 = WorksheetFunction.VLookup(Sheets("Distancier").Range("A1:P20000").Cells(i, 1).Text, Sheets("ObjetsB").Range("A2:P2000"), 4, False)

        ElseIf WorksheetFunction.IsNumber(InStr(1, Sheets("Distancier").Range("A1:P20000").Cells(i, 1), "ObjC", vbTextCompare)) = True Then
            Lat1 = WorksheetFunction.VLookup(Sheets("Distancier").Range("A1:P20000").Cells(i, 1).Text, Sheets("ObjetsC").Range("A2:P2000"), 3, False)
            Long1 = WorksheetFunction.VLookup(Sheets("Distancier").Range("A1:P20000").Cells(i, 1).Text, Sheets("ObjetsC").Range("A2:P2000"), 4, False)

        ElseIf WorksheetFunction.IsNumber(InStr(1, Sheets("Distancier").Range("A1:P20000").Cells(i, 1), "ObjD", vbTextCompare)) = True Then
            Lat1 = WorksheetFunction.VLookup(Sheets("Distancier").Range("A1:P20000").Cells(i, 1).Text, Sheets("ObjetsD").Range("A2:P2000"), 3, False)
            Long1 = WorksheetFunction.VLookup(Sheets("Distancier").Range("A1:P20000").Cells(i, 1).Text, Sheets("ObjetsD").Range("A2:P2000"), 4, False)
        Else:
            Lat1 = 0
            Long1 = 0
        End If

        ' This block does the same thing as the first block but looks through the column headers (row 1)
        If WorksheetFunction.IsNumber(InStr(1, Sheets("Distancier").Range("A1:P20000").Cells(1, j), "ObjA", vbTextCompare)) = True Then
            Lat2 = WorksheetFunction.VLookup(Sheets("Distancier").Range("A1:P20000").Cells(1, j).Text, Sheets("ObjetsA").Range("A2:P2000"), 3, False)
            Long2 = WorksheetFunction.VLookup(Sheets("Distancier").Range("A1:P20000").Cells(1, j).Text, Sheets("ObjetsA").Range("A2:P2000"), 4, False)

        ElseIf WorksheetFunction.IsNumber(InStr(1, Sheets("Distancier").Range("A1:P20000").Cells(1, j), "ObjB", vbTextCompare)) = True Then
            Lat2 = WorksheetFunction.VLookup(Sheets("Distancier").Range("A1:P20000").Cells(1, j).Text, Sheets("ObjetsB").Range("A2:P2000"), 3, False)
            Long2 = WorksheetFunction.VLookup(Sheets("Distancier").Range("A1:P20000").Cells(1, j).Text, Sheets("ObjetsB").Range("A2:P2000"), 4, False)

        ElseIf WorksheetFunction.IsNumber(InStr(1, Sheets("Distancier").Range("A1:P20000").Cells(1, j), "ObjC", vbTextCompare)) = True Then
            Lat2 = WorksheetFunction.VLookup(Sheets("Distancier").Range("A1:P20000").Cells(1, j).Text, Sheets("ObjetsC").Range("A2:P2000"), 3, False)
            Long2 = WorksheetFunction.VLookup(Sheets("Distancier").Range("A1:P20000").Cells(1, j).Text, Sheets("ObjetsC").Range("A2:P2000"), 4, False)

        ElseIf WorksheetFunction.IsNumber(InStr(1, Sheets("Distancier").Range("A1:P20000").Cells(1, j), "ObjD", vbTextCompare)) = True Then
            Lat2 = WorksheetFunction.VLookup(Sheets("Distancier").Range("A1:P20000").Cells(1, j).Text, Sheets("ObjetsD").Range("A2:P2000"), 3, False)
            Long2 = WorksheetFunction.VLookup(Sheets("Distancier").Range("A1:P20000").Cells(1, j).Text, Sheets("ObjetsD").Range("A2:P2000"), 4, False)
         Else:
            Lat1 = 0
            Long1 = 0
        End If

        ' If the latitudes and longitudes are numbers, then the distance between the two points is calculated.

        If WorksheetFunction.And(WorksheetFunction.IsNumber(Lat1), WorksheetFunction.IsNumber(Lat2), WorksheetFunction.IsNumber(Long1), WorksheetFunction.IsNumber(Long2)) Then
            Dist = WorksheetFunction.IfError(WorksheetFunction.Acos(Sin((3.14 / 180) * Lat1) * Sin((3.14 / 180) * Lat2) + Cos((3.14 / 180) * Lat1) * Cos((3.14 / 180) * Lat2) * Cos((3.14 / 180) * (Long2 - Long1))) * 6371, 0) * 1.3
        End If
        Sheets("Distancier").Range("A1:P20000").Cells(i, j).Value = Dist
    Next
Next

End Sub

欢迎所有帮助,因为我无法弄清楚它为什么不起作用!

感谢。

0 个答案:

没有答案