
[英]Excel - How can I get the average of cells where the value in one column is X and the value in another column is Y?
[英]Increasing Value in Column by X if Value in Another Column is Like Y
我正在寻求一些帮助来调整我的 VBA。 我相信我很接近,但无法弄清楚最后的必要步骤。 如果 C 列中的字符串类似于 X,我想将 K 列中的值增加 X%。以下是我已经拥有的,如果需要任何进一步的信息,请告诉我。 谢谢!
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
从您的问题来看,您在 C 列中实际拥有的内容有点模棱两可,这对答案产生了影响。 我假设您可能有一个类似“Item01”的字符串,并且您想使用“Item”或“01”或该字符串的任何其他子集来识别它。
正如评论中提到的,根据您的代码,似乎没有必要向后循环,所以我也删除了它。
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
快速修复<\/strong>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.