Excluding certain cell values from if statement macro vba

ぐ巨炮叔叔 提交于 2021-01-29 04:25:55

问题


I have a macro that runs through 3000 ++ lines of data and I want to create an exclusion list, so for instance when my if statement goes to look at a range if it matches a certain set of names for it to skip that row and keep going.

Example would be :

If Column A has a value of "J Smith" for it to skip that line and continue as it was, the surnames have been changed to 'Smith' but these may vary.

The entire code around it works if I remove the code from the macro it works perfectly without any problems but as soon as i add it to give me a bit flexibility with the data it dies:

 If Range("A" & r).Value = "C Hopcroft" Or "M Pyatt" Or "D Freeman" Or "D Jolley" Or "A Marsh" Or "M Heslop" Or "K Knowles" Or "J Robertson" Or "D Wood" Then
            r = r + 1
            Else

Other than when it finds when of the names within column A it crashes on a 'Type Mismatch' but the name seems to match perfectly as if I hover over 'If Range("A" & r).Value it gives me the correct value and if I remove the names from the data is look at the macro will complete perfectly.

Any ideas?

My macro code is below:

Sub unusedMacro()

Dim lr As Long, lr2 As Long, r As Long
    Set Sh1 = ThisWorkbook.Worksheets("Depot Dashboard")
    Set Sh2 = ThisWorkbook.Worksheets("Summary Data")
    Sh2.Select
    lr = Sh2.Cells(Rows.Count, "A").End(xlUp).Row
    x = 39
    For r = 2 To lr

        If Range("C" & r) = "TRUE" Then
        If Range("N" & r).Value = Worksheets("Depot Dashboard").Range("B17") Then
        If Range("A" & r).Value = "C Hopcroft" Or "M Pyatt" Or "D Freeman" Or "D Jolley" Or "A Marsh" Or "M Heslop" Or "K Knowles" Or "J Robertson" Or "D Wood" Then
        r = r + 1
        Else
            Sh2.Cells(r, 15).Copy
            Sh1.Cells(x, 7).PasteSpecial xlPasteValues  'Date
            Sh2.Cells(r, 1).Copy
            Sh1.Cells(x, 8).PasteSpecial xlPasteValues  'Name
            Sh2.Cells(r, 28).Copy
            Sh1.Cells(x, 9).PasteSpecial xlPasteValues  'Turn ID
            Sh2.Cells(r, 29).Copy
            Sh1.Cells(x, 10).PasteSpecial xlPasteValues  'Turn Desc
            x = x + 1
        End If
        End If
        End If
    Next r
    Sh1.Select
    Set Sh1 = Nothing
    Set Sh2 = Nothing
    Set x = Nothing


End Sub

回答1:


The IF line of an IF statement must return a Boolean value [True, False]. As it stands, the first test in If Range("A" & r).Value = "C Hopcroft" Or "M Pyatt" Or "D Freeman" Or "D Jolley" returns a Boolean value, but the others don't. To correct it, each string (name) must be tested against the value in the cell.

For example:

If Range("A" & r).Value = "C Hopcroft" Or Range("A" & r).value = "M Pyatt" Or Range("A" & r).value) = "D Freeman" Or Range("A" & r).value = "D Jolley"

Does that fix the problem?



来源:https://stackoverflow.com/questions/28677454/excluding-certain-cell-values-from-if-statement-macro-vba

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!