尝试连接变量时键入不匹配

时间:2014-12-05 06:28:36

标签: excel vba excel-vba

如何将2'Variant'变量组合成单个Variant或字符串的任何想法。要查找的行是:

SelectColumnsRows = DisplayColumns & DisplayRows

我得到错误类型不匹配,因为尝试将它与ws.Range(SelectColumnsRows)一起使用。选择。任何帮助都会很棒。感谢

Sub previewSub()
   Dim ws As Worksheet

    For Each ws In ActiveWorkbook.Worksheets
        Call previewDisplayColumnRow(ws)

    Next
    MsgBox "Click on each Worksheets to preview columns/rows which will be displayed after clicking on 'UPDATE LAYOUT'"
End Sub

Sub previewDisplayColumnRow(ws As Worksheet)

    Dim DisplayColumns       As Variant
    Dim DisplayRows          As Variant
    Dim HideColumnsRows      As Variant
    Dim SelectColumnsRows    As String

    Dim myrange

    Set myrange = Worksheets("Control").Range("range_sheetProperties")

    'Lookup Worksheet name and identify columns & rows to display
    HideColumnsRows = Application.VLookup(ws.Name, myrange, 5, False)
    DisplayColumns = Application.VLookup(ws.Name, myrange, 6, False)
    DisplayRows = Application.VLookup(ws.Name, myrange, 7, False)

    SelectColumnsRows = DisplayColumns & DisplayRows

   'Preview Columns / Rows which will be hidden
    If Not IsError(HideColumnsRows) Then

            If HideColumnsRows = "Y" Then
            'MsgBox ws.Name & " - " & SelectColumnsRows
            ws.Activate
            ws.Range(SelectColumnsRows).Select
            End If
    End If

End Sub

2 个答案:

答案 0 :(得分:0)

如果这两个vlookup中的一个有任何一个返回错误,即“#N / A”,它会在尝试将其分配给字符串时抛出类型不匹配错误。

答案 1 :(得分:0)

我猜变量DisplayColumns和DisplayRows有错误值。

您的代码会检查“HideColumnsRows”的错误,因此您可能会发生错误。在检查之前,不应使用'HideColumnsRows','DisplayColumns'和'DisplayRows'。

我修改了代码以避免错误,如下所示。试试吧。

Sub previewSub()
   Dim ws As Worksheet

    For Each ws In ActiveWorkbook.Worksheets
        Call previewDisplayColumnRow(ws)

    Next
    MsgBox "Click on each Worksheets to preview columns/rows which will be displayed after clicking on 'UPDATE LAYOUT'"
End Sub

Sub previewDisplayColumnRow(ws As Worksheet)

    Dim DisplayColumns       As Variant
    Dim DisplayRows          As Variant
    Dim HideColumnsRows      As Variant
    Dim SelectColumnsRows    As String

    Dim myrange

    Set myrange = Worksheets("Control").Range("range_sheetProperties")

    'Lookup Worksheet name and identify columns & rows to display
    HideColumnsRows = Application.VLookup(ws.Name, myrange, 5, False)

    If Not IsError(HideColumnsRows) Then 'If no error.
        DisplayColumns = Application.VLookup(ws.Name, myrange, 6, False)
        DisplayRows = Application.VLookup(ws.Name, myrange, 7, False)

        SelectColumnsRows = DisplayColumns & DisplayRows

       'Preview Columns / Rows which will be hidden

        If HideColumnsRows = "Y" Then
            'MsgBox ws.Name & " - " & SelectColumnsRows
            ws.Activate
            ws.Range(SelectColumnsRows).Select
        End If
    Else 'Error: ws.name is not included in myrange.
        MsgBox ws.Name & " does not exist in sheet property information. Skipped.") 'Message for explain the error.
    End If

End Sub