#1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 37
Default User Form

Hi,

I'm trying to call a userform from a macro, using:
UserForm1.show

When I run the macro, I get a Run Time Error 91 Object Variable or With
Block Variable not set error. Because the code returns to the UserForm1.show
statement in the macro rather than going on to the next step.

If I step through it, it proceeds back to the UserForm, and again stops at
the UserForm1.show statement.

I tried Load UserForm1 and had the same problem. How can I get it to
proceed to the next step in the original macro?

Thank you,

Brian
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 770
Default User Form

Brian,

In the VBE goto ToolsOptionsGeneral and choose "Break in Class Module."
The debugger will now show the line in the userform that's causing the
error.

hth,

Doug

"Brian C" wrote in message
...
Hi,

I'm trying to call a userform from a macro, using:
UserForm1.show

When I run the macro, I get a Run Time Error 91 Object Variable or With
Block Variable not set error. Because the code returns to the

UserForm1.show
statement in the macro rather than going on to the next step.

If I step through it, it proceeds back to the UserForm, and again stops at
the UserForm1.show statement.

I tried Load UserForm1 and had the same problem. How can I get it to
proceed to the next step in the original macro?

Thank you,

Brian



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default User Form


Brian,

Do you have any With statements in your macro? You will get the Ru
Time Error 91 sometimes if you have a With statement, but are missin
the End With statement. If thats not the case, copy the code you ar
using to show the userform, and I will look at it and see if I ca
figure out whats causing the problem for you.

Brian C Wrote:
Hi,

I'm trying to call a userform from a macro, using:
UserForm1.show

When I run the macro, I get a Run Time Error 91 Object Variable o
With
Block Variable not set error. Because the code returns to th
UserForm1.show
statement in the macro rather than going on to the next step.

If I step through it, it proceeds back to the UserForm, and again stop
at
the UserForm1.show statement.

I tried Load UserForm1 and had the same problem. How can I get it to
proceed to the next step in the original macro?

Thank you,

Bria


--
dok11
-----------------------------------------------------------------------
dok112's Profile: http://www.excelforum.com/member.php...fo&userid=1058
View this thread: http://www.excelforum.com/showthread.php?threadid=52938

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 37
Default User Form

Hi Dok112 and Doug,

I set the Break in Class and it stops at the same statement, where I
originally show the UserForm1 in the main macro. It looks like it's trying
to repeat that step and not proceeding to the next one.

I don't have any with statements in the userform, so it may have an issue
with the Object Variable part. NOTE: in the macro, the next step after the
Load UserForm1 command is a section named POPULATE:
Not sure if this matters.

Here's the UserForm code I have. Below I copied the section of the main
macro that loads the Userform, with the statements immediately preceding and
following it:

Option Explicit

Private Sub userform_initialize()

Dim AllCells As Range, Cell As Range
Dim myStart As Range
Dim destWB As Workbook
Dim searchltr As String, testltr As String
Dim sourceVal As String
Dim sourceWB As Workbook
Dim sourceRange As Range
Dim x As Long
Dim a As Long

Set sourceWB = Workbooks("CustomerData.xls")
Set destWB = Workbooks("ODonnell Sales Model17.xls")

Set sourceRange = destWB.Sheets("Customer Data").Range("b6")

If sourceRange.Value = "" Then
MsgBox "Please enter a Name"
sourceRange.Select
Exit Sub
End If

If Len(sourceRange.Value) = 1 Then searchltr = _
UCase(Left(sourceRange.Value, 1))


sourceWB.Activate
Set myStart = Range("D:D")
x = myStart.End(xlDown).Row - myStart.Row + 1



For a = 2 To x
Set AllCells = Range("d" & a)
Let testltr = UCase(Left(AllCells.Value, 1))
If searchltr = testltr Then
Me.ListBox1.AddItem AllCells.Value
End If
Next a

' Show the UserForm
UserForm1.Show
End Sub




Private Sub CANCEL_Click()
Unload Me

