Spaces in sheet name stops macro

JonatMV

New Member
Joined
Jan 28, 2011
Messages
31
Hi - I've had a macro running for ages, and just found it bugs out when there are spaces in a sheet name.

The user selects a destination cell as part of a form, which passes the variable to the following steps:

Code:
looperx = 0
Do
looperx = looperx + 1
Loop Until Mid(celldestinationstring, looperx, 1) = "!"
Sheets(Left(celldestinationstring, looperx - 1)).Select

The purpose of which is to isolate the sheet name from the cell reference, which is typically like: 'Bob Sheet'!$B$9

This works fine where there are no spaces, but 'dies' with a runtime error when there are. With the apostrophes I assumed it would work around spaces in the string but I can't get it to work.

Appreciate any help... I'm thinking it's probably something simple.
 
Last edited:

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
If you just want to select the sheet for some reason, I would use:
Code:
Range(celldestinationstring).Worksheet.Select
 
Upvote 0
The problem is probably the 's.

You wouldn't use them when referring to a worksheet by name in VBA.

Code:
pos = InStr(1, celldestination, "!")
 
celldestination = Left(celldestination, pos-1)
 
celldestination = Replace(celldestination, "'", "")

Sheets(celldestination).Select
 
Upvote 0
You could just extract the sheet directly:

Code:
Range(celldestinationstring).Parent.Select

Note that you don't usually have to Select anything to work with it and doing so is inefficient.
 
Upvote 0
Thanks all for your responses.

I realised after reading some of the replies that I'd written this some time ago and it could well be optimised somewhat (InStr being a good example!)

I used Richard's in the end as it was a one liner which I quite liked. From memory - I chose to select the sheet so the user ends up looking at the output of the activesheet.querytable that is created subsequently... albeit admittedly it may be a redundant step as I've not tested without it.

Cheers again.
 
Upvote 0
That's true, Wendy, we don't.
 
Upvote 0
Ooh you beast!!! Handbags at dawn and if you think I'm doing your blue-rinse next week, forget it! :lol:
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,595
Messages
6,179,798
Members
452,943
Latest member
Newbie4296

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