在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
答案 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