是否可以使用 Excel 中的用户表单禁用在电子表格中添加空行?

Is it possible to disable adding of empty row in spreadsheet using a Userform in Excel?

你好,我这里有一个代码,允许我使用用户表单 add/insert 在电子表格中添加一个新行,但是它也允许我添加 BLANK/EMPTY 行。有谁知道如何禁用它?我没有编码方面的知识,所以我以教程为基础,但我似乎找不到。

代码如下:

Private Sub cmbAdd_Click()
    Dim sheet As Worksheet
    Set sheet = ThisWorkbook.Sheets("TRY TRY")

    nextrow = sheet.Cells(Rows.Count, 1).End(xlUp).Row + 1

    sheet.Cells(nextrow, 1) = Me.cmbSchema
    sheet.Cells(nextrow, 2) = Me.cmbEnvironment
    sheet.Cells(nextrow, 3) = Me.cmbHost
    sheet.Cells(nextrow, 4) = Me.cmbIP
    sheet.Cells(nextrow, 5) = Me.cmbAccessible
    sheet.Cells(nextrow, 6) = Me.cmbLast
    sheet.Cells(nextrow, 7) = Me.cmbConfirmation
    sheet.Cells(nextrow, 8) = Me.cmbProjects

    MsgBox "Data Added!"

End Sub

我可以再有一个 MsgBox 说 "You cannot add empty rows." 吗?

Private Sub cmbAdd_Click()

If IsEmpty(Me.cmbSchema) Then
    MsgBox "cmbSchema is blank!"
    Exit Sub
End If


'....Rest of code goes here


End Sub

尝试以下操作,更改 sheet 名称以防您的 sheet。

Private Sub CommandButton1_Click()
    Dim sht As Worksheet
    Set sht = ThisWorkbook.Sheets("TRY_TRY")

    nextrow = sht.Cells(Rows.Count, 1).End(xlUp).Row + 1

    If Me.cmbSchema = "" Or _
        Me.cmbEnvironment = "" Or _
        Me.cmbHost = "" Or _
        Me.cmbIP = "" Or _
        Me.cmbAccessible = "" Or _
        Me.cmbLast = "" Or _
        Me.cmbConfirmation = "" Or _
        Me.cmbProjects = "" Then

        MsgBox "Empty value is not allowed", vbCritical, "Data Missing"

    Else

        sht.Cells(nextrow, 1) = Me.cmbSchema
        sht.Cells(nextrow, 2) = Me.cmbEnvironment
        sht.Cells(nextrow, 3) = Me.cmbHost
        sht.Cells(nextrow, 4) = Me.cmbIP
        sht.Cells(nextrow, 5) = Me.cmbAccessible
        sht.Cells(nextrow, 6) = Me.cmbLast
        sht.Cells(nextrow, 7) = Me.cmbConfirmation
        sht.Cells(nextrow, 8) = Me.cmbProjects

        MsgBox "Data Added!"

    End If
End Sub