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

时间:2019-01-29 01:35:40

标签: excel vba userform

您好,因此我在这里有一个代码,该代码允许我使用用户表单在电子表格中添加/插入新行,但是它也允许我添加空白/空行。有谁知道如何禁用它?我没有编码知识,所以我以教程为基础,但似乎找不到。

代码如下:

filter

我是否可以再有一个MsgBox,说“您不能添加空行”。 ?

2 个答案:

答案 0 :(得分:2)

请尝试以下操作,以防万一您更改工作表名称。

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

答案 1 :(得分:1)

Private Sub cmbAdd_Click()

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


'....Rest of code goes here


End Sub