如果包含这些值,则遍历列并替换。如果不按原样离开
Posted
技术标签:
【中文标题】如果包含这些值,则遍历列并替换。如果不按原样离开【英文标题】:Go through column and replace if containing these values. If not leave as is 【发布时间】:2015-12-22 21:06:33 【问题描述】:我想遍历 L 列并将包含这些文本字段的单元格替换为 0。如果不是,我想保持原样。代码运行但在遇到的第一个 #N/A
处停止。
Sub Drops()
Dim i&, z&
With Application
.ScreenUpdating = False
.Calculation = xlCalculationManual
With Sheets("Input")
i = .Cells(Rows.Count, "L").End(xlUp).Row
For z = i To 2 Step -1
If (.Cells(z, "L").Value2 Like "*Customer Dropoff*" _
Or .Cells(z, "L").Value2 Like "*RE-Ships No pick up charge*" _
Or .Cells(z, "L").Value2 Like "*Undeliverable Publication Mail (NO P/U CHARGE)*" _
Or .Cells(z, "L").Value2 Like "*RETURNS*" _
Or .Cells(z, "L").Value2 Like "*K2 Fed Ex*" _
Or .Cells(z, "L").Value2 Like "*WorldNet Shipping*" _
Or .Cells(z, "L").Value2 Like "*OSM (NO P/U COST)*" _
Or .Cells(z, "L").Value2 Like "*TEST PICK UP*") Then
.Cells(z, "L").Value2 = 0
End If
Next z
z = .Cells(Rows.Count, "L").End(xlUp).Row
End With
.ScreenUpdating = True
.Calculation = xlCalculationAutomatic
End With
MsgBox i - z & " Rows has been changed!"
End Sub
【问题讨论】:
它突出显示整个 if 或 block 【参考方案1】:未经测试:
Sub Drops()
Dim i As Long, z As Long, arr, v, n As Long
Dim numEdits As Long
numEdits = 0
arr = Array("Customer Dropoff", "RE-Ships No pick up charge", _
"Undeliverable Publication Mail (NO P/U CHARGE)") 'etc
With Application
.ScreenUpdating = False
.Calculation = xlCalculationManual
End With
With Sheets("Input")
i = .Cells(Rows.Count, "L").End(xlUp).Row
For z = i To 2 Step -1
v = .Cells(z, "L").Value
If IsError(v) Then
'ignore error ?
Else
For n = LBound(arr) To UBound(arr)
If v Like "*" & arr(n) & "*" Then
.Cells(z, "L").Value = 0
numEdits = numEdits + 1
Exit For
End If
Next n
End If
Next z
End With
With Application
.ScreenUpdating = True
.Calculation = xlCalculationAutomatic
End With
MsgBox numedits & " Row(s) changed!"
End Sub
【讨论】:
效果很好,但消息框不显示更改的单元格数。当进行大量更改时,它显示为 0。不过还是谢谢。 不清楚你想用那个 Msgbox 做什么。请参阅上面的更新。以上是关于如果包含这些值,则遍历列并替换。如果不按原样离开的主要内容,如果未能解决你的问题,请参考以下文章
如果所有值都相同,则循环遍历 bash 测试中的列 - AWK