简体   繁体   中英

If column A contains x AND column B contains y THEN add value

I'm very new to macros (it's been a few days now!) but slowly working my way through. I'd like to create a macro that adds a value of 2 to a cell if column D contains the text "(2)" AND column AG contains the text "Adult".

I've created a macro that so far changes the value of the cell to 5 (instead of adding to it) if column D contains the text "(2)" - I've spent a while messing around with "And" functions but I can't seem to find a way to make it take into account the both the "(2)" text in the D column and the "Adult" text in the AG column (I can only make it search one or the other).

Here's my attempt (this doesn't include any of my failed attempts to include the "Adult" text):

Sub BUBFindGuests()
    Dim SrchRng As Range
    lastRow = Range("D" & Rows.Count).End(xlUp).Row
    Set SrchRng = Range("D1:D" & lastRow, "AG1:AG" & lastRow)
    For Each cel In SrchRng
        If InStr(1, cel.Value, "(2)") > 0 Then
            With cel.Offset(0, 39)
                .Offset(-1, 0) = "5"
            End With
        End If
    Next cel
End Sub

I'm basically just trying to work out how to include the "Adult" text from the AG column, and also how to make the macro add rather than change the end value. I'm also relatively certain that some parts of my code are unnecessary or clunky, but with my level of experience I'm unsure of how to correct that. Any help would be much appreciated.

Judging by your code, you want to add 2 to column C, if that's the case this should do the trick:

Sub BUBFindGuests()

lastRow = Sheets("SHEETNAME").Range("D" & Rows.Count).End(xlUp).Row

For x = 1 to lastRow
  If InStr(1, Sheets("SHEETNAME").Cells(x, 4), "(2)") <> 0 _ 'Checks column D
  And Instr(1, Sheets("SHEETNAME").Cells(x, 33), "Adult") <> 0 Then 'Checks column AG
    Sheets("SHEETNAME").Cells(x, 3).Value = _
    Sheets("SHEETNAME").Cells(x, 3).Value + 2 'Change 3 to the appropriate column 
  End If
Next x

End Sub

You can search for Adult just as you searched for the (2) . Just use the InStr-function two times and combine the result-booleans. You can do that in two ways, logical with And or nested with two if-statements:

  • If InStrResult1 **And** InStrResult2 Then 'do stuff End If
  • If InStrResult1 Then If InStrResult2 Then 'do stuff End If End If

Sorry for the bad formation.

You can then store the current value of your cell in a variable. Then add 2 to that variable ( myVariable = myVariable + 2 ) and then set its value to your cell instead of 5.

EDIT: It turns out I misread your question. See revised code.

Sub BUBFindGuests()

    Dim SrchRng As Range
    lastRow = Range("D" & Rows.Count).End(xlUp).Row
    Set SrchRng = Range("D1:D" & lastRow, "AG1:AG" & lastRow)
    For Each cel In SrchRng

        If InStr(1, cel.Value, "(2)") > 0 And InStr(1, cel.Value, "Adult") > 0 Then cel.Offset(-1, 39).Value = .Offset(-1, 0).Value & "5"

    Next cel

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM