Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 16
Default how to reference a worksheet with a variable vs a name

I have 3 worksheets that I fill with Data depending if the country is US,
Canada or Not Defined. What I want to do is reference the worksheet with a
variable such as

if country = US then n = 1
else if country = Canada then n = 2
else n = 3
endif

This worksheet(n) = the data

I don't know if this can be done. I get an error when I try it. If I can
do this then I don't have to write a section for each of the different
worksheets.

Thanks In advance

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,494
Default how to reference a worksheet with a variable vs a name

here is something i came up with
Option Explicit
Dim country As String
Dim n As Long
Sub test()
country = "US"
If country = "US" Then
n = 2
Else
n = 3
End If
Sheets(n).Activate
End Sub

--


Gary


"ibbm" wrote in message
...
I have 3 worksheets that I fill with Data depending if the country is US,
Canada or Not Defined. What I want to do is reference the worksheet with a
variable such as

if country = US then n = 1
else if country = Canada then n = 2
else n = 3
endif

This worksheet(n) = the data

I don't know if this can be done. I get an error when I try it. If I can
do this then I don't have to write a section for each of the different
worksheets.

Thanks In advance



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5
Default how to reference a worksheet with a variable vs a name

maybe this will help:

Dim ShtName as String
ShtName = "Sheet" & str(n)

reference it like this:
ThisWorkbook.Worksheets(ShtName)

Hope this helps
-Steve


"ibbm" wrote:

I have 3 worksheets that I fill with Data depending if the country is US,
Canada or Not Defined. What I want to do is reference the worksheet with a
variable such as

if country = US then n = 1
else if country = Canada then n = 2
else n = 3
endif

This worksheet(n) = the data

I don't know if this can be done. I get an error when I try it. If I can
do this then I don't have to write a section for each of the different
worksheets.

Thanks In advance

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 16
Default how to reference a worksheet with a variable vs a name



"Patch61" wrote:

maybe this will help:

Dim ShtName as String
ShtName = "Sheet" & str(n)

reference it like this:


Hope this helps
-Steve

In theory I should be able to go

ShtName = "Canada"
ThisWorkbook.Worksheets(ShtName)

ThisWorkbook.Sheets(ns).Range(Cells(2, 3), Cells(2, 13)).Select
Range(Cells(2, 3), Cells(2, 13)).Value = "Sales in Units"

But I get an error message 1004 Application defined or object error
"ibbm" wrote:




I have 3 worksheets that I fill with Data depending if the country is US,
Canada or Not Defined. What I want to do is reference the worksheet with a
variable such as

if country = US then n = 1
else if country = Canada then n = 2
else n = 3
endif

This worksheet(n) = the data

I don't know if this can be done. I get an error when I try it. If I can
do this then I don't have to write a section for each of the different
worksheets.

Thanks In advance

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
making a portion of reference to a worksheet variable in a formula Celia Excel Discussion (Misc queries) 1 October 25th 09 08:27 AM
Vlookup with variable worksheet reference trempnvt Excel Worksheet Functions 6 May 23rd 06 06:47 PM
Making a file and worksheet reference into a variable.... Mr Mike Excel Worksheet Functions 3 July 6th 05 08:52 PM
Problem with =sum(offset(cell reference,w,x,y,z). I want cell reference to be variable [email protected] Excel Worksheet Functions 2 December 11th 04 12:00 AM
Using a variable as a row reference? mjmorrison Excel Programming 2 July 14th 03 07:51 PM


All times are GMT +1. The time now is 05:16 PM.

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

About Us

"It's about Microsoft Excel"