Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default assigning worksheet variable

I want to set the variable ws similar to the For statement, but I want to set it for just the current active sheet.

Dim ws As Worksheet

For Each ws In ActiveWorkbook.Worksheets

please help this is not working:

ws = ActiveSheets

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,253
Default assigning worksheet variable


Activesheets does not exist. Activesheet does.
I assumed it's just a typo in your post... but typo's in code can be
crucial!

Therefor: ALWAYS start your code modules with
OPTION EXPLICIT
(it will force you to declare your variables before you can use them,
it may look cumbersome... but it's just good practice)


dim str as string
dim wks as worksheet 'this is an OBJECT variable
dim var 'type is not declared = variant


str = "Hi" 'Assign to normal variable officially it's
LET str = "Hi" 'but you're allowed to skip the LET statement

SET wks = Activesheet 'Assign to object variable

'Anytinh can be assinged to a variant but if you're working with an
object you must use SET
var = "Hi"
set var = Activesheet



Same goes for comparing...

use IS for objects

if wks IS nothing
if wks IS thisworkbook.worksheets(1)

use = for values
if str = "hi"
if str = "oops"
if wks.name = "Sheet1"

etc...

search help on DIM and take it from there.




keepITcool

< email : keepitcool chello nl (with @ and .)
< homepage: http://members.chello.nl/keepitcool


"?B?U2ltb24gU2hhdw==?="
wrote:

I want to set the variable ws similar to the For statement, but I want
to set it for just the current active sheet.

Dim ws As Worksheet

For Each ws In ActiveWorkbook.Worksheets

please help this is not working:

ws = ActiveSheets



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default assigning worksheet variable

Simon,

There is only one activesheet in a workbook, so you can get it simply by

Set ws = Activesheet

but you could just as easily use use ACtivesheet in your code directly.

You may be thinking of selected sheets, which is accessed like

For Each ws In Activewindow.SelectedSheets
...
Next ws

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

"Simon Shaw" wrote in message
...
I want to set the variable ws similar to the For statement, but I want to

set it for just the current active sheet.

Dim ws As Worksheet

For Each ws In ActiveWorkbook.Worksheets

please help this is not working:

ws = ActiveSheets



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default assigning worksheet variable

Thanks, it was the SET command I was missing, I had tried a million different ways otherwise (without an 's', with worksheets, etc...)
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,824
Default assigning worksheet variable

Just to add to Bob's reply...

One very nice reason to use the variable ws (and to declare it as a worksheet)
is that you'll get VBE's intellisense while you're writing the code.



Bob Phillips wrote:

Simon,

There is only one activesheet in a workbook, so you can get it simply by

Set ws = Activesheet

but you could just as easily use use ACtivesheet in your code directly.

You may be thinking of selected sheets, which is accessed like

For Each ws In Activewindow.SelectedSheets
...
Next ws

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

"Simon Shaw" wrote in message
...
I want to set the variable ws similar to the For statement, but I want to

set it for just the current active sheet.

Dim ws As Worksheet

For Each ws In ActiveWorkbook.Worksheets

please help this is not working:

ws = ActiveSheets


--

Dave Peterson

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
Assigning values to a variable set John A[_2_] Excel Discussion (Misc queries) 1 April 23rd 10 08:20 PM
Assigning a variable Zack Excel Worksheet Functions 3 December 11th 08 07:00 PM
Assigning Variable Range JMay Excel Discussion (Misc queries) 1 July 7th 07 04:52 PM
Assigning worksheet to worksheet variable PO Excel Programming 3 February 10th 04 02:22 PM
Re-assigning the value of a variable for a for loop from Mike Berry Excel Programming 1 July 16th 03 02:41 PM


All times are GMT +1. The time now is 09:14 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"