有人可以帮助我理解为什么我的代码出问题了吗?
下面的代码是一些示例数据。毫无疑问,它将删除具有百分比的第一行。
预先感谢:)
`With ThisWorkbook.Sheets(1)
hcRow = Cells(Rows.Count, "A").End(xlUp).Row
p = 5
For i = 4000 To 1 Step -1
.Cells(i, p).Value = Format(.Cells(i, p).Value, "Percent")
If .Cells(i, p).Value < 0.01 Then
.Cells(i, p).EntireRow.Delete
'.Cells(i, p).Interior.ColorIndex = 3
End If
Next i
End With`
答案 0 :(得分:0)
此行无法正常运行.Cells(i, p).Value = Format(.Cells(i, p).Value, "Percent")
我在Excel中玩过它,直到找到有用的东西。见下文。
Sub test()
With ThisWorkbook.Sheets(1)
hcRow = Cells(Rows.Count, "A").End(xlUp).Row
p = 5
For i = hcRow To 2 Step -1 'Changed to take advantage of your detection of the last row, and stops before your title row
Cells(i, p) = Replace(Cells(i, p), "%", "") 'Removes the percent sign
Cells(i, p).NumberFormat = "0.00%" 'Reformats the cell (at this point, Excel still sees it formatted as text)
Cells(i, p) = Cells(i, p) & "%" 'Re-addition of the percent sign to the end triggers Excel to see it as a percent
If .Cells(i, p).Value < 0.01 Then 'Now your code works as desired
.Cells(i, p).EntireRow.Delete
End If
Next i
End With
End Sub
我不能假装理解为什么会这样。它一定与Excel如何理解数字有关。
截屏之前:
截图后: