связывать события для динамически создаваемых элементов управления в фреймах в пользовательской форме vba - PullRequest
0 голосов
/ 09 апреля 2020

Я новичок здесь, и я борюсь с VBA, особенно с пользовательской формой.

Я хочу создать пользовательскую форму, которая позволяет проверять, присутствует ли каждый в списке людей.

Список людей может меняться каждый день, поэтому я решил создать динамическую c пользовательскую форму. Я прошу Excel набрать go количество людей в моем списке, а затем я создаю пользовательскую форму со всеми именами этих людей и 2 опциональными кнопками в каждой строке "здесь" или "отсутствует".

Моя проблема это: мои дополнительные кнопки были созданы в FOR l oop, поэтому они имеют следующие имена: «здесь» & i или «отсутствует» & i (с i от 1 до числа людей в моем списке)

Теперь я могу sh Я мог бы создать код, когда вы нажмете эти кнопки, чтобы обновить лист Excel. Что-то вроде «Private sub here & i_click () ....»

Но я не знаю, сколько у меня кнопок, и значение i больше не меняется, так как мы вышли из FOR l oop .

Может ли кто-нибудь помочь мне с этим? Спасибо

Вот мой код. Когда отображается пользовательская форма rosterfill, мне бы хотелось, чтобы различные кадры действовали следующим образом:

for i= 6 to lastrowd
If here & i is clicked then frame3 & i is visible
If absent & i is clicked then frame2 & i is visible
if none is clicked then msgbox ("select here/absent for everyone")
Next

Конечно, я понимаю, что это невозможно сделать с такого рода l oop, но я не могу найти ни одного примера в Интернете в моей ситуации ...

- VBA CODE ---

Workbooks.Open (pathroot & "\" & dpt & "\Roster")
Worksheets("Shift " & shi).Activate
lastrowd = Cells(Rows.Count, 1).End(xlUp).Row
topini = 18
initop = 18

For i = 6 To lastrowd
Set thelabel = rosterfill.Controls.Add("Forms.TextBox.1", "label" & i, True)
With thelabel
    .Value = Cells(i, 1).Value 'name of the person
    .Left = 6
    .Width = 234
    .Top = topini + 30
    .Locked = True
    .Height = 24
End With

Set theframe = rosterfill.Controls.Add("Forms.Frame.1", "frame" & i, True)
With theframe
    .Top = initop + 30
    .Width = 100
    .Left = 246
    .Height = 24

    Set here = .Controls.Add("forms.OptionButton.1", "here" & i, True)
        With here
        .Height = 18
        .Left = 5
        .Width = 108
        .Caption = "here"
        End With


    Set absent = .Controls.Add("forms.OptionButton.1", "absent" & i, True)
        With absent
        .Height = 18
        .Left = 50
        .Width = 108
        .Caption = "absent"
        End With

End With

Set theframe2 = rosterfill.Controls.Add("Forms.Frame.1", "frame2" & i, False)
With theframe2
    .Top = initop + 30
    .Width = 350
    .Left = 350
    .Height = 24

    Set abs1 = .Controls.Add("forms.OptionButton.1", "abs1" & i, True)
        With abs1
        .Height = 18
        .Left = 5
        .Width = 108
        .Caption = "PTO"
        End With

    Set abs2 = .Controls.Add("forms.OptionButton.1", "abs2" & i, True)
        With abs2
        .Height = 18
        .Left = 50
        .Width = 108
        .Caption = "UPTO"
        End With

    Set abs3 = .Controls.Add("forms.OptionButton.1", "abs3" & i, True)
        With abs3
        .Height = 18
        .Left = 95
        .Width = 108
        .Caption = "MED"
        End With

    Set abs4 = .Controls.Add("forms.OptionButton.1", "abs4" & i, True)
        With abs4
        .Height = 18
        .Left = 140
        .Width = 108
        .Caption = "ANCI"
        End With

    Set abs5 = .Controls.Add("forms.OptionButton.1", "abs5" & i, True)
        With abs5
        .Height = 18
        .Left = 185
        .Width = 108
        .Caption = "FMLA"
        End With

    Set abs6 = .Controls.Add("forms.OptionButton.1", "abs6" & i, True)
        With abs6
        .Height = 18
        .Left = 230
        .Width = 108
        .Caption = "AE"
        End With

    Set abs7 = .Controls.Add("forms.OptionButton.1", "abs7" & i, True)
        With abs7
        .Height = 18
        .Left = 260
        .Width = 108
        .Caption = "S"
        End With

    Set abs8 = .Controls.Add("forms.OptionButton.1", "abs8" & i, True)
        With abs8
        .Height = 18
        .Left = 285
        .Width = 108
        .Caption = "BRV"
        End With

    Set abs9 = .Controls.Add("forms.OptionButton.1", "abs9" & i, True)
        With abs9
        .Height = 18
        .Left = 320
        .Width = 108
        .Caption = "JD"
        End With

End With

Set hours = rosterfill.Controls.Add("forms.TextBox.1", "hours" & i, False)
    With hours
        .Left = 700
        .Width = 50
        .Top = topini + 30
        .Height = 24
    End With
Set Comment = rosterfill.Controls.Add("forms.TextBox.1", "comment" & i, False)
    With Comment
        .Left = 755
        .Width = 100
        .Top = topini + 30
        .Height = 24
    End With


Set theframe3 = rosterfill.Controls.Add("Forms.Frame.1", "frame3" & i, False)
With theframe3
    .Top = initop + 30
    .Width = 350
    .Left = 350
    .Height = 24

    Set here1 = .Controls.Add("forms.OptionButton.1", "here1" & i, True)
        With here1
        .Height = 18
        .Left = 5
        .Width = 108
        .Caption = "LE"
        End With

    Set here2 = .Controls.Add("forms.OptionButton.1", "here2" & i, True)
        With here2
        .Height = 18
        .Left = 50
        .Width = 108
        .Caption = "AL"
        End With

    Set here3 = .Controls.Add("forms.OptionButton.1", "here3" & i, True)
        With here3
        .Height = 18
        .Left = 95
        .Width = 108
        .Caption = "Entire Shift"
        End With
End With

topini = topini + 30
initop = initop + 30
Next i

If topini + 100 > 450 Then
rosterfill.Height = 450
rosterfill.CommandButton1.Top = topini + 100 - 60
rosterfill.CommandButton2.Top = topini + 100 - 60
rosterfill.ScrollBars = fmScrollBarsVertical
rosterfill.ScrollHeight = topini + 100
rosterfill.ScrollWidth = 50
rosterfill.ScrollTop = 0
Else
rosterfill.Height = topini + 100
rosterfill.CommandButton1.Top = topini + 100 - 60
rosterfill.CommandButton2.Top = topini + 100 - 60
End If
rosterfill.Show
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...