Excel userform:如何在接受数据之前检查某些单元格?

时间:2017-09-13 14:13:11

标签: excel vba userform

Private Sub CancelCommandButton_Click()
    Unload Me
End Sub

Private Sub ClearCommandButton_Click()
    Call UserForm_Initialize
End Sub

Private Sub OkCommandButton_Click()
    Dim emptyRow As Long

    Sheet1.Activate

    emptyRow = WorksheetFunction.CountA(Range("A:A")) + 1

    Cells(emptyRow, 1).Value = EntityNameTextBox.Value
    Cells(emptyRow, 2).Value = JurComboBox.Value
    Cells(emptyRow, 3).Value = MailTypeComboBox.Value
End Sub

Private Sub UserForm_Initialize()
    EntityNameTextBox.Value = ""

    JurComboBox.Clear

    With JurComboBox
        .AddItem "AL"
        .AddItem "AK"
        .AddItem "AR"
        .AddItem "AZ"
        .AddItem "CA"
        .AddItem "CO"
        .AddItem "CT"
        .AddItem "DE"
        .AddItem "HI"
        .AddItem "IA"
    End With

    MailTypeComboBox.Clear

    With MailTypeComboBox
        .AddItem "AR"
        .AddItem "ARR"
        .AddItem "DOR"
        .AddItem "DTN"
        .AddItem "FAR"
    End With

    EntityNameTextBox.SetFocus
End Sub

1 个答案:

答案 0 :(得分:0)

请使用此代码:

Private Sub OkCommandButton_Click()
    Dim emptyRow As Long
    Dim MsgString as String

    Sheet1.Activate

    emptyRow = WorksheetFunction.CountA(Range("A:A")) + 1

    MsgString = "Do you want to update data?" & vbNewLine & "A" & emptyRow & " = " & EntityNameTextBox.Value & vbNewLine & "B" & emptyRow & " = " & JurComboBox.Value & vbNewLine & "C" & emptyRow & " = " & MailTypeComboBox.Value
    If MsgBox(MsgString, vbYesNo, "Update confirmation...!!!") = vbYes Then
        Cells(emptyRow, 1).Value = EntityNameTextBox.Value
        Cells(emptyRow, 2).Value = JurComboBox.Value
        Cells(emptyRow, 3).Value = MailTypeComboBox.Value
    Else
        Msgbox "Data Update failed.", vbExclamation, "Failed update data."
    End If
End Sub

希望得到这个帮助。