简体   繁体   中英

Increasing Value in Column by X if Value in Another Column is Like Y

I am looking for some help adjusting my VBA. I believe I am close but can't figure out the final necessary steps. I want to increase the value in column K by X percent if the string in column C is like X. Below is what I have already, please let me know if there is any further information needed. Thanks!

Sub Cost_Increase()

Dim strInput As String
strInput = InputBox("Input Part Number", "Part Number for Cost Increase", "Enter your input text HERE")


Dim iInput As Integer
iInput = InputBox("Please enter an increase amount", "Increase Customer Cost")

Dim r As Long, ws As Worksheet

Set ws = Sheets("All Cust")

For r = ws.Cells(Rows.Count, "C").End(xlUp).Row To 2 Step -1
    
If ws.Range("C" & r).Value Like strInput Then
        
ws.Range("K" & r).Value = ((ws.Range("K" & r).Value * iInput) + ws.Range("K" & r).Value)
    
End If

Next r

End Sub

From your question, it's a little ambiguous what you actually have in column C, and that makes a difference to the answer. I've assumed you might have a string like "Item01" and you want to identify it using "Item" or "01", or any other subset of that string.

Sub Cost_Increase()

    Dim findString As String, multiplier As Long, colC As Range, r As Range
    
    findString = InputBox("Input Part Number", "Part Number for Cost Increase", "Enter your input text HERE")
    multiplier = InputBox("Please enter an increase amount", "Increase Customer Cost")
    
    With ThisWorkbook.Worksheets("All Cust")
        Set colC = .Range(.Range("C2"), .Range("C1048576").End(xlUp))
    End With
    
    For Each r In colC
        If InStr(r.Value, findString) > 0 Then
            r.Offset(0, 8).Value = r.Offset(0, 8) * (1 + multiplier)
        End If
    Next r

End Sub

Increase By Percentage<\/h2>

A Quick Fix<\/strong>

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