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

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
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,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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