尝试使用百分比数字作为值 [英]Trying to use a number with percentage as value

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"

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值