End Sub


Private Sub ListBox1_Click()
Dim AllCells As Range, Cell As Range
Dim myStart As Range
Dim destWB As Workbook
Dim searchltr As String, testltr As String
Dim sourceVal As String
Dim sourceWB As Workbook
Dim sourceRange As Range
Dim a As Long
Dim rng As Range
Dim rng1 As Range
Dim x As Long
Dim keyRange As Range

If Me.ListBox1.ListIndex = -1 Then Exit Sub
Set sourceWB = Workbooks("CustomerData.xls")
Set destWB = Workbooks("ODonnell Sales Model17.xls")

Set sourceRange = destWB.Sheets("Customer Data").Range("B6")


If Len(sourceRange.Value) = 1 Then searchltr = _
UCase(Left(sourceRange.Value, 1))


sourceWB.Activate
Set myStart = Range("D:D")
x = myStart.End(xlDown).Row - myStart.Row + 1


For a = 2 To x
Set AllCells = Range("D" & a)
If AllCells.Value = Me.ListBox1.Value Then
Set rng = AllCells
Exit For
End If
Next a

If Not rng Is Nothing Then
Application.Goto rng, True
Else

MsgBox "Not found"
End If

Set keyRange = destWB.Sheets("Sheet3").Range("A7")
ActiveCell.EntireRow.Copy _
Destination:=keyRange


Unload Me

End Sub
__________________________________________________ ____________
Code before and after loading userform:

With sourceWB.Sheets(1).Range("D:D")
Set rng = .Find(What:=sourceRange.Value, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValue, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)

End With

If Not rng Is Nothing Then
rng.EntireRow.Copy destrange
Msg = sourceRange.Value & "Loading."
MsgBox (Msg)
GoTo POPULATE
Else

Msg = sourceRange.Value & " Not Found. Would you like to lookup?" ' Define
message.
Style = vbYesNo ' Define buttons.
Title = "Customer Not Found" ' Define title.
Ctxt = 1000 ' Define topic
Response = MsgBox(Msg, Style, Title)
If Response = vbNo Then GoTo ender ' User chose No, so end.
End If

Load UserForm1

POPULATE:
Application.ScreenUpdating = False
destWB.Activate
Sheets("Sheet3").Visible = True
Sheets("Calculations").Visible = True
Sheets("Customer Data").Visible = True
Sheets("Calculations").Unprotect
Sheets("Customer Data").Unprotect



"dok112" wrote:


Brian,

Do you have any With statements in your macro? You will get the Run
Time Error 91 sometimes if you have a With statement, but are missing
the End With statement. If thats not the case, copy the code you are
using to show the userform, and I will look at it and see if I can
figure out whats causing the problem for you.

Brian C Wrote:
Hi,

I'm trying to call a userform from a macro, using:
UserForm1.show

When I run the macro, I get a Run Time Error 91 Object Variable or
With
Block Variable not set error. Because the code returns to the
UserForm1.show
statement in the macro rather than going on to the next step.

If I step through it, it proceeds back to the UserForm, and again stops
at
the UserForm1.show statement.

I tried Load UserForm1 and had the same problem. How can I get it to
proceed to the next step in the original macro?

Thank you,

Brian



--
dok112
------------------------------------------------------------------------
dok112's Profile: http://www.excelforum.com/member.php...o&userid=10581
View this thread: http://www.excelforum.com/showthread...hreadid=529388


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 770
Default User Form

Brian,

I should have been clearer. In Debug mode when you're at the Load
statement, use F8 to step through your code. That might, or might not, show
you an error in the Userform itself.

One other thought: instead of loading it in your main module, Show it there
instead. I think there may be some problem with the order of the events as
you have them as I got the same error with just the Load statement in a main
module and just the Show statement in the Initialize event. Showing it in
the main module should not affect your Initialize code, at least I don't
think so.

hth,

Doug

"Brian C" wrote in message
...
Hi Dok112 and Doug,

I set the Break in Class and it stops at the same statement, where I
originally show the UserForm1 in the main macro. It looks like it's

trying
to repeat that step and not proceeding to the next one.

I don't have any with statements in the userform, so it may have an issue
with the Object Variable part. NOTE: in the macro, the next step after

the
Load UserForm1 command is a section named POPULATE:
Not sure if this matters.

Here's the UserForm code I have. Below I copied the section of the main
macro that loads the Userform, with the statements immediately preceding

and
following it:

Option Explicit

Private Sub userform_initialize()

Dim AllCells As Range, Cell As Range
Dim myStart As Range
Dim destWB As Workbook
Dim searchltr As String, testltr As String
Dim sourceVal As String
Dim sourceWB As Workbook
Dim sourceRange As Range
Dim x As Long
Dim a As Long

Set sourceWB = Workbooks("CustomerData.xls")
Set destWB = Workbooks("ODonnell Sales Model17.xls")

Set sourceRange = destWB.Sheets("Customer Data").Range("b6")

If sourceRange.Value = "" Then
MsgBox "Please enter a Name"
sourceRange.Select
Exit Sub
End If

If Len(sourceRange.Value) = 1 Then searchltr = _
UCase(Left(sourceRange.Value, 1))


sourceWB.Activate
Set myStart = Range("D:D")
x = myStart.End(xlDown).Row - myStart.Row + 1



For a = 2 To x
Set AllCells = Range("d" & a)
Let testltr = UCase(Left(AllCells.Value, 1))
If searchltr = testltr Then
Me.ListBox1.AddItem AllCells.Value
End If
Next a

' Show the UserForm
UserForm1.Show
End Sub




Private Sub CANCEL_Click()
Unload Me

End Sub


Private Sub ListBox1_Click()
Dim AllCells As Range, Cell As Range
Dim myStart As Range
Dim destWB As Workbook
Dim searchltr As String, testltr As String
Dim sourceVal As String
Dim sourceWB As Workbook
Dim sourceRange As Range
Dim a As Long
Dim rng As Range
Dim rng1 As Range
Dim x As Long
Dim keyRange As Range

If Me.ListBox1.ListIndex = -1 Then Exit Sub
Set sourceWB = Workbooks("CustomerData.xls")
Set destWB = Workbooks("ODonnell Sales Model17.xls")

Set sourceRange = destWB.Sheets("Customer Data").Range("B6")


If Len(sourceRange.Value) = 1 Then searchltr = _
UCase(Left(sourceRange.Value, 1))


sourceWB.Activate
Set myStart = Range("D:D")
x = myStart.End(xlDown).Row - myStart.Row + 1


For a = 2 To x
Set AllCells = Range("D" & a)
If AllCells.Value = Me.ListBox1.Value Then
Set rng = AllCells
Exit For
End If
Next a

If Not rng Is Nothing Then
Application.Goto rng, True
Else

MsgBox "Not found"
End If

Set keyRange = destWB.Sheets("Sheet3").Range("A7")
ActiveCell.EntireRow.Copy _
Destination:=keyRange


Unload Me

End Sub
__________________________________________________ ____________
Code before and after loading userform:

With sourceWB.Sheets(1).Range("D:D")
Set rng = .Find(What:=sourceRange.Value, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValue, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)

End With

If Not rng Is Nothing Then
rng.EntireRow.Copy destrange
Msg = sourceRange.Value & "Loading."
MsgBox (Msg)
GoTo POPULATE
Else

Msg = sourceRange.Value & " Not Found. Would you like to lookup?" '

Define
message.
Style = vbYesNo ' Define buttons.
Title = "Customer Not Found" ' Define title.
Ctxt = 1000 ' Define topic
Response = MsgBox(Msg, Style, Title)
If Response = vbNo Then GoTo ender ' User chose No, so end.
End If

Load UserForm1

POPULATE:
Application.ScreenUpdating = False
destWB.Activate
Sheets("Sheet3").Visible = True
Sheets("Calculations").Visible = True
Sheets("Customer Data").Visible = True
Sheets("Calculations").Unprotect
Sheets("Customer Data").Unprotect



"dok112" wrote:


Brian,

Do you have any With statements in your macro? You will get the Run
Time Error 91 sometimes if you have a With statement, but are missing
the End With statement. If thats not the case, copy the code you are
using to show the userform, and I will look at it and see if I can
figure out whats causing the problem for you.

Brian C Wrote:
Hi,

I'm trying to call a userform from a macro, using:
UserForm1.show

When I run the macro, I get a Run Time Error 91 Object Variable or
With
Block Variable not set error. Because the code returns to the
UserForm1.show
statement in the macro rather than going on to the next step.

If I step through it, it proceeds back to the UserForm, and again

stops
at
the UserForm1.show statement.

I tried Load UserForm1 and had the same problem. How can I get it to
proceed to the next step in the original macro?

Thank you,

Brian



--
dok112
------------------------------------------------------------------------
dok112's Profile:

http://www.excelforum.com/member.php...o&userid=10581
View this thread:

http://www.excelforum.com/showthread...hreadid=529388






  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 37
Default User Form

Hi Doug,

Thanks for working with me on this. I changed it to Userform1.show in the
main macro, and it goes through the initialize routine, and then shows the
form. When you click on the selection in the userform, it goes through the
Listbox Click routine, but since it didn't end the userform initialize sub,
it goes back to that routine to end the sub. That seems to be where the
error occurs, then it stops at the original Userform.show statement in the
macro.

I inserted an on error resume next in the userform initialize and in the
calling macro, and this seems to work, so I think the problem may be moot,
but it's interesting that it occurred at all.

Thanks for all your help!

Brian

"Doug Glancy" wrote:

Brian,

I should have been clearer. In Debug mode when you're at the Load
statement, use F8 to step through your code. That might, or might not, show
you an error in the Userform itself.

One other thought: instead of loading it in your main module, Show it there
instead. I think there may be some problem with the order of the events as
you have them as I got the same error with just the Load statement in a main
module and just the Show statement in the Initialize event. Showing it in
the main module should not affect your Initialize code, at least I don't
think so.

hth,

Doug

"Brian C" wrote in message
...
Hi Dok112 and Doug,

I set the Break in Class and it stops at the same statement, where I
originally show the UserForm1 in the main macro. It looks like it's

trying
to repeat that step and not proceeding to the next one.

I don't have any with statements in the userform, so it may have an issue
with the Object Variable part. NOTE: in the macro, the next step after

the
Load UserForm1 command is a section named POPULATE:
Not sure if this matters.

Here's the UserForm code I have. Below I copied the section of the main
macro that loads the Userform, with the statements immediately preceding

and
following it:

Option Explicit

Private Sub userform_initialize()

Dim AllCells As Range, Cell As Range
Dim myStart As Range
Dim destWB As Workbook
Dim searchltr As String, testltr As String
Dim sourceVal As String
Dim sourceWB As Workbook
Dim sourceRange As Range
Dim x As Long
Dim a As Long

Set sourceWB = Workbooks("CustomerData.xls")
Set destWB = Workbooks("ODonnell Sales Model17.xls")

Set sourceRange = destWB.Sheets("Customer Data").Range("b6")

If sourceRange.Value = "" Then
MsgBox "Please enter a Name"
sourceRange.Select
Exit Sub
End If

If Len(sourceRange.Value) = 1 Then searchltr = _
UCase(Left(sourceRange.Value, 1))


sourceWB.Activate
Set myStart = Range("D:D")
x = myStart.End(xlDown).Row - myStart.Row + 1



For a = 2 To x
Set AllCells = Range("d" & a)
Let testltr = UCase(Left(AllCells.Value, 1))
If searchltr = testltr Then
Me.ListBox1.AddItem AllCells.Value
End If
Next a

' Show the UserForm
UserForm1.Show
End Sub




Private Sub CANCEL_Click()
Unload Me

End Sub


Private Sub ListBox1_Click()
Dim AllCells As Range, Cell As Range
Dim myStart As Range
Dim destWB As Workbook
Dim searchltr As String, testltr As String
Dim sourceVal As String
Dim sourceWB As Workbook
Dim sourceRange As Range
Dim a As Long
Dim rng As Range
Dim rng1 As Range
Dim x As Long
Dim keyRange As Range

If Me.ListBox1.ListIndex = -1 Then Exit Sub
Set sourceWB = Workbooks("CustomerData.xls")
Set destWB = Workbooks("ODonnell Sales Model17.xls")

Set sourceRange = destWB.Sheets("Customer Data").Range("B6")


If Len(sourceRange.Value) = 1 Then searchltr = _
UCase(Left(sourceRange.Value, 1))


sourceWB.Activate
Set myStart = Range("D:D")
x = myStart.End(xlDown).Row - myStart.Row + 1


For a = 2 To x
Set AllCells = Range("D" & a)
If AllCells.Value = Me.ListBox1.Value Then
Set rng = AllCells
Exit For
End If
Next a

If Not rng Is Nothing Then
Application.Goto rng, True
Else

MsgBox "Not found"
End If

Set keyRange = destWB.Sheets("Sheet3").Range("A7")
ActiveCell.EntireRow.Copy _
Destination:=keyRange


Unload Me

End Sub
__________________________________________________ ____________
Code before and after loading userform:

With sourceWB.Sheets(1).Range("D:D")
Set rng = .Find(What:=sourceRange.Value, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValue, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)

End With

If Not rng Is Nothing Then
rng.EntireRow.Copy destrange
Msg = sourceRange.Value & "Loading."
MsgBox (Msg)
GoTo POPULATE
Else

Msg = sourceRange.Value & " Not Found. Would you like to lookup?" '

Define
message.
Style = vbYesNo ' Define buttons.
Title = "Customer Not Found" ' Define title.
Ctxt = 1000 ' Define topic
Response = MsgBox(Msg, Style, Title)
If Response = vbNo Then GoTo ender ' User chose No, so end.
End If

Load UserForm1

POPULATE:
Application.ScreenUpdating = False
destWB.Activate
Sheets("Sheet3").Visible = True
Sheets("Calculations").Visible = True
Sheets("Customer Data").Visible = True
Sheets("Calculations").Unprotect
Sheets("Customer Data").Unprotect



"dok112" wrote:


Brian,

Do you have any With statements in your macro? You will get the Run
Time Error 91 sometimes if you have a With statement, but are missing
the End With statement. If thats not the case, copy the code you are
using to show the userform, and I will look at it and see if I can
figure out whats causing the problem for you.

Brian C Wrote:
Hi,

I'm trying to call a userform from a macro, using:
UserForm1.show

When I run the macro, I get a Run Time Error 91 Object Variable or
With
Block Variable not set error. Because the code returns to the
UserForm1.show
statement in the macro rather than going on to the next step.

If I step through it, it proceeds back to the UserForm, and again

stops
at
the UserForm1.show statement.

I tried Load UserForm1 and had the same problem. How can I get it to
proceed to the next step in the original macro?

Thank you,

Brian


--
dok112
------------------------------------------------------------------------
dok112's Profile:

http://www.excelforum.com/member.php...o&userid=10581
View this thread:

http://www.excelforum.com/showthread...hreadid=529388





Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I fill a cell in a user form from a selection on same form? Terry Tipsy Excel Discussion (Misc queries) 4 June 11th 07 02:59 PM
How to: User Form to assign a user defined range to a macro variab TrevTrav Excel Programming 1 March 22nd 05 07:57 PM
I am looking to see if anybody has an equivalant user form to Outlooks CONTACT form BruceJ[_2_] Excel Programming 2 October 15th 03 05:28 PM
User Form SP Excel Programming 2 July 21st 03 11:34 PM


All times are GMT +1. The time now is 11:13 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"