VBA combobox additem and getting a runtime error 7

2019-09-18 04:29发布

I have created a userform that has two buttons on it. One is called CmdCon6 and the other is called CmdLbs6. When clicked, they are suppose to close the current userform, pull up another userform, and pull values from the 4th column in sheet18 and add them to a combobox named x48 (both of the new userforms have a combobox named x48)in the new userform. The range of cell values to be added to the combobox x48 will flucuate, but never exceed 20 (hence why I added a loop). Everything works great and does what it is suppose to do when I click the CmdCon6 button, but when I click the CmdLbs button, it gives me a run-time error '70' Permission denied and highlights the 20th line of code (line between the If and end if in the Sub CmdLbs_Click()).

I have tried to change the name of the combobox x48 in the frmInputLbs6 userform and keep it as x48 for the frmInputCon6 userform, but I still received the same error.

Any suggestions to fix this issue? I'm stumped, and can't think of a way around it. Thanks in advance!

Private Sub CmdCon6_Click()
    Unload Me

    For x = 1 To 20
        If Sheet18.Cells(x, 4).Value <> "" Then
            frmInputCon6.x48.AddItem Sheet18.Cells(x, 4)
        End If
    Next x

    frmInputCon6.Show  
End Sub

Private Sub CmdLbs6_Click()
    Unload Me

    For x = 1 To 20
        If Sheet18.Cells(x, 4).Value <> "" Then
            frmInputLbs6.x48.AddItem Sheet18.Cells(x, 4)
        End If
    Next x

    frmInputLbs6.Show  
End Sub

1条回答
干净又极端
2楼-- · 2019-09-18 05:02

Controls on UserForms are private by default. You need to access them through the Controls collection:

Private Sub CmdLbs6_Click()
    Unload Me

    For x = 1 To 20
        If Sheet18.Cells(x, 4).Value <> "" Then
            frmInputLbs6.Controls("x48").AddItem Sheet18.Cells(x, 4)
        End If
    Next x

    frmInputLbs6.Show
End Sub

I'd also note that although you mention that "they are suppose to close the current userform", this isn't what happens. Your forms also aren't actually being fully unloaded until the other form is closed. The .Show method defaults to modal so in the code above frmInputCon6 doesn't fully unload until after frmInputLbs6 is closed.

Just something to keep in mind, because it really messes up your event stack. You can see the results by with this simple test code. Add UserForm1 and UserForm2, and put a button on each of them and the following code:

UserForm1:

Private Sub CommandButton1_Click()
    Unload Me
    UserForm2.Show
End Sub     '<--Put a breakpoint here.

Private Sub UserForm_Terminate()
    Debug.Print "UserForm1 closed"
End Sub

UserForm2:

Private Sub CommandButton1_Click()
    Unload Me
    UserForm1.Show
End Sub     '<--Put a breakpoint here.

Private Sub UserForm_Terminate()
    Debug.Print "UserForm2 closed"
End Sub

Put a breakpoint on the End Subs of each Click() event, fire up one of the forms and hit the buttons to hop back and forth a few times. Then close one of the forms and count how many times you hit the breakpoints before you actually exit.

查看更多
登录 后发表回答