【问题标题】:VBA Excel cellvalue is "not" NULLVBA Excel 单元格值为“非”NULL
【发布时间】:2016-04-04 21:26:36
【问题描述】:

我在这样的 for 循环中获取单元格值:

cellValue = rng.Cells(i, j).Value

然后我检查单元格的值,如果它是空的我需要跳过,像这样:

    If (Not IsEmpty(cellValue)) 
       /doThings/

即使单元格值为空,IsEmpty(cellValue) 条件也会变为假。我加了一块手表,想看看这些牢房里有什么,但我什么也没得到:

在这些单元格中,我有一个公式,如果某些条件不成立,它会返回“”,这就是单元格变为空的原因。

【问题讨论】:

  • 它的值不是Null,它可能是一个空字符串。 If cellValue <> ""
  • IsEmpty 仅适用于 Variant。你是怎么定义cellValue的?

标签: vba excel


【解决方案1】:

检查单元格是否为空的最佳方法是使用此

If Len(Trim(cellValue)) <> 0 Then

【讨论】:

    【解决方案2】:

    也许可以试试这样的:

    If Not IsEmpty(CellValue) OR CellValue <> "" 
    

    【讨论】:

      【解决方案3】:
       If Not IsEmpty(rng.Cells(i, j)) then
      

      应该工作!

      【讨论】:

        【解决方案4】:

        也可以尝试添加“”

        cellValue = rng.Cells(i, j).Value
        If (Not IsEmpty(cellValue)) Or cellValue <> "" Then
           'Do stuff
        End If
        

        【讨论】:

          【解决方案5】:

          应该是:

          if cellValue.value = "" then
             'do stuff
          end if
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2015-07-10
            • 2016-11-04
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多