检查所有组合框是否都是空的

时间:2018-01-16 12:59:11

标签: excel vba excel-vba

我有一份表格,我需要提供一些信息来安排医疗预约。

在我注册约会之前,我希望代码检查是否有任何文本框和组合框是空的,如果是,则显示MsgBox告诉它是空的并重新填写表单(不会丢失所有其他非空的组合框/文本框。)

我的代码:

Private Sub btCadastra_Click()
    Sheets("AGENDA").Select

    lastrow1 = (Range("A" & Rows.Count).End(xlUp).Row)
    Range("A" & lastrow1).Select
    ActiveCell.Offset(1, 0).Select

    Dim data_cad As Date

    id = lastrow1

    ActiveCell = id
    ActiveCell.Offset(0, 1).Select
    ActiveCell = cmbProfissional
    ActiveCell.Offset(0, 3).Select
    ActiveCell = txtData
    ActiveCell.Offset(0, 1).Select
    ActiveCell = txthorario
    ActiveCell.Offset(0, 1).Select
    ActiveCell = cmbDescricao
    ActiveCell.Offset(0, 1).Select
    ActiveCell = cmbNomePaciente
    ActiveCell.Offset(0, 1).Select
    ActiveCell = cmbStatus
    ActiveCell.Offset(0, 1).Select
    ActiveCell = Format(Date, "dd/mm/yyyy") & " - " & Time

    MsgBox "Dados cadastrados com sucesso", vbExclamation

    Unload Me
    frmAgenda.Show
End Sub

2 个答案:

答案 0 :(得分:0)

If cmbNomePaciente.ListRows >0 And cmbDescricao.ListRows >0 And cmbNomePaciente.ListRows >0  then
    msgbox "No Box is Empty"
Else
    msgbox "One of the Box is Empty"

Endif

答案 1 :(得分:0)

你需要这样的东西:

Private Sub btCadastra_Click()
    Dim c As Control, lastRow As Long, data_cad As Date, rng As Range

    'This loop checks for empty fields, exits sub if found (i.e. return to userform)
    For Each c In Me.Controls
        If TypeName(c) = "TextBox" Or TypeName(c) = "ComboBox" Then
            If c.Value = "" Then
                Exit Sub
            End If
        End If
    Next c

    'I've tried to tidy your code to write the fields to your s/sheet

    With Worksheets("AGENDA")
       lastRow = .Range("A" & .Rows.Count).End(xlUp).Row
       Set rng = .Range("A" & lastrow).Offset(1, 0)

       rng.Offset(0, 1) = cmbProfissional
       rng.Offset(0, 4) = txtData
       rng.Offset(0, 5) = cmbDescricao
       rng.Offset(0, 6) = cmbNomePaciente
       rng.Offset(0, 7) = cmbStatus
       rng.Offset(0, 8) = cmbStatus
       rng.Offset(0, 9) = Format(Date, "dd/mm/yyyy") & " - " & Time
    End With

    MsgBox "Dados cadastrados com sucesso", vbExclamation
    Unload Me
End Sub
相关问题