所以我有一张看起来像这样的表:
mtcars[c(1, 1), ]
如果E F G H
BS m1 blah INDC
BSB m2 blah INDC
BS m1 blah INDC
BS m2 blah INDC
中的值等于Column H
且值为"INDC"
,我需要做的是将"IOE"
的值从Column E
更改为"BS"
在Column F
中等于"m1"
(与第3行相同)。
我曾尝试编写几个代码但无法使其工作。
答案 0 :(得分:4)
你可以尝试这样的事情......
Sub ReplaceValues()
Dim ws As Worksheet
Dim lr As Long
Application.ScreenUpdating = False
Set ws = ActiveSheet
lr = ws.Cells(Rows.Count, "E").End(xlUp).Row
ws.AutoFilterMode = False
With ws.Rows(1)
.AutoFilter field:=8, Criteria1:="INDC"
.AutoFilter field:=5, Criteria1:="BS"
.AutoFilter field:=6, Criteria1:="m1"
If ws.Range("E1").CurrentRegion.SpecialCells(xlCellTypeVisible).Rows.Count > 1 Then
ws.Range("H2:H" & lr).SpecialCells(xlCellTypeVisible).Value = "IOE"
End If
.AutoFilter
End With
Application.ScreenUpdating = True
End Sub
答案 1 :(得分:2)
此代码将有效地完成工作。
如果要仅在已经等于" INDC"时更改列H值,则需要在if语句中添加另一个标准AND wsh.Cells(i,8) = "INDC"
Dim i As Integer
Dim wsh as Worksheet
Sub substitute()
Set wsh = ActiveSheet
i = 1
While wsh.Cells(i,5) <> ""
If wsh.Cells(i,5) = "BS" AND wsh.Cells(i,6) = "m1" Then wsh.Cells(i,8) = "IOE"
i = i + 1
Wend
End Sub