UserForm Initialise

AlexanderBB

Well-known Member
Joined
Jul 1, 2009
Messages
2,097
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.
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
In your userform, up, only :
Public dat as range

and in your doubleclick event

Code:
with myform 
set .dat = Range("A" & Target.Row & ":F" & Target.Row ) 
.Show 
end with
 
Upvote 0
Stepping through the code, as soon as reaching with myform the next thing that
executes is Private Sub UserForm_Initialize() which expects Dat to be Set. Thus repeating error "Run-time error '91':
Object variable or With block variable not set"
 
Upvote 0
You must do it in the activate event

In DoubleClick event
Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    Cancel = True
    With UserForm1
        Set .dat = Range("A" & Target.Row & ":F" & Target.Row)
        .Show
    End With
End Sub


In userform
Code:
[COLOR=#0000ff]Public dat As Range[/COLOR]


Private Sub [COLOR=#0000ff]UserForm_Activate[/COLOR]()
    MsgBox dat.Address
End Sub
 
Upvote 0
No, that does not work. Line 3

Code:
With UserForm1

executes UserForm_Initialize() which goes to error as the Range hasn't been set yet.
Anyway I can probably work out some way of doing this; my question was to see what might be the right way.
 
Upvote 0
Try this.

Worksheet module.
Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Dim frm As UserForm1

    Cancel = True
    
    Set frm = New UserForm1
    
    Set frm.Dat = Me.Range("A" & Target.Row & ":F" & Target.Row)
    
    frm.Show
    
End Sub
Userform module
Code:
Option Explicit

Private GetRow As Range

Property Set Dat(ByRef rng As Range)
    Set GetRow = rng
End Property

Private Sub UserForm_Activate()
    MsgBox GetRow.Address
End Sub
 
Upvote 0
But you must remove your initialize event, since that event is executed before loading the variables
 
Upvote 0
I need the initialise event. Norie, your "Set frm.Dat..." also goes to UserForm_Initialize().
 
Upvote 0
What do you need the Initialize event for?
 
Upvote 0
You can use the initialize event, but do not occupy the dat variable there.

Code:
Public dat As Range


Private Sub UserForm_Activate()
MsgBox dat.Address


End Sub


Private Sub UserForm_Initialize()
'your code


End Sub
 
Upvote 0

Forum statistics

Threads
1,224,819
Messages
6,181,153
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