vba - check worksheet existence

davidhasselhoff

New Member
Joined
Feb 12, 2009
Messages
37
Hello!

I got a listbox which chooses from which worksheet a value should be copied.

Now If there is a value in the listbox, where there is no corresponding worksheet, I get an "out of range" error.

What I need is a way to check if there is a worksheet called as the choice the user makes to produce an error/cancel/go back message if there isn't.

Here is the code i use:

Code:
Cells(11, 3) = ThisWorkbook.Worksheets(UserFormsea.ListBox3.Text).Range("b29")

Any help would be greatly appreciated!
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Here's How I do it


Code:
Dim MySheet as WorkSheet
On Error Resume Next
Set MySheet = ThisWorkbook.Worksheets(UserFormsea.ListBox3.Text)
On Error Goto 0
If MySheet is Nothing Then
    Msgbox "Sheet Doesn't exist"
    Exit Sub
End If
Cells(11, 3) = MySheet.Range("b29")

Hope that helps...
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

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