PC Review


Reply
Thread Tools Rate Thread

Changing the ActiveSheet

 
 
David
Guest
Posts: n/a
 
      2nd Nov 2007
I've got a workbook with several different sheets. I want a macro
that will select the required sheet depending on what cell is
highlighted.

For example there are cells for Jan, through to Dec. If the Mar cell
is highlighted, when the user clicks on the macro button I want it to
open the sheet for Mar and do some processing. I know how to do the
processing but how do you change what sheet is active based on this
kind of user action?

Thanks!

 
Reply With Quote
 
 
 
 
=?Utf-8?B?SmF5?=
Guest
Posts: n/a
 
      2nd Nov 2007
Hi David -

Here's a basic starting point:

Private Sub CommandButton1_Click()
Worksheets(ActiveCell.Value).Activate
End Sub
---
Jay

"David" wrote:

> I've got a workbook with several different sheets. I want a macro
> that will select the required sheet depending on what cell is
> highlighted.
>
> For example there are cells for Jan, through to Dec. If the Mar cell
> is highlighted, when the user clicks on the macro button I want it to
> open the sheet for Mar and do some processing. I know how to do the
> processing but how do you change what sheet is active based on this
> kind of user action?
>
> Thanks!
>
>

 
Reply With Quote
 
David
Guest
Posts: n/a
 
      2nd Nov 2007
OK, this works:

sMonth = ActiveCell.Value

Worksheets(sMonth).Activate

Another question, how do I used wildcards in Excel?

For example, if the active cell was "Mar" but the worksheet was Mar
'07, how could I get it to still open the right worksheet?

I tried adding a * after the Worksheets(sMonth) but that didn't work,
any ideas?

 
Reply With Quote
 
=?Utf-8?B?SmF5?=
Guest
Posts: n/a
 
      2nd Nov 2007
The following activates the worksheet whose name starts with whatever is in
the activecell (sMonth):

Private Sub CommandButton1_Click()

sMonth = ActiveCell.Value

For Each ws In Worksheets
If InStr(1, ws.Name, sMonth) = 1 Then ws.Activate
Next 'ws

End Sub
---
Jay

"David" wrote:

> OK, this works:
>
> sMonth = ActiveCell.Value
>
> Worksheets(sMonth).Activate
>
> Another question, how do I used wildcards in Excel?
>
> For example, if the active cell was "Mar" but the worksheet was Mar
> '07, how could I get it to still open the right worksheet?
>
> I tried adding a * after the Worksheets(sMonth) but that didn't work,
> any ideas?
>
>

 
Reply With Quote
 
Mike Fogleman
Guest
Posts: n/a
 
      2nd Nov 2007
Take a look at the LIKE operator.

Mike F
"David" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> OK, this works:
>
> sMonth = ActiveCell.Value
>
> Worksheets(sMonth).Activate
>
> Another question, how do I used wildcards in Excel?
>
> For example, if the active cell was "Mar" but the worksheet was Mar
> '07, how could I get it to still open the right worksheet?
>
> I tried adding a * after the Worksheets(sMonth) but that didn't work,
> any ideas?
>



 
Reply With Quote
 
David
Guest
Posts: n/a
 
      2nd Nov 2007
Thanks for the help so far, it's working well!

Another query.

I have worksheets Apr, May, Jun etc.

Sometimes when I go into Jun worksheet for example, I might need to
also check what was in the previous month, is there an easy way of
doing this rather than saying if it's Jun then check May etc etc? Are
worksheets numbered even though they have names?

Thanks again!


 
Reply With Quote
 
=?Utf-8?B?SmF5?=
Guest
Posts: n/a
 
      2nd Nov 2007
Hi David -

Yes, worksheets have an Index property and they are numbered sequentially
from left to right, starting with 1. So, these statements may work for you:

'Check the value in cell A1 of the sheet before the activesheet
MsgBox Worksheets(ActiveSheet.Index - 1).Range("A1")

'Activate the sheet before the activesheet
Worksheets(ActiveSheet.Index - 1).Activate

---
Jay

"David" wrote:

> Thanks for the help so far, it's working well!
>
> Another query.
>
> I have worksheets Apr, May, Jun etc.
>
> Sometimes when I go into Jun worksheet for example, I might need to
> also check what was in the previous month, is there an easy way of
> doing this rather than saying if it's Jun then check May etc etc? Are
> worksheets numbered even though they have names?
>
> Thanks again!
>
>
>

 
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
changing activesheet shapes label caption baha17@gmail.com Microsoft Excel Programming 1 31st Aug 2011 01:58 AM
Use a password in VBA ActiveSheet.protect & ActiveSheet.unprotect? Jim K. Microsoft Excel Programming 2 2nd Jun 2008 08:09 PM
Copying new activesheet after other activesheet is hidden? Simon Lloyd Microsoft Excel Programming 1 20th Jun 2006 10:02 AM
Get ActiveSheet name in VB Geoff Lambert Microsoft Excel Programming 1 8th Oct 2004 02:25 PM
ActiveSheet.Name? Andrew Stedman Microsoft Excel Programming 5 30th Jul 2003 01:17 PM


Features
 

Advertising
 

Newsgroups
 


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