Macro problem

Noz2k

Well-known Member
Joined
Mar 15, 2011
Messages
693
I have 2 command buttons, 1 which opens a userform and enables the user to scroll through the records and amend them, and another which opens the userform but only allows the user to make a new entry.

This is done by enabling/disabling a combobox.

The problem I am having is that I have to seemingly run the macro twice for it to take effect. Any ideas why this might be?

Here are the 2 codes

Editable
Code:
Sub ViewForm()
Dim pword As String
pword = InputBox("Please enter the password:")
If pword <> "mypassword" Then
MsgBox "Incorrect password"
Else
 
UserForm1.Show
 
End If
End Sub


Create New only
Code:
Sub NewForm()
UserForm1.Show
UserForm1.cboAccidentID.Enabled = False
End Sub
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Hi Noz,

Which macro do you have to run twice? Both of them?

Have you tried Enabling the combobox in the ViewForm Sub?
 
Upvote 0
Yes both of them.

I have, but the only difference it makes is that when I then close the form it generates an error on that line "cboAccidentID.Enabled = True"

"Run-time error '424':

Object Required"

Edit: Apologies that was because the line should have been UserForm1.cboAccidentID.Enabled = True

So no error now, but still have to run the macros twice
 
Last edited:
Upvote 0
So I had a couple of Me.EnableEvents in my Userform_Initialize which didn't need to be there (I'd left them from something I tried earlier).

So now it works, but strangely the wrong way around where

.Enabled = False, it seems to be enabled

and where

.Enabled = True, it seems to be disabled
 
Upvote 0
Ahh I see now. Swap the cbo enabled line with the userform.show line:

Code:
    Dim pword As String
        pword = InputBox("Please enter the password:")
    If pword <> "a" Then
        MsgBox "Incorrect password"
    Else
        UserForm1.cboAccidentID.Enabled = True
        UserForm1.Show
    End If

Code:
    UserForm1.cboAccidentID.Enabled = False
    UserForm1.Show
 
Upvote 0

Forum statistics

Threads
1,224,508
Messages
6,179,189
Members
452,893
Latest member
denay

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top