Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
KR KR is offline
external usenet poster
 
Posts: 121
Default Easy Syntax question: referring to worksheet by VBA name

I've renamed my worksheets in the VBE properties window ("Apple", "Pear",
"Melon"); in the Excel user view, they still show up as "sheet1", sheet2",
and "sheet3".

Now in my VBA I need to refer to a range on a selected sheet. If I hardcode
it, I can use:
Apple.Range("B4:F17").Select

but I'm pulling in the appropriate sheet name as a text string, "Apple" or
"Pear" or "Melon".

How do I use this dynamically? I'm sure it is something close to:

Dim oWrkSht as Worksheet

Set oWrkSht = "Apple"

oWrkSht.Range("B4:F17").Select

but I get a type mismatch, because the sheet can't be set to a string...

I'm sure there is an easy way to do this...

Thanks,
Keith

--
The enclosed questions or comments are entirely mine and don't represent the
thoughts, views, or policy of my employer. Any errors or omissions are my
own.


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Easy Syntax question: referring to worksheet by VBA name

Dim oWrkSht as Worksheet

set oWrkSht = apple

or use it directly.

apple.Range("A1").Value


do demo, from the immediate window:

set wksht = apple
? wksht.Name
Sheet1
? wksht.CodeName
apple

--
Regards,
Tom Ogilvy

"KR" wrote in message
...
I've renamed my worksheets in the VBE properties window ("Apple", "Pear",
"Melon"); in the Excel user view, they still show up as "sheet1", sheet2",
and "sheet3".

Now in my VBA I need to refer to a range on a selected sheet. If I

hardcode
it, I can use:
Apple.Range("B4:F17").Select

but I'm pulling in the appropriate sheet name as a text string, "Apple" or
"Pear" or "Melon".

How do I use this dynamically? I'm sure it is something close to:

Dim oWrkSht as Worksheet

Set oWrkSht = "Apple"

oWrkSht.Range("B4:F17").Select

but I get a type mismatch, because the sheet can't be set to a string...

I'm sure there is an easy way to do this...

Thanks,
Keith

--
The enclosed questions or comments are entirely mine and don't represent

the
thoughts, views, or policy of my employer. Any errors or omissions are my
own.




  #3   Report Post  
Posted to microsoft.public.excel.programming
KR KR is offline
external usenet poster
 
Posts: 121
Default Easy Syntax question: referring to worksheet by VBA name

Tom-
Thank you for your reply; please let me clarify- I may not be clearly
expressing (or even understanding) what I need;

I'm using Randy Birch's code to get the current user's Login ID. In my case,
that is "Keith1" which is acquired via the call to another procedu

sUsername = Trim(GetThreadUserName())
where the procedure returns a string via; Private Function
GetThreadUserName() As String

I've renamed my worksheet codename to Keith1 to match my username (and the
other sheets to the usernames of my officemates). Now I need to select the
right sheet, based on the username of whoever is logged in, e.g. the string
LoginID.

My guess is that rather than your suggested syntax
set oWrkSht = apple


what I have is the equivalent of

set oWrkSht = "Apple"

because I'm using
set oWrkSht = sUserName

which throws a compile error (type mismatch).

I'm sure there is a better approach, preferably one that doesn't involve
looping through all the sheets to check each one to see if the codename
matches the string.

I appreciate any addition insight you and others might provide...
Thanks,
Keith




"Tom Ogilvy" wrote in message
...
Dim oWrkSht as Worksheet

set oWrkSht = apple

or use it directly.

apple.Range("A1").Value


do demo, from the immediate window:

set wksht = apple
? wksht.Name
Sheet1
? wksht.CodeName
apple

--
Regards,
Tom Ogilvy

"KR" wrote in message
...
I've renamed my worksheets in the VBE properties window ("Apple",

"Pear",
"Melon"); in the Excel user view, they still show up as "sheet1",

sheet2",
and "sheet3".

Now in my VBA I need to refer to a range on a selected sheet. If I

hardcode
it, I can use:
Apple.Range("B4:F17").Select

but I'm pulling in the appropriate sheet name as a text string, "Apple"

or
"Pear" or "Melon".

How do I use this dynamically? I'm sure it is something close to:

Dim oWrkSht as Worksheet

Set oWrkSht = "Apple"

oWrkSht.Range("B4:F17").Select

but I get a type mismatch, because the sheet can't be set to a string...

I'm sure there is an easy way to do this...

Thanks,
Keith

--
The enclosed questions or comments are entirely mine and don't represent

the
thoughts, views, or policy of my employer. Any errors or omissions are

my
own.






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
Easy question - copying worksheet and keeping formulas vj1031 Excel Discussion (Misc queries) 2 October 29th 08 04:03 PM
new user with easy question? not easy for me speakeztruth New Users to Excel 5 June 3rd 05 09:40 PM
Easy VBA Syntax Question Hulk[_6_] Excel Programming 4 October 13th 04 07:23 PM
Easy VBA Syntax Question Hulk[_7_] Excel Programming 0 October 13th 04 06:41 PM
Syntax when referring to self Jack Schitt Excel Programming 3 September 2nd 04 02:04 AM


All times are GMT +1. The time now is 08:06 AM.

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"