I want to perform a simple calculation on another cell based off a key word in the string from a different cell. For example Column H might have a description along the lines of "Purchase of 50,000 shares." I want to multiple Column B by -1 if the word Purchase appears. Is it possible to build a condition off of a key word in a string?
You can do it in formula or VBA.
In formula, say B1 had content X before, you change to formula =(X) * IF(ISNUMBER(SEARCH("Purchase", H1)), -1, 1)
In VBA, try this
Sub NegateB_IfPurchaseH()
Dim celB As Range
Dim celH As Range
Dim celPtr As Range
' you can change 1:10 to any other range
Set celB = Range("B1:B10")
Set celH = celB.Cells(1, 7)
For Each celPtr In celB
If InStr(1, celPtr.Cells(1, 7).Value, "purchase", vbTextCompare) > 0 Then
celPtr.Formula = "=-" & Mid(celPtr.Formula, 2)
End If
Next
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.