在VBA中检查两个标准

时间:2017-11-20 04:17:43

标签: excel vba excel-vba

我有这个VBA宏搜索范围'SV'并更新另一个具有特定值的单元格('PEL-SVFC')。我想扩展这个,所以它只更新该单元格,如果它本身在更改之前包含另一个值(PEL-CD),但我得到类型不匹配错误。

下面是宏,当前的工作版本以及引发类型不匹配错误的版本:

Sub Update3011()
'Update various values in the 3011

'Define some variables
Dim PromoNameColumn As Range
Dim PromoNameColumnAsArray As Variant

Dim AccountNameColumn As Range
Dim AccountNameColumnAsArray As Variant

Dim i As Long

'First action: search cells in column W for text containing "SV". Of those matches, if the cell in column D of that row matches "PEL-CD", replace it with "PEL-SVFC".

Set PromoNameColumn = Range("W2:W" & ThisWorkbook.Worksheets("3011").UsedRange.Rows.Count)
PromoNameColumnAsArray = PromoNameColumn  ' PromoNameColumnAsArray is now array

Set AccountNameColumn = Range("D2:D" & ThisWorkbook.Worksheets("3011").UsedRange.Rows.Count)
AccountNameColumnAsArray = AccountNameColumn  ' AccountNameColumnAsArray is now array

For i = LBound(PromoNameColumnAsArray, 1) To UBound(PromoNameColumnAsArray, 1)

    If InStr(1, PromoNameColumnAsArray(i, 1), "SV") Then 'If the range "W2:2" contains SV and the range "D2:D" contains "PEL-CD", continue
        AccountNameColumnAsArray(i, 1) = "PEL-SVFC"
    End If

Next

AccountNameColumn = AccountNameColumnAsArray

MsgBox ("3011 updated.")

End Sub

非工作人员:

Sub Update3011()
'Update various values in the 3011

'Define some variables
Dim PromoNameColumn As Range
Dim PromoNameColumnAsArray As Variant

Dim AccountNameColumn As Range
Dim AccountNameColumnAsArray As Variant

Dim i As Long

'First action: search cells in column W for text containing "SV". Of those matches, if the cell in column D of that row matches "PEL-CD", replace it with "PEL-SVFC".

Set PromoNameColumn = Range("W2:W" & ThisWorkbook.Worksheets("3011").UsedRange.Rows.Count)
PromoNameColumnAsArray = PromoNameColumn  ' PromoNameColumnAsArray is now array

Set AccountNameColumn = Range("D2:D" & ThisWorkbook.Worksheets("3011").UsedRange.Rows.Count)
AccountNameColumnAsArray = AccountNameColumn  ' AccountNameColumnAsArray is now array

For i = LBound(PromoNameColumnAsArray, 1) To UBound(PromoNameColumnAsArray, 1)

    If InStr(1, PromoNameColumnAsArray(i, 1), "SV") And InStr(1, AccountNameColumnAsArray(i, 1), "PEL-CD") Then 'If the range "W2:2" contains SV and the range "D2:D" contains "PEL-CD", continue
        AccountNameColumnAsArray(i, 1) = "PEL-SVFC"
    End If

Next

AccountNameColumn = AccountNameColumnAsArray

MsgBox ("3011 updated.")

End Sub

因此,导致错误的位是And InStr(1, AccountNameColumnAsArray(i, 1), "PEL-CD")语句中的if,是代码块之间唯一不同的东西。

如何在更新单元格之前修改此代码以添加额外的逻辑检查?

2 个答案:

答案 0 :(得分:1)

您的代码应该有效。如果列D或W中的任何单元格包含错误值,则它不会仅在一种情况下有效。你的数据就是这种情况吗?

如果是,您可以添加另一个条件来检查这两列中的值是否不是错误值。

For i = LBound(PromoNameColumnAsArray, 1) To UBound(PromoNameColumnAsArray, 1)

    If Not IsError(PromoNameColumnAsArray(i, 1)) And Not IsError(AccountNameColumnAsArray(i, 1)) Then
        If InStr(1, PromoNameColumnAsArray(i, 1), "SV") > 0 And InStr(1, AccountNameColumnAsArray(i, 1), "PEL-CD") > 0 Then 'If the range "W2:2" contains SV and the range "D2:D" contains "PEL-CD", continue
            AccountNameColumnAsArray(i, 1) = "PEL-SVFC"
        End If
    End If
Next

答案 1 :(得分:0)

InStr返回索引,它找到了您要搜索的字符串,例如

InStr(1, "_PEL-CD_", "PEL-CD")

将返回2.

在你的第一个代码中,你很幸运它在索引1处找到“SV”,幸运的是,VBA将1理解为True,所以当你要求VBA将1和2评估为布尔值时,它将返回错误。

只需将第二个宏中的代码更改为

即可
If InStr(1, PromoNameColumnAsArray(i, 1), "SV") > 0 And InStr(1, AccountNameColumnAsArray(i, 1), "PEL-CD") < 0 Then

会解决它。