PC Review


Reply
Thread Tools Rate Thread

Activate a Worksheet

 
 
Sean
Guest
Posts: n/a
 
      26th Oct 2006
For some reason, when I open up a workbook with several worksheets, I
am not able to activate a worksheet to select a range. Here's the
code:

Workbooks(excelFilename).Activate
Sheets(wsName).Select

Both excelFilename and wsname are Dim as String. As I trace the code,
it opens the workbook, but it will not select the sheet wsName.

For example, it will open the workbook and Sheet1 is active. When it
hits the Sheets(wsName).Select part of the code (where wsName is
"Sheet3), it does not switch to Sheet3.

I've also tried Sheets(wsName).Activate, and this does not work either.

 
Reply With Quote
 
 
 
 
Sandy
Guest
Posts: n/a
 
      26th Oct 2006
Replace Sheets(wsName).Select
with WorkSheets(wsName).Select

Sandy

Sean wrote:
> For some reason, when I open up a workbook with several worksheets, I
> am not able to activate a worksheet to select a range. Here's the
> code:
>
> Workbooks(excelFilename).Activate
> Sheets(wsName).Select
>
> Both excelFilename and wsname are Dim as String. As I trace the code,
> it opens the workbook, but it will not select the sheet wsName.
>
> For example, it will open the workbook and Sheet1 is active. When it
> hits the Sheets(wsName).Select part of the code (where wsName is
> "Sheet3), it does not switch to Sheet3.
>
> I've also tried Sheets(wsName).Activate, and this does not work either.


 
Reply With Quote
 
Sean
Guest
Posts: n/a
 
      26th Oct 2006

Sandy wrote:
> Replace Sheets(wsName).Select
> with WorkSheets(wsName).Select
>
> Sandy
>


That didn't work. It still does not Select the Sheet

 
Reply With Quote
 
=?Utf-8?B?Q2hhZEY=?=
Guest
Posts: n/a
 
      26th Oct 2006
Hey Sean,

Did you try :

Workbooks(excelFilename).Sheets(wsName).Range( ) ... ??

You could also try the following:

Public Function FindWorksheet(wsName as String) as Worksheet
Dim aWS as Worksheet

For Each aWS in ActiveWorkbook
If aWS.Name = wsName then
Set FindWorksheet = aWS
Exit Function
End If
Next aWS

Set FindWorksheet = Nothing
End Function


You'd call this function like this :

Dim myWorksheet as Worksheet

.... ' (some code)

Set myWorksheet = FindWorksheet(wsName)

myWorksheet.Range().Select ... ' whatever you want to do ...

Hope this helps,
Chad



"Sandy" wrote:

> Replace Sheets(wsName).Select
> with WorkSheets(wsName).Select
>
> Sandy
>
> Sean wrote:
> > For some reason, when I open up a workbook with several worksheets, I
> > am not able to activate a worksheet to select a range. Here's the
> > code:
> >
> > Workbooks(excelFilename).Activate
> > Sheets(wsName).Select
> >
> > Both excelFilename and wsname are Dim as String. As I trace the code,
> > it opens the workbook, but it will not select the sheet wsName.
> >
> > For example, it will open the workbook and Sheet1 is active. When it
> > hits the Sheets(wsName).Select part of the code (where wsName is
> > "Sheet3), it does not switch to Sheet3.
> >
> > I've also tried Sheets(wsName).Activate, and this does not work either.

>
>

 
Reply With Quote
 
Sandy
Guest
Posts: n/a
 
      26th Oct 2006
what is the name of the sheet exactly the way that it appears in the
tab...also let me see the code that this is in, from dimming the two
strings to calling the workbook and worksheet, and I'll see what may be
going on

Sandy
Sean wrote:
> Sandy wrote:
> > Replace Sheets(wsName).Select
> > with WorkSheets(wsName).Select
> >
> > Sandy
> >

>
> That didn't work. It still does not Select the Sheet


 
Reply With Quote
 
Sean
Guest
Posts: n/a
 
      27th Oct 2006
Here is the basic code:

Dim wbName As String
Dim wsName As String
Dim fileName As String

FileName = Cells(w, y)
Workbooks.Open FileName:= _
FileName, UpdateLinks:=xlUpdateLinksAlways
Workbooks(wbName).Activate
Worksheets(wsName).Activate
Rows("10:1000").Select

The code above is in XL1.xls. It will open an excel file (fileName)
and should activate the worksheet (wsname) and then select rows
10:1000....but it doesn't work.

If I walk through the code in the fileName it works, so I'm not sure
why it doesn't work with my code shown above.

 
Reply With Quote
 
=?Utf-8?B?Q2hhZEY=?=
Guest
Posts: n/a
 
      27th Oct 2006

Hey Sean,

Looking at your code...

> Dim wbName As String
> Dim wsName As String
> Dim fileName As String
>
> FileName = Cells(w, y)
> Workbooks.Open FileName:= _
> FileName, UpdateLinks:=xlUpdateLinksAlways


FileName has a definition... what doesnt have an assignment
is both wbName and wsName ...

> Workbooks(wbName).Activate
> Worksheets(wsName).Activate


So these 2 statements wont work the way it's written

... try Workbooks(FileName).Activate
and set the wsName before referencing it in
Worksheet(wsName).Activate ...

Hope this helps,
Chad





"Sean" wrote:

> Here is the basic code:
>
> Dim wbName As String
> Dim wsName As String
> Dim fileName As String
>
> FileName = Cells(w, y)
> Workbooks.Open FileName:= _
> FileName, UpdateLinks:=xlUpdateLinksAlways
> Workbooks(wbName).Activate
> Worksheets(wsName).Activate
> Rows("10:1000").Select
>
> The code above is in XL1.xls. It will open an excel file (fileName)
> and should activate the worksheet (wsname) and then select rows
> 10:1000....but it doesn't work.
>
> If I walk through the code in the fileName it works, so I'm not sure
> why it doesn't work with my code shown above.
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
trigger worksheet activate event in another worksheet Raj Microsoft Excel Programming 2 2nd Jun 2008 01:09 PM
Change Worksheet Button Caption on Worksheet.Activate =?Utf-8?B?TWlrZVp6?= Microsoft Excel Programming 1 7th Dec 2006 06:01 AM
Activate Previous worksheet after adding a new worksheet Chingangel@yahoo.com Microsoft Excel Programming 3 19th Oct 2005 01:01 AM
Activate Worksheet =?Utf-8?B?Y290dGFnZTY=?= Microsoft Excel Programming 1 21st Apr 2005 06:46 PM
Worksheet.activate =?Utf-8?B?SmVmZg==?= Microsoft Excel Misc 1 14th Dec 2004 01:52 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:41 PM.