Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7
Default Using a variable to select a cell on a worksheet..."Subscript out orrange"

See code below. I know this is pretty simple, but what I'm trying to
do is pull data from a cell (in the test.xls spreadsheet) that has a
varying (based upon what someone writes in the cell) text input. I
then want the macro I run to reference this cell to select which
worksheet it is going to select in the spreadsheet in which I'm
running the macro.




Dim rng As RangeDim ws1 As Worksheet
Dim WhichSheetCell As String
WhichSheetCell = Workbooks.Open("Z:\Invoices
\test.xls").Worksheets("Sheet1").Cells(3, 2)
Set ws1 = Sheets("WhichSheetCell")


Set rng = ws1.Range("A1:N" & Rows.Count)

I get a "subscript out of range" error when I run it.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 638
Default Using a variable to select a cell on a worksheet..."Subscript outor range"

On May 28, 10:00*am, Zarlot wrote:
See code below. *I know this is pretty simple, but what I'm trying to
do is pull data from a cell (in the test.xls spreadsheet) that has a
varying (based upon what someone writes in the cell) text input. *I
then want the macro I run to reference this cell to select which
worksheet it is going to select in the spreadsheet in which I'm
running the macro.

Dim rng As RangeDim ws1 As Worksheet
*Dim WhichSheetCell As String
* WhichSheetCell = Workbooks.Open("Z:\Invoices
\test.xls").Worksheets("Sheet1").Cells(3, 2)
* * Set ws1 = Sheets("WhichSheetCell")

Set rng = ws1.Range("A1:N" & Rows.Count)

I get a "subscript out of range" error when I run it.


2 things. You have ws1 declared as a Worksheet, but you are trying to
assign a range to it (.Cells(3,2) from the WhichSheetCell variable).
WhichSheetCell is a variable, but you have quotes around it in your
Set ws1 statement.

I'm not exactly sure where the Cells(3,2) is coming into play, but
here is a snippet you can use for an example.

Dim ws1 As Worksheet, rng As Range
Set ws1 = Workbooks.Open("Z:\Invoices\test.xls") _
.Worksheets("Sheet1")
Set rng = ws1.Range("A1:N" & Rows.Count)
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,441
Default Using a variable to select a cell on a worksheet..."Subscript out or range"

Zarlot,

Change

Set ws1 = Sheets("WhichSheetCell")

to

Set ws1 = Sheets(WhichSheetCell)

(Remove the double quotes.)

Still, you will probably need some error checking in there...

HTH,
Bernie
MS Excel MVP


"Zarlot" wrote in message
...
See code below. I know this is pretty simple, but what I'm trying to
do is pull data from a cell (in the test.xls spreadsheet) that has a
varying (based upon what someone writes in the cell) text input. I
then want the macro I run to reference this cell to select which
worksheet it is going to select in the spreadsheet in which I'm
running the macro.




Dim rng As RangeDim ws1 As Worksheet
Dim WhichSheetCell As String
WhichSheetCell = Workbooks.Open("Z:\Invoices
\test.xls").Worksheets("Sheet1").Cells(3, 2)
Set ws1 = Sheets("WhichSheetCell")


Set rng = ws1.Range("A1:N" & Rows.Count)

I get a "subscript out of range" error when I run it.



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7
Default Using a variable to select a cell on a worksheet..."Subscript outor range"

On May 28, 10:25*am, "Bernie Deitrick" <deitbe @ consumer dot org
wrote:
Zarlot,

Change

Set ws1 = Sheets("WhichSheetCell")

to

Set ws1 = Sheets(WhichSheetCell)

(Remove the double quotes.)

Still, you will probably need some error checking in there...

HTH,
Bernie
MS Excel MVP

"Zarlot" wrote in message

...



See code below. *I know this is pretty simple, but what I'm trying to
do is pull data from a cell (in the test.xls spreadsheet) that has a
varying (based upon what someone writes in the cell) text input. *I
then want the macro I run to reference this cell to select which
worksheet it is going to select in the spreadsheet in which I'm
running the macro.


Dim rng As RangeDim ws1 As Worksheet
Dim WhichSheetCell As String
*WhichSheetCell = Workbooks.Open("Z:\Invoices
\test.xls").Worksheets("Sheet1").Cells(3, 2)
* *Set ws1 = Sheets("WhichSheetCell")


Set rng = ws1.Range("A1:N" & Rows.Count)


I get a "subscript out of range" error when I run it.- Hide quoted text -


- Show quoted text -


Yeah, I think one of my problems is that I'm referencing the cell when
I really want to reference the contents of the cell...the text in the
cell. But what I will try this.
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7
Default Using a variable to select a cell on a worksheet..."Subscript outor range"

On May 28, 10:22*am, JW wrote:
On May 28, 10:00*am, Zarlot wrote:





See code below. *I know this is pretty simple, but what I'm trying to
do is pull data from a cell (in the test.xls spreadsheet) that has a
varying (based upon what someone writes in the cell) text input. *I
then want the macro I run to reference this cell to select which
worksheet it is going to select in the spreadsheet in which I'm
running the macro.


Dim rng As RangeDim ws1 As Worksheet
*Dim WhichSheetCell As String
* WhichSheetCell = Workbooks.Open("Z:\Invoices
\test.xls").Worksheets("Sheet1").Cells(3, 2)
* * Set ws1 = Sheets("WhichSheetCell")


Set rng = ws1.Range("A1:N" & Rows.Count)


I get a "subscript out of range" error when I run it.


2 things. *You have ws1 declared as a Worksheet, but you are trying to
assign a range to it (.Cells(3,2) from the WhichSheetCell variable).
WhichSheetCell is a variable, but you have quotes around it in your
Set ws1 statement.

I'm not exactly sure where the Cells(3,2) is coming into play, but
here is a snippet you can use for an example.

* * Dim ws1 As Worksheet, rng As Range
* * Set ws1 = Workbooks.Open("Z:\Invoices\test.xls") _
* * * * .Worksheets("Sheet1")
* * Set rng = ws1.Range("A1:N" & Rows.Count)- Hide quoted text -

- Show quoted text -



Thanks for the help. Cell(3, 2) comes into play because it's the cell
which has in it the worksheet name (it's types in there) that the
macro is supposed to use to find on another spreadsheet and then
select that worksheet to do all of its work on.

Kind of like... I enter on the main spreadsheet what worksheet I want
it to look for on the other, and I put it in Cell(3, 2).

Really... the more I think about it... the more really a Message/Input
Box would be a lot easier and using the input to the messagebox as the
variable... I am really a novice though but I'll try to figure this
out.

Thanks
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
Excel - Golf - how to display "-2" as "2 Under" or "4"as "+4" or "4 Over" in a calculation cell Steve Kay Excel Discussion (Misc queries) 2 August 8th 08 01:54 AM
Range("C100:D200").Select with variable names Fan924 Excel Programming 2 October 15th 07 03:54 PM
Format Cell to show "-1" in subscript PCH Excel Discussion (Misc queries) 1 May 19th 06 02:46 AM
use variable in Workbooks("book1").Worksheets("sheet1").Range("a1" Luc[_3_] Excel Programming 2 September 28th 05 08:37 PM
"Subscript out of range" error for: Workbooks("Test1.xls").Save Just12341234 Excel Programming 2 June 17th 05 03:16 PM


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