Referring to a Worksheet with a variable

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

This The expression works OK:-
Set VBComp = ThisWorkbook.VBProject.VBComponents("Sheet1")
But what is the correct syntax if I want to refer to a sheet with a variable
number?
Suppose the variable sheet number is MySh, then ("SheetMySh") produces a
runtime error "Script out of range"
 
MsgBox Application.Worksheets(1).Name
MsgBox Application.Sheets(1).Name

Substitute your variable for 1

--

Regards,

Dave Patrick ....Please no email replies - reply in newsgroup.
Microsoft Certified Professional
Microsoft MVP [Windows]
http://www.microsoft.com/protect

:
| This The expression works OK:-
| Set VBComp = ThisWorkbook.VBProject.VBComponents("Sheet1")
| But what is the correct syntax if I want to refer to a sheet with a
variable
| number?
| Suppose the variable sheet number is MySh, then ("SheetMySh") produces a
| runtime error "Script out of range"
| --
| donwb
 
Set VBComp = ThisWorkbook.VBProject.VBComponents(MySh)


--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)
 
Thanks for the inputs but still no go.
Perhaps if I repeat the problem.
The expression:-
Set VBComp = ThisWorkbook.VBProject.VBComponents("Sheet1")
works fine, but
Set VBComp = ThisWorkbook.VBProject.VBComponents("SheetMySh")
where "MySh" is a variable sheet number replacing the "1"
Does not work.
I think it's in the syntax, but not sure.
 
Try;

Set VBComp = ThisWorkbook.VBProject.VBComponents("Sheet" & MySh)
MsgBox VBComp.Name

--

Regards,

Dave Patrick ....Please no email replies - reply in newsgroup.
Microsoft Certified Professional
Microsoft MVP [Windows]
http://www.microsoft.com/protect

:
| Thanks for the inputs but still no go.
| Perhaps if I repeat the problem.
| The expression:-
| Set VBComp = ThisWorkbook.VBProject.VBComponents("Sheet1")
| works fine, but
| Set VBComp = ThisWorkbook.VBProject.VBComponents("SheetMySh")
| where "MySh" is a variable sheet number replacing the "1"
| Does not work.
| I think it's in the syntax, but not sure.
| --
| donwb
 
Doesn't that return ThisWorkbook ? Or maybe that's what you wanted.

--

Regards,

Dave Patrick ....Please no email replies - reply in newsgroup.
Microsoft Certified Professional
Microsoft MVP [Windows]
http://www.microsoft.com/protect

:
| Thanks Bob - simple when you know how: that works fine
| --
| donwb
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top