Userform.Show on a form button will not recognize

2019-08-07 03:11发布

I know very little about VBA, but I'm trying to design a userform for an excel workbook. The idea is click the button, bring up the userform, enter info, hit OK, and your info is formatted correctly and inserted into the worksheet.

I have 3 userforms that all work fine, but any macro I create that references one of them just does not recognize that that particular userform exists.

The code I'm having a problem with is pretty straightforward:

Private Sub LiquidFormButton_Click()

LiquidEntryUserform.Show

End Sub

Edit (Update): So I tried making a new userform, with a different name. I copied and pasted all of the controls from the object over to the new userform, changed the name of the macro to bring up the userform, and voila, it works. However, now the userform itself doesn't do anything because none of the controls actually have any codes behind them telling them what to do. That's fine, I'll just copy over the codes from the broken form and BOOM now it doesn't work. Soooo something in the very very simple coding within the userform itself is preventing it from being shown, even though the new userform AND the broken one both, in fact, do everything else they need to do besides show up. I'll post the full userform code up later on after some dabbling. Thank you!

1条回答
Summer. ? 凉城
2楼-- · 2019-08-07 04:10

You should 'instantiate' the form like so

Private Sub LiquidFormButton_Click()

    Dim liquid as LiquidEntryUserform ' define a liquid var of the correct type
    Set liquid = new LiquidEntryUserform ' create the Form
    liquid.Show  'show it

    ' here you can still access variables 
    ' on the form
    If liquid.TextBox1.Text = "700" Then 
       'do things
    End if

End Sub

My project looks like this:

enter image description here

You can use the Object Browser (View|Object Browser or hit F2) to find the Forms and Classes you have in your project:

enter image description here

查看更多
登录 后发表回答