Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5
Default Help! Selecting a Worksheet with VBA

Hello,

Ok we all know that I can select a sheet on Excel by doing:
sheets("Name").select or worksheets("Name").select

But my problem is that I take the name from a cell with something like:

Dim Name as String
Name = Range("A1").value

If I now do:
sheet(Name).select

I get an error (Name as to be an Object, not a string). I can I do
this? It shouldn't be that hard to select a sheet from a cell value!

Damm VBA!

Regards,

Yona

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,290
Default Help! Selecting a Worksheet with VBA

Yona,
1. Don't use "Name" as a variable, it is reserved for use by Excel.
2. Your code is missing a character...
sheet(Name).select
Should be...
Sheets(Name).Select
--
Jim Cone
San Francisco, USA
http://www.realezsites.com/bus/primitivesoftware




wrote in message
Hello,
Ok we all know that I can select a sheet on Excel by doing:
sheets("Name").select or worksheets("Name").select
But my problem is that I take the name from a cell with something like:

Dim Name as String
Name = Range("A1").value

If I now do:
sheet(Name).select
I get an error (Name as to be an Object, not a string). I can I do
this? It shouldn't be that hard to select a sheet from a cell value!
Damm VBA!
Regards,
Yona

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,123
Default Help! Selecting a Worksheet with VBA

This is working OK

Add also the sheet name to the name string
sheet(Name).select

you forgot the s


Dim Name As String
Name = Sheets("Sheet1").Range("A1").Value

Sheets(Name).Select


--

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm


wrote in message oups.com...
Hello,

Ok we all know that I can select a sheet on Excel by doing:
sheets("Name").select or worksheets("Name").select

But my problem is that I take the name from a cell with something like:

Dim Name as String
Name = Range("A1").value

If I now do:
sheet(Name).select

I get an error (Name as to be an Object, not a string). I can I do
this? It shouldn't be that hard to select a sheet from a cell value!

Damm VBA!

Regards,

Yona

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 22,906
Default Help! Selecting a Worksheet with VBA

Yona

This works for me........xl2003

Dim Name As String
Name = Range("A1").Value
Sheets(Name).Select

Note the Sheets as opposed to Sheet


Gord Dibben MS Excel MVP


On 18 Dec 2006 11:09:15 -0800, wrote:

Hello,

Ok we all know that I can select a sheet on Excel by doing:
sheets("Name").select or worksheets("Name").select

But my problem is that I take the name from a cell with something like:

Dim Name as String
Name = Range("A1").value

If I now do:
sheet(Name).select

I get an error (Name as to be an Object, not a string). I can I do
this? It shouldn't be that hard to select a sheet from a cell value!

Damm VBA!

Regards,

Yona


  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5
Default Help! Selecting a Worksheet with VBA

Thanks all for your answers,

If I create a new book, put "Sheet2" on A1 and add your code I get the
following:

Run time error 9
Subscript out of range

I don't know why this works on your excel and not mine (XL 2003 too!).

Regards,

Yona


Gord Dibben wrote:
Yona

This works for me........xl2003

Dim Name As String
Name = Range("A1").Value
Sheets(Name).Select

Note the Sheets as opposed to Sheet


Gord Dibben MS Excel MVP


On 18 Dec 2006 11:09:15 -0800, wrote:

Hello,

Ok we all know that I can select a sheet on Excel by doing:
sheets("Name").select or worksheets("Name").select

But my problem is that I take the name from a cell with something like:

Dim Name as String
Name = Range("A1").value

If I now do:
sheet(Name).select

I get an error (Name as to be an Object, not a string). I can I do
this? It shouldn't be that hard to select a sheet from a cell value!

Damm VBA!

Regards,

Yona


  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5
Default Help! Selecting a Worksheet with VBA

Thank you for the answer,

Unfortunately this doesn't work for me. I get the following error:

run time error 9
Subscript out of range

Any idea??

Regards,

Yona


Jim Thomlinson wrote:
This will work so long as the value in the cell is actually the name of one
of the sheets.

Sheets(Range("A1").Value).Select

I would be inclined to confirm that the sheet exists before I try to select
it...

if SheetExists(Range("A1").Value) then Sheets(Range("A1").Value).Select

...

Public Function SheetExists(SName As String, _
Optional ByVal Wb As Workbook) As Boolean
'Chip Pearson
On Error Resume Next
If Wb Is Nothing Then Set Wb = ThisWorkbook
SheetExists = CBool(Len(Wb.Sheets(SName).Name))
End Function

--
HTH...

Jim Thomlinson


" wrote:

Hello,

Ok we all know that I can select a sheet on Excel by doing:
sheets("Name").select or worksheets("Name").select

But my problem is that I take the name from a cell with something like:

Dim Name as String
Name = Range("A1").value

If I now do:
sheet(Name).select

I get an error (Name as to be an Object, not a string). I can I do
this? It shouldn't be that hard to select a sheet from a cell value!

Damm VBA!

Regards,

Yona



  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default Help! Selecting a Worksheet with VBA

That's because you didn't declare from which sheet range("A1) you took the
data from,so the correct answer would be;

'In a new workbook
Private Sub Test()
Dim Name As String
Name = Worksheets("sheet1").Range("A1").Value
Sheets(Name).Select
End Sub

wrote in message
ups.com...
Thanks all for your answers,

If I create a new book, put "Sheet2" on A1 and add your code I get the
following:

Run time error 9
Subscript out of range

I don't know why this works on your excel and not mine (XL 2003 too!).

Regards,

Yona


Gord Dibben wrote:
Yona

This works for me........xl2003

Dim Name As String
Name = Range("A1").Value
Sheets(Name).Select

Note the Sheets as opposed to Sheet


Gord Dibben MS Excel MVP


On 18 Dec 2006 11:09:15 -0800, wrote:

Hello,

Ok we all know that I can select a sheet on Excel by doing:
sheets("Name").select or worksheets("Name").select

But my problem is that I take the name from a cell with something like:

Dim Name as String
Name = Range("A1").value

If I now do:
sheet(Name).select

I get an error (Name as to be an Object, not a string). I can I do
this? It shouldn't be that hard to select a sheet from a cell value!

Damm VBA!

Regards,

Yona




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
Selecting a row in a worksheet Rochelle in Melbourne Excel Worksheet Functions 1 April 9th 09 11:34 AM
Selecting Next Worksheet Fleone Excel Programming 3 May 2nd 06 05:05 PM
selecting a worksheet rocket0612 Excel Worksheet Functions 2 March 1st 06 09:47 AM
selecting cell range in other worksheet without switching to worksheet suzetter[_4_] Excel Programming 4 June 22nd 05 08:55 PM
Selecting Last Worksheet Alex Excel Programming 7 September 26th 04 09:35 PM


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