UserForm Initialise

AlexanderBB

Well-known Member
Joined
Jul 1, 2009
Messages
2,099
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
I'm attempting to Load a UserForm from a Worksheet Double Click
I also want to pass a worksheet Range to the Form so using:

Code:
Public Property Let Dat (inVal as Range)
Set MyRow= inVal
End Property

When this is called from the worksheet BeforeDoubleClick
Code:
myForm.Dat = Me.Range("A" & Target.Row & ":F" & Target.Row)

the Userform Initialise event run. But it uses the Range not yet Set.
So a boolean Exits the Initialise event and the Property Let executes to Set MyRow.

But after that a Load (or Show) UserForm skips the initialise event, which is now wanted.
I can think of some hacks to maybe get around this, but what the correct way to do it please?

Thanks for any help.
 
It's a pity I can't upload a file here as both you you would get a better idea of what's going on.
I need the initialise code and the range to be available there, because that's how I've written it.

Have either of you struck this scenario? You set controls up on a Form and if you step through code everything is perfect.
But if you run it without stepping, a listbox does not keep the correct width and height. And I can prove it happening every time.

I can't use Activate because this Form may call another Form... and if you do stuff on that form, then come back to This Form to Close it, it's activate
event runs and width and height are mucked up. It's unbelievably frustrating...
 
Upvote 0

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
The double click event is activated only with one cell, then in the active cell you have the row.
Let's not complicate, use the following:

Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    Cancel = True
    myform.Show
End Sub

Code:
[COLOR=#0000ff]'Up in the general statements[/COLOR]
Dim dat As Range

[COLOR=#0000ff]'Code[/COLOR]
Private Sub UserForm_Activate()
    MsgBox dat.Address
End Sub

[B][COLOR=#000000]'You can use initialize with your range, but pointing to the active cell not to the Target object.[/COLOR][/B]
[COLOR=#0000ff]Private Sub UserForm_Initialize[/COLOR]()
    [B]Set dat =[/B] Range("A" & ActiveCell.Row & ":F" & ActiveCell.Row)
End Sub
 
Upvote 0
Ah, thanks Dante, that was a great tip and has solved one of the issues.
I need to repeatedly call the Form from the worksheet so I added

Code:
                NoInit = True: Unload frmSM2: NoInit = False
                frmSM2.Show

Then first line in UserForm_Initialize
Code:
If NoInit = True Then Exit Sub

That seem okay to you ?

But the remaining issue is driving me batty. I have a listbox control which is populated in UserForm_Initialize
and (if I step through the code) all is 100%/ But if I run the code the listbox loses it's height and width.

If I debug.print it's height and width just before Ending UserForm_Initialize, they're correct.
But once finished they're wrong. Printing W & H in the debug.window also shows the wrong values.

So my question is, why are they OK if I step through the code, but not if I run it; and what changes them anyway ?
 
Upvote 0
You can put all your code to understand what you're doing
 
Upvote 0
You could upload a copy of your file to a free site such as www.box.com or www.dropbox.com. Once you do that, mark it for 'Sharing' and you will be given a link to the file that you can post here. If the workbook contains confidential information, you could replace it with generic data.
 
Upvote 0
Hi Dante

https://www.dropbox.com/s/66jfmmz1mdo5kym/Book4.zip?dl=0

I have set up a test condition to show the problem. BTW I don't need it fixed (so much) as just to understand why it's doing this.

If you double click in column Prefix it will stop in UserForm_Initialize. I then type 'frmSMM2.show' in the debug window and all is OK.
If you run or step from that Stop all is OK. Stepping shows there is no more code anyway.

But if you remove the Stop, and run again, the ListBox width and height are never right.

Thanks for looking at this !
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,176
Members
453,021
Latest member
Justyna P

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