簡體   English   中英

如果表列中沒有數據則顯示消息框的代碼

[英]Code to show message box if there is no data in table column

我有一個帶有 2 個組合框的用戶表單。

兩個組合框都是從工作表中的表格填充的。

這是我的代碼。

Private Sub UserForm_Initialize()
    Dim ws As Worksheet, tbl As ListObject, rng As Range

    Set ws = Sheets("Summary of Accounts")
    Set tbl = ws.ListObjects("groupheads")
    Set rng = tbl.ListColumns(3).DataBodyRange

    Me.cmb1.Clear
    Me.cmb2.Clear

    With cmb1
        .AddItem "Debit"
        .AddItem "Credit"
    End With


    "If rng = vbNullString Then"
        MsgBox ("Please create the Group Head Account First")
    Exit Sub

    End If

End Sub

錯誤發生在引用行,因為未定義對象變量。 我想要的是,如果表列中沒有數據,則應出現我在上面的代碼中顯示的 msg 框。

如果 ListObject 表的.DataBodyRange 屬性的第一行中沒有數據,則為其設置范圍類型變量將導致 Nothing。

If rng Is Nothing Then
    MsgBox ("Please create the Group Head Account First")
    Exit Sub
End If

是的,看起來那里有一個空行,但 .DataBodyRange 實際上什么都沒有,直到它在其中一個單元格中獲得至少一個值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM