简体   繁体   English

如果A列包含x AND B列包含y然后添加值

[英]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". 我想创建一个宏,如果列D包含文本“(2)”并且列AG包含文本“ Adult”,则将值2 添加到单元格。

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). 我创建了一个宏,到目前为止,如果D列包含文本“(2)”,则将单元格的值更改为5(而不是添加到其中)-我花了一些时间弄乱“ And”函数,但是我似乎找不到一种方法可以同时考虑D列中的“(2)”文本 AG列中的“成人”文本(我只能使其搜索一个或另一个) 。

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. 我基本上只是想弄清楚如何从AG列中包含“成人”文本,以及如何使宏添加而不是更改最终值。 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: 根据您的代码判断,您想在C列中添加 2,如果是这种情况,这应该可以解决问题:

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) . 您可以像搜索(2)一样搜索Adult Just use the InStr-function two times and combine the result-booleans. 只需两次使用InStr函数并将结果布尔值组合在一起。 You can do that in two ways, logical with And or nested with two if-statements: 您可以通过两种方式来做到这一点,用And逻辑或嵌套两个if语句:

  • 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. 然后将2添加到该变量( myVariable = myVariable + 2 ),然后将其值设置为您的单元格,而不是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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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