【问题标题】:vba Vlookup Type MisMatchvba Vlookup 类型不匹配
【发布时间】:2015-11-10 11:19:29
【问题描述】:

我在下面的代码中使用了 vlookup,但它导致了类型不匹配。我已经改变了所有的惯例,使变量变体,将其更改为应用程序而不是工作表函数,但我仍然得到错误。请问谁能看出我做错了什么?

Sub createSQL()

Dim Br As Range
Dim Branch As Variant
Dim Rep As Range
Dim Report As Variant
Dim RowNo As Long
Dim SQLCode As Range
Dim SQLCode2 As String
Dim SQLCode3 As String
Dim BranchID As Long
Dim Exception As String
Dim ExCode As Variant

Set Br = Sheets("sheet3").Range("D2:D5")
SQLCode3 = Sheets("Issues").Range("F2")
SQLCode2 = Sheets("Issues").Range("F3")

For Each Branch In Br

RowNo = Branch.Row
Set SQLCode = Sheets("Sheet3").Range("L" & RowNo)
BranchID = Sheets("Sheet3").Range("C" & RowNo)
SQLCode = SQLCode3 & BranchID & SQLCode2

Set Rep = Sheets("Sheet3").Range("I" & RowNo & ":K" & RowNo).Columns

For Each Report In Rep

If Report <> "" Then
SQLCode = SQLCode & Report
Else
SQLCode = ""

End If

ExCode = Sheets("Sheet3").Range("C" & RowNo) & Sheets("Sheet3").Range("D" & RowNo) & Cells(1, Report.Column)

Exception = Application.VLookup(ExCode, Sheets("Exceptions").Range("D2:E2"), 2, False)

SQLCode = SQLCode & Exception & " Union All "

Next Report
SQLCode = Left(SQLCode, Len(SQLCode) - 10)

Next Branch

End Sub

【问题讨论】:

  • ExCode的值是多少?
  • 它将类似于这样的东西; 0R V WallisZero 委员会
  • 您的 Exception 变量是一个字符串。 Application.VLookup 不一定会作为字符串返回,例如,如果出现错误(即不匹配),它将返回 Error 对象。您可以使用 VarType 检查对象的类型 Application.VLookup 正在返回并正确处理,或者将 Exception 设置为 Variant。
  • 第一个问题是它应该是Application.WorksheetFunction.VLookup。什么类型的数据(不是变量类型)是 ExCode 和你放在 Exception 中的 Vlookup 的结果?如果您将两者都设置为字符串,它可能会起作用! ;)

标签: vba excel vlookup


【解决方案1】:

第一个问题是它应该是Application.WorksheetFunction.VLookup

第二个是VLookUp 可能会返回一个错误,这不会进入任何字符串,因此您必须在将其分配给Exception 之前对其进行测试。

我只是对此倾斜,但您正试图对仅一行进行垂直查找...这肯定是错误的根源,因为您不会在一行中找到很多不同的东西...

如果您将ExCodeException 都设置为字符串,它可能适用于以下代码:

Dim LookUp_Range As Range
Set LookUp_Range = Sheets("Exceptions").Range("D2:E10")


If IsError(Application.WorksheetFunction.VLookup(ExCode, LookUp_Range, 2, False)) Then
    Exception = "Error"
Else
    Exception = Application.WorksheetFunction.VLookup(ExCode, LookUp_Range, 2, False)
End If

所以您的整个代码将如下所示:

Sub createSQL()

Dim Br As Range
Dim Branch As Variant
Dim Rep As Range
Dim Report As Variant
Dim RowNo As Long
Dim SQLCode As Range
Dim SQLCode2 As String
Dim SQLCode3 As String
Dim BranchID As Long
Dim Exception As String
Dim ExCode As String
Dim LookUp_Range As Range

Set Br = Sheets("sheet3").Range("D2:D5")
SQLCode3 = Sheets("Issues").Range("F2")
SQLCode2 = Sheets("Issues").Range("F3")

Set LookUp_Range = Sheets("Exceptions").Range("D2:E10")

With Sheets("Sheet3")
    For Each Branch In Br
        RowNo = Branch.Row
        Set SQLCode = .Range("L" & RowNo)
        BranchID = .Range("C" & RowNo)
        SQLCode = SQLCode3 & BranchID & SQLCode2
        Set Rep = .Range("I" & RowNo & ":K" & RowNo).Columns
        For Each Report In Rep
            If Report <> "" Then
                SQLCode = SQLCode & Report
            Else
                SQLCode = ""
            End If
            ExCode = .Range("C" & RowNo) & .Range("D" & RowNo) & Cells(1, Report.Column)
            If IsError(Application.WorksheetFunction.VLookup(ExCode, LookUp_Range, 2, False)) Then
                Exception = "Error"
            Else
                Exception = Application.WorksheetFunction.VLookup(ExCode, LookUp_Range, 2, False)
                'Concatenate only if there is a match!
                SQLCode = SQLCode & Exception & " Union All "
            End If
        Next Report
        SQLCode = Left(SQLCode, Len(SQLCode) - 10)
    Next Branch
End With
End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-28
    • 1970-01-01
    • 2013-06-05
    • 1970-01-01
    • 2016-01-23
    • 2021-08-25
    相关资源
    最近更新 更多