简体   繁体   中英

Using Option Buttons on a UserForm to place text in a range from the sub calling the UserForm

I apologize if this is posted elsewhere, but I am having issues understanding the relationship between the sub calling the user form and the user form controls. I have a sub that will populate data into a worksheet from another worksheet. One of the cells that needs to be filled out is an explanation for the change in quantities of the project. I generated a user form with option buttons that the user can select the appropriate "reason" from. Once the OK button is clicked it will place the selected reason in the cell of the worksheet.

In the sub, I am using For Each cell in the range to populate the data across the row for each value above a specific criteria and it should show the form for each cell meeting the criteria. Can I pass the cell I am using as a row reference into the user form so that it can use that cell as an offset to enter in the chosen "reason" then unload the form?

Private Sub okbutton1_Click(bc As Range)  'bc should be the range from the sub calling this form

Select Case True
Case OptionButton1
    If bc.Offset(0, 3).Value = "A" Then
        Set bc.Offset(0, 6).Value = "Actual amount required is more than plan quantity."
    Else
        Set bc.Offset(0, 6).Value = "Actual amount required is less than plan quantity."
    End If
Case OptionButton2
        Set bc.Offset(0, 6).Value = "This items was not constructed/used/required."
Case OptionButton3
        Set bc.Offset(0, 6).Value = "Only a portion of the contingency was required for items not in original plan."
Case OptionButton4
        Set bc.Offset(0, 6).Value = "Deficiency levied against Contractor per IDOT Section 105.03."
Case OptionButton5
        Set bc.Offset(0, 6).Value = "Damages levied against Contractor per IDOT Section 108.09."
Case OptionButton6
        Set bc.Offset(0, 6).Value = InputBox("Please enter your reasoning below.", "Other")
End Select
Unload AuthReason2

End Sub

Then this is a portion of the sub that I am working with to populate the worksheet.

Line5:  'Populates the BLR13210A from the data entered on the BLR13210

Application.ScreenUpdating = False
Dim bws As Worksheet
    Set bws = Worksheets("BLR 13210A")
Dim Arange As Range
    Set Arange = aws.Range("AZ34:AZ198")
Dim Bcell As Range  ' First cell in AttachA form
    Set Bcell = bws.Range("B11")

For Each ACell In Arange
    If ACell.Value > 1999.99 Then
        Bcell.Value = ACell.Offset(0, -47).Value
        Bcell.Offset(0, 1).Value = ACell.Value
        Bcell.Offset(0, 2).Value = ACell.Offset(0, -37).Value
        Bcell.Offset(0, 3).Value = ACell.Offset(0, -22).Value
        AuthReason2(Bcell).Show
    End If
    Bcell = Bcell.Offset(1, 0)

Application.ScreenUpdating = True

Thank you in advance for your assistance.

User forms should be used to retrieve user inputs and pass them to processing sub that will treat data accordingly

so you'd better act the opposite way, ie:

  • your main sub

    should "launch" the user form and retrieve data from it, treat those data to act on other data and then close the user form

    it could be like follows:

     Option Explicit Sub main() Dim bws As Worksheet Set bws = Worksheets("BLR 13210A") Dim Bcell As Range ' First cell in AttachA form Set Bcell = bws.Range("B11") With UserForm1 '<--| change "UserForm1" to your actual userform name .Tag = Bcell.Offset(0, 3).Value '<--| store the value you want to share with Userform in its 'Tag' property .Show '<-- show the userform and have it process user inputs Bcell.Offset(0, 6).Value = .Tag '<--| retrieve the value that userform has left in its 'Tag' property accordingly to user inputs End With Unload UserForm1 '<--| change "UserForm1" to your actual userform name End Sub

    of course you will change Bcell.Offset(0, 3).Value to whatever range value should fit your needs

  • your user form

    will handle user inputs and pass them back to the sub

    there are many ways it can be done, but the following could suit your needs

    put in its code pane something like follows:

     Option Explicit Private Sub okbutton1_Click() Dim txt As String With Me '<--| reference the userform Select Case True Case .OptionButton1 '<--| with the dot (.) access the referenced object members (in this case its controls) If .Tag = "A" Then '<--| query the value that the calling sub has left in the useform 'Tag' property txt = "Actual amount required is more than plan quantity." Else txt = "Actual amount required is less than plan quantity." End If Case .OptionButton2 txt = "This items was not constructed/used/required." Case .OptionButton3 txt = "Only a portion of the contingency was required for items not in original plan." Case .OptionButton4 txt = "Deficiency levied against Contractor per IDOT Section 105.03." Case .OptionButton5 txt = "Damages levied against Contractor per IDOT Section 108.09." Case .OptionButton6 txt = InputBox("Please enter your reasoning below.", "Other") Case Else '<--| you may want to handle the case when the user dosen't select any option txt = "some text" '<--| change it to your needs End Select .Tag = txt '<--| use 'Tag' property to store the value you want to pass back to the calling sub .Hide '<--| hide the userform End With End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM