Userform help

Damo10

Active Member
Joined
Dec 13, 2010
Messages
460
Hi,

I am trying to set the worksheet name in my code to that of a selection from a combobox. I cant seem to get the code right for this part, the rest works fine can anyone help?

Regards

Rich (BB code):
Private Sub CommandButton1_Click()
Dim lr As Long
Dim x As Long
Dim list As String
Dim ws1 As Worksheet
Dim sh As String
ws1 = Me.cboSheet - Problem with this line
With ws1
lr = .Range("B" & Rows.Count).End(xlUp).Row
End With
For x = 10 To lr
With ws1
list = .Range("B" & x) & " " & .Range("C" & x)
Me.cboList.AddItem list
End With
Next x
End Sub
Private Sub UserForm_Initialize()
Dim sh As String
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
sh = ws.Name
Me.cboSheet.AddItem sh
Next ws
End Sub
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Since it's an object you need to use Set when assigning, like this:

Code:
Set ws1 = Me.cboSheet 'no more problem with this line
[/COLOR]
 
Upvote 0
Hi,

I tried that first and still get the same error

Run time error 91
Object variable or with block variable not set

Regards
 
Upvote 0
oops, my bad, I only looked at the line in red :biggrin:

you should declare ws1 and instantiate it, like this
Code:
Dim ws1 As New Worksheet
or like this

Code:
Dim ws1 As Worksheet
Set ws1 = new Worksheet
difference between the two is cause for major debate, but comes down to personal preference :rolleyes:
 
Upvote 0
Hi,

I have tried that and it does not work :-(

I am trying to use a userform to select one of the existing sheets in my workbook and then from the selection populate a combobox with the values from the sheet selected from the first combobox.

Regards
 
Upvote 0
Hmm, okay, so the rest of your code does not work fine, like you claimed :stickouttounge:

This commandbutton, is that a button on a worksheet or on your userform?

What error do you get now, and at what line in the code?
 
Upvote 0
the button is from the userform.
The error I get now is still on the same line and is a run time error 13 type mismatch
 
Upvote 0
Can you try this:

Code:
Set ws1 = ThisWorkbook.Worksheets(Me.cboSheet)

I'm still trying to figure out your code...
 
Upvote 0
Hi how are you.

Yes there is a mismatch you declare wsl as a worksheet and then you equate that to Me.cbo...... which is an object on the userform. What do you what to achieve from this line?

Are you loading the activesheets name of the activework at initialization ( when loading the userform). Can you through light on the overall plan of the userform. This will help to resolve the issues around the errors
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,564
Messages
6,179,543
Members
452,924
Latest member
JackiG

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