Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 107
Default Referring to a Worksheet with a variable

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
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 249
Default Referring to a Worksheet with a variable

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

"donbowyer" wrote:
| 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


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,986
Default Referring to a Worksheet with a variable

Sheets("MySheet")

"donbowyer" wrote:

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

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10,593
Default Referring to a Worksheet with a variable

Set VBComp = ThisWorkbook.VBProject.VBComponents(MySh)


--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)

"donbowyer" wrote in message
...
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



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 107
Default Referring to a Worksheet with a variable

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


"Dave Patrick" wrote:

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

"donbowyer" wrote:
| 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





  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 107
Default Referring to a Worksheet with a variable

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


"Bob Phillips" wrote:

Set VBComp = ThisWorkbook.VBProject.VBComponents(MySh)


--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)

"donbowyer" wrote in message
...
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




  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 249
Default Referring to a Worksheet with a variable

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

"donbowyer" wrote:
| 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


  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 249
Default Referring to a Worksheet with a variable

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

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


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Referring to a variable cell on another sheet PaladinWhite Excel Worksheet Functions 5 March 24th 08 02:14 AM
Referring to a variable file name Brettjg Excel Discussion (Misc queries) 4 March 3rd 07 11:18 AM
Recall a variable referring to a worksheet name? Mike[_79_] Excel Programming 5 April 22nd 04 11:03 PM
Referring to a worksheet using variable WSF Excel Programming 5 January 20th 04 03:16 PM
Referring to a local or global variable or constant dynamically? PC[_2_] Excel Programming 1 September 15th 03 02:31 PM


All times are GMT +1. The time now is 06:40 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"