1

我在使用下面的 vba 脚本时遇到了一些问题。这很简单。它旨在获取下拉列表的值并根据另一张表中的是/否/空值进行响应。代码工作正常,除了 vlookup 行中存在阻止脚本运行的问题。

前两行 vlookup 行返回“应用程序定义或对象定义错误”,第三行返回“数据类型不匹配”错误。公式引用的每个单元格都被格式化为文本,所以我不确定问题是什么......任何反馈都会非常有帮助。谢谢!

If Not Intersect(Target, Range("countryProductCell")) Is Nothing Then
    lastcolumn = ActiveSheet.UsedRange.Column - 1 + ActiveSheet.UsedRange.Columns.Count
    cellRow = Target.Row
    defaultCellColumn = 4
    Dim countryIndex As Variant
    countryIndex = Array(6, 6, 30, 21, 35, 29, 32, 20, 23, 18, 19, 34, 33, 22, 31, 26, 25, 27, 28, 7, 8, 15, 12, 10, 13, 11, 16, 17, 9)
    i = 0
    For Each countryCell In Range(Cells(cellRow, 5), Cells(cellRow, lastcolumn))
        'If Default is selected
        'If Not Target.Value = "(Select Title)" Then
            'If Product is not selected
            If countryCell.Value = "Use Default" Then
               'Look Up Purchaseablility, Needs Array
                If Not Application.VLookup(ActiveSheet.Cells(cellRow, defaultCellColumn), Sheets("Active Product Catalog").Range("E:AK"), countryIndex(i), False) = "Yes" Then
                    'If Not Purchaseable, Change Color
                    countryCell.Interior.ColorIndex = 3
                End If
                If Application.VLookup(ActiveSheet.Cells(cellRow, defaultCellColumn), Sheets("Active Product Catalog").Range("E:AK"), countryIndex(i), False) = "Yes" Then
                    'If Purchaseable, Change Color
                    countryCell.Interior.ColorIndex = 35
                End If
            Else
                If Application.VLookup(ActiveSheet.Cells(cellRow, countryCell.Column), Sheets("Active Product Catalog").Range("E:AK"), countryIndex(i), False) = "Yes" Then
                    countryCell.Interior.ColorIndex = 35
                End If
            End If
        'Else
        'End If
        i = i + 1
    Next
End If
4

2 回答 2

0

您在前两个公式中定义defaultCellColumn=4但使用未定义的变量。在模块的开头defaultCell使用是个好主意- 然后在编译时会被捕获。Option Explicit

不确定第三个错误,但您的countryIndex数组一直到 35,这比E:AK. 认识到第三个参数VLOOKUP使用相对于范围的列偏移量,而不是绝对列(因此 2=F 在您的情况下,35=AM 超出您指定的范围并会引发错误。)

于 2013-05-05T03:38:22.687 回答
0

Data type mismatch可以使用以下方法处理错误。

On Error Resume Next
Result = Application.VLookup(Cells(cellRow, countryCell.Column), Sheets("Active Product Catalog").Range("E:AK"), countryIndex(i), False)
If Result = "Error 2042" Then
    Result = "Nothing Found"
Else
    MsgBox Result
End If
On Error GoTo 0
于 2013-05-05T04:27:26.187 回答