检查范围内的任何单元格是否有值

Check If Any Cell In A Range Has A Value

我无法确认指定范围内的任何单元格是否包含任何值。希望有人能帮我解决语法问题。

提前致谢

        For i = 1 To DataRange.Rows.Count
            CheckCells = If(Qty > 0 And WS1.Range("Sheet1!A" & i & ":Sheet1!Z" & i).Value <> "", "HasValue", "NoValue")
        Next i

您可以遍历单元格以检查每个单元格:

Dim r = xl.Range("A1", "D4")
Dim nCells = r.Cells.Count
Dim isAllBlank = True

For i = 1 To nCells
    If DirectCast(r.Cells(i), Excel.Range).Value IsNot Nothing Then
        isAllBlank = False
        Exit For
    End If
Next

checkCells = If(isAllBlank, "NoValue", "HasValue")

其中 xl.Range 是您需要检查的范围。

我尝试使用 range.SpecialCells(Excel.XlCellType.xlCellTypeBlanks),但它是 bit moody about it

我能够通过使用以下代码实现我想要的。

                For i = 1 To DataRange.Rows.Count
                    If xlApp.WorksheetFunction.CountA(WS.Range("Parts!Z" & R & ":Parts!AI" & R)) > 0 Then
                        CheckCells = "HasValue"
                    Else
                        CheckCells = "NoValue"
                    End If
                Next i