Hi there I am trying to run a loop that changes the value of all cells containing 0% to value "N/A". I wrote the following code:
您好我在尝试运行一个循环,将包含0%的所有单元格的值更改为值“N / A”。我写了以下代码:
Dim Fail As Integer
Fail = "0%"
ThisWorkbook.Sheets("Summary").Activate
LastRow = Cells(Rows.Count, "G").End(xlUp).Row
For X = 22 To LastRow
If Range("G" & X).Value = Fail Then Range("G" & X).Value = "N/A"
Next X
but all it does is that it replaces all the values in column G with "N/A". Where did I go wrong?
但它所做的只是它用“N / A”替换G列中的所有值。我哪里做错了?
2 个解决方案
#1
1
The character "%" can't exist within an integer. You can only use whole numbers when dimming an integer. Source
字符“%”不能存在于整数内。调暗整数时,只能使用整数。资源
Your problem lies within Fail = "0%", excel automatically changes the formatting of all cells containing % to percentage. So what you need to do is to change Fail = "0%" to Fail = "0".
你的问题在于Fail =“0%”,excel会自动将包含%的所有单元格的格式更改为百分比。所以你需要做的是将Fail =“0%”更改为Fail =“0”。
See the final code below:
请参阅下面的最终代码:
Dim Fail As Integer
Fail = "0"
ThisWorkbook.Sheets("Summary").Activate
LastRow = Cells(Rows.Count, "G").End(xlUp).Row
For X = 22 To LastRow
If Range("G" & X).Value = Fail Then Range("G" & X).Value = "N/A"
Next X
#2
2
Use simple replace statement on your range.
在您的范围内使用简单的替换语句。
Worksheets("Summary").Range("G22:G1000").Replace What:="0%", Replacement:="NA"