简体   繁体   English

如何确定单元格值是否为 0%?

[英]How to find if cell value is 0%?

I want to compare percentage in cells.我想比较单元格中的百分比。

dim ws as worksheet
dim LastRow as long

For Each ws In ActiveWorkbook.Worksheets
    If ws.Name <> "test1" And ws.Name <> "test2" Then
        LastRow = ws.Cells(Rows.Count, "A").End(xlUp).Row
        
        For i = 3 To LastRow
            If ws.Range("M" & i).Value = 0 Then  '<---here it give me error
                ws.Range("M" & i).Locked = True
            End If
        Next i

    End If
Next ws

In Column M in my sheet, the whole column starting from M3 is percentage format type.在我的工作表的 M 列中,从 M3 开始的整列都是百分比格式类型。

I want to find out if the cell is 0%.我想知道单元格是否为 0%。

Let's say range("M5") is 0%, I want to check format protection to lock the cells and last protect the worksheet.假设 range("M5") 为 0%,我想检查格式保护以锁定单元格并最后保护工作表。

I have tried many code such as:我尝试了很多代码,例如:

ws.Range("M" & i).Value = 0
ws.Range("M" & i) = 0
ws.Range("M" & i).Value = "0"
ws.Range("M" & i) = "0%"

It gives type mismatch error.它给出了类型不匹配错误。

Try to add some error checking on the cell first:尝试先在单元格上添加一些错误检查:

For i = 3 To LastRow
    If Not IsError(ws.Range("M" & i)) Then
        If ws.Range("M" & i).Value = 0 Then
            ws.Range("M" & i).Locked = True
        End If
    End If
Next i

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM