ListBox + With End With

pit84

New Member
Joined
Sep 17, 2017
Messages
16
Hi


With FormExtraSheets.ListBoxExtraSheets
ActiveWorkbook.Worksheets(.Value).Delete
End With
- subscript out of range

With FormExtraSheets.ListBoxExtraSheets
shtName = .Value
ActiveWorkbook.Worksheets(shtName).Delete
End With
- works fine

Could someone explain to me, why, please, what the difference is
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
because outside of the activeworkbook.worksheets function, the reference in the with statement is attached, but within it the reference inferred in the with statement is not carried through, i've come across oddities like this in the past... and have something similarly annoying bugging me right now !
 
Last edited:
Upvote 0
One possibility is that the list box is returning a number (say 100). So in your first code you would be trying to delete sheet(100) (ie sheet index). But in the second code if ShtName has been declared as a string you would be deleting sheet("100") (ie sheet name).

So 2 questions
1) What is the listbox value when you get the error?
2) Have you declared shtName & if so what have you declared it as?
 
Last edited:
Upvote 0

Forum statistics

Threads
1,216,126
Messages
6,129,004
Members
449,480
Latest member
yesitisasport

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