Проверка кнопок настроек в кадре

0
задан Pᴇʜ 6 March 2019 в 09:11
поделиться

1 ответ

Добавьте этот код в свою пользовательскую форму и найдите '< < < Настройте раздел >>>

Также проверьте комментарии внутри кода.

Option Explicit

Function ValidateOptions() As Boolean

    ' Declare objects
    Dim ctrl As Control

    ' Declare other variables
    Dim oneOptionSelected As Boolean

    ' Loop through each of the forms' controls
    For Each ctrl In Me.Controls
        ' If the control is an option button
        If TypeOf ctrl Is MSForms.OptionButton Then
            ' If it's parent is an specific frame
            If ctrl.Parent.Name = "Frame1" Then

                oneOptionSelected = ctrl.Value
                ' If any of the option buttons is selected
                If oneOptionSelected = True Then Exit For
            End If
        End If
    Next ctrl

    ' If at least one of the option buttons was checked
    If oneOptionSelected = True Then
        ValidateOptions = True
    Else
        MsgBox "No selection for option buttons in Frame1"
    End If

End Function

Private Sub CommandButton1_Click()

    ' Declare objects
    Dim targetSheet As Worksheet

    ' Declare other variables
    Dim targetSheetName As String
    Dim emptyRow As Long
    Dim cancel As Boolean

    ' <<< Customize this >>>
    targetSheetName = "Sheet1"

    ' Validate options
    If ValidateOptions = False Then Exit Sub

    ' Initialize objects
    Set targetSheet = ThisWorkbook.Worksheets(targetSheetName)

    ' Determine emptyRow
    emptyRow = WorksheetFunction.CountA(targetSheet.Range("A:A")) + 1 ' This code is not reliable. Look for solutions on how to find next empty row here in SO

    If Me.OptionButton1.Value = True Then
        targetSheet.Cells(emptyRow, 1).Value = "Accepted"
    Else
        targetSheet.Cells(emptyRow, 2).Value = "Rejected"
    End If

End Sub
0
ответ дан Ricardo Diaz 6 March 2019 в 09:11
поделиться
Другие вопросы по тегам:

Похожие вопросы: