因此,在我的宏中,如果列A中的单词“汇款”,我试图将列C中的所有数字都更改为负数,但是我不完全确定自己的代码有什么问题。我收到“对象'_Global'的方法'范围'失败”:
Dim ws1 As Worksheet
ws1.AutoFilterMode = False
With ws1.Range("A1:A" & ws1LR)
.AutoFilter Field:=1, Criteria1:="*Remit*"
Set rngFiltered = Range("C1:C").SpecialCells(xlCellTypeVisible)
rngFiltered.Value = rng.Filtered.Value * -1
End With
为什么我会这样尝试获得“对象必需”的范围:
With ws1.Range("A1:A" & ws1LR)
.AutoFilter Field:=1, Criteria1:="*Remit*"
Set rngFiltered = Range("C1:C" & ws1LR).SpecialCells(xlCellTypeVisible)
rngFiltered.Value = rng.Filtered.Value * -1
End With
任何帮助将不胜感激!
答案 0 :(得分:0)
此代码将满足您的要求,请确保更改贴花的工作表名称以匹配您的工作表名称。
它使用Marcucciboy2建议的Range Find。
对于具有大量数据行的工作表,可能需要一些时间才能完成。
Sub NegativeRemitNumbers()
Dim SearchName As Variant
Dim NumberChange As Integer
Dim ws As Worksheet
Set ws = Sheets("Sheet1") ' Change to match your sheet name
For Each SearchName In ws.Range("A:A") ' This is the column we will search
If SearchName.Value = "remit" Then ' if the serach vale = Remit then turn the number negative
NumberChange = ws.Cells(SearchName.Row, 3).Value ' get the number value in colunm C
NumberChange = NumberChange - NumberChange - NumberChange ' Take away the value from itself twice to get a negative value (there maybe better ways to do this but this was quick to write)
ws.Cells(SearchName.Row, 3).Value = NumberChange ' replace the positive number with the negative positive number
End If
Next
End Sub
使用此代码,如果要运行两次,则它将把负数恢复为正数,您需要在处理该单元格值之前添加一条if语句,以检查数字是否为负数。