检查UserForm中的电子表格控件是否为空或者所有单元格是否为空

时间:2018-02-08 19:37:27

标签: excel-vba vba excel

在UserForm中放置一个SpreadSheet控件和一个ComboBox。我想检查此电子表格控件中的所有单元格是否为空。我已经尝试了几种可能和我可以找到的变体,但都没有效果。这是最后一次尝试。没有生成错误,但没有任何反应。

Sub check_if_empty()

Dim rngSource As Object 
Dim TEST1 As String

TEST1 = CmbBox1.text

Set rngSource = Spreadsheet1.Sheets(TEST1).Range("A2:A2001")

If rngSource Is Nothing Then

     MsgBox "This spreadsheet control is empty"

  Else

     MsgBox "There is at least one value"

end if
end sub

1 个答案:

答案 0 :(得分:0)

您可以使用WorksheetFunction作为测试对照范围运行CountA()

Sub check_if_empty()    
    Dim rngSource As Range 'range is a range
    Dim TEST1 As String

    TEST1 = CmbBox1.text

    'May need to add Spreadsheet1 workbook(?) object back in here
    Set rngSource = Sheets(TEST1).Range("A2:A2001")

    If Application.WorksheetFunction.CountA(rngSource) = 0 Then
         MsgBox "This spreadsheet control is empty"
    Else
         MsgBox "There is at least one value"
    End If
End Sub