Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Automatically starting a macro


I have a macro I would like to run when a partiuclar sheet is selected
in a workbook but cant find where that is covered. I would also like
that same macro to run when the sheet is closed and another sheet is
opened. Is there such a command in Excel? TIA


--
moorso
------------------------------------------------------------------------
moorso's Profile: http://www.excelforum.com/member.php...fo&userid=8966
View this thread: http://www.excelforum.com/showthread...hreadid=380501

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Automatically starting a macro


Private Sub Worksheet_Activate()
'do your stuff
End Sub


'This is worksheet event code, which means that it needs to be
'placed in the appropriate worksheet code module, not a standard
'code module. To do this, right-click on the sheet tab, select
'the View Code option from the menu, and paste the code in.



--

HTH

RP
(remove nothere from the email address if mailing direct)


"moorso" wrote in
message ...

I have a macro I would like to run when a partiuclar sheet is selected
in a workbook but cant find where that is covered. I would also like
that same macro to run when the sheet is closed and another sheet is
opened. Is there such a command in Excel? TIA


--
moorso
------------------------------------------------------------------------
moorso's Profile:

http://www.excelforum.com/member.php...fo&userid=8966
View this thread: http://www.excelforum.com/showthread...hreadid=380501



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Automatically starting a macro


Thank you Bob. It does run the macro but I note one small problem.
.... I added a worksheet_deactivate event so that the same macro woul
run when another worksheet is selected. Only problem is, the macr
wont allow any other sheet to be selected. Any thoughts on how to ru
the macro and select another sheet at the same time? Thanks very muc
for the help

--
moors
-----------------------------------------------------------------------
moorso's Profile: http://www.excelforum.com/member.php...nfo&userid=896
View this thread: http://www.excelforum.com/showthread.php?threadid=38050

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Automatically starting a macro

If it is wanted for any sheet, use the workbook sheet select event


Private Sub Workbook_SheetActivate(ByVal Sh As Object)
'do your stuff
End Sub

'This is workbook event code.
'To input this code, right click on the Excel icon on the worksheet
'(or next to the File menu if you maximise your workbooks),
'select View Code from the menu, and paste the code


--

HTH

RP
(remove nothere from the email address if mailing direct)


"moorso" wrote in
message ...

Thank you Bob. It does run the macro but I note one small problem..
... I added a worksheet_deactivate event so that the same macro would
run when another worksheet is selected. Only problem is, the macro
wont allow any other sheet to be selected. Any thoughts on how to run
the macro and select another sheet at the same time? Thanks very much
for the help.


--
moorso
------------------------------------------------------------------------
moorso's Profile:

http://www.excelforum.com/member.php...fo&userid=8966
View this thread: http://www.excelforum.com/showthread...hreadid=380501



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Automatically starting a macro


Thanks again Bob. That also works, but again I have the same problem.
The macro that is called to perform when any of the sheets is selecte
first calls up the sheet on which the macro runs, then runs the macr
then that sheet remains as the active sheet. For example, the macr
that sorts the data for the vlookup function is on sheet "labor".
There are 3 other sheets, I'll call Sheet1, Sheet2 and Sheet3. No
when I click on say Sheet1, it goes to the "labor" sheet, runs the sor
macro, then stays right there. How can I make it so that it runs th
macro on "labor" sheet then returns to sheet1, the one I originall
selected? I do appreciate your help with this

--
moors
-----------------------------------------------------------------------
moorso's Profile: http://www.excelforum.com/member.php...nfo&userid=896
View this thread: http://www.excelforum.com/showthread.php?threadid=38050



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Automatically starting a macro

Moorso,

Normally if you want to work on another worksheet, you don't activate it,
you just reference that worksheet from your code. So for instance, if you
wanted to run a macro against Sheet2, you would use something like

With Worksheets("Sheet2")
.Range("A1").Value = 17
'etc
End With

using this method, Sheet2 does not have to be selected. Can we use an
approach like this, or are you using Activate to trigger the macro.

--

HTH

RP
(remove nothere from the email address if mailing direct)


"moorso" wrote in
message ...

Thanks again Bob. That also works, but again I have the same problem.
The macro that is called to perform when any of the sheets is selected
first calls up the sheet on which the macro runs, then runs the macro
then that sheet remains as the active sheet. For example, the macro
that sorts the data for the vlookup function is on sheet "labor".
There are 3 other sheets, I'll call Sheet1, Sheet2 and Sheet3. Now
when I click on say Sheet1, it goes to the "labor" sheet, runs the sort
macro, then stays right there. How can I make it so that it runs the
macro on "labor" sheet then returns to sheet1, the one I originally
selected? I do appreciate your help with this.


--
moorso
------------------------------------------------------------------------
moorso's Profile:

http://www.excelforum.com/member.php...fo&userid=8966
View this thread: http://www.excelforum.com/showthread...hreadid=380501



  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Automatically starting a macro


Bob, I am currently using the selection of a sheet to trigger the macr
on the "labor" sheet. I dont know how to run the macro on this shee
without first having the macro select the "labor" sheet. I would lov
to have the macro run without having to select this sheet if I knew ho
to do it.

In other words, I have a macro called crew_sort. This macro sorts th
crews in ascending order so that I can use the vlookup function on th
other workbook sheets to grab the information from the labor sheet.
Apparently vlookup has to work with sorted data in the ascending orde
for it to work properly. With the "Workbook_SheetActivate" yo
suggested, I now have it run the "crew_sort" macro which physicall
selects the "labor" sheet, runs the macro and sits right there. Th
outcome is I can't actually select the other sheets in the workboo
because the macro makes it stick on the "labor" sheet. I know thi
sounds confusing and I'll try to understand how to run a macro withou
actually going to the sheet it works on. I dont know how yet..:

--
moors
-----------------------------------------------------------------------
moorso's Profile: http://www.excelforum.com/member.php...nfo&userid=896
View this thread: http://www.excelforum.com/showthread.php?threadid=38050

  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Automatically starting a macro


Bob,

here is the macro that I want to run when the crew sheet is deselected
and any other sheet is selected. I have been calling this sheet
"labor" by mistake. It actually is called "crews".



Sub Sort_Crews()
'
Sheets("Crews").Select
Sheets("Crews").Unprotect

Sheets("Crews").Columns("S:AA").Copy
Range("AB1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone,
SkipBlanks _
:=False, Transpose:=False

Application.Goto Reference:="CREWSUM"
Selection.Sort Key1:=Range("AD3"), Order1:=xlAscending,
Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom,
_
DataOption1:=xlSortNormal
Selection.EntireColumn.Hidden = True



Application.CutCopyMode = False
Sheets("Crews").Protect DrawingObjects:=True, Contents:=True,
Scenarios:=True

End Sub


--
moorso
------------------------------------------------------------------------
moorso's Profile: http://www.excelforum.com/member.php...fo&userid=8966
View this thread: http://www.excelforum.com/showthread...hreadid=380501

  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Automatically starting a macro


Untested but use complete sheet references when working with your range
and you can avoid selecting the crews sheet.


Code:
--------------------
Sub Sort_Crews()
'
Sheets("Crews").Unprotect

With Sheets("Crews")
.Columns("S:AA").Copy
.Range("AB1").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False

Application.Goto Reference:="CREWSUM"
.Range("AB:AJ).Sort Key1:=.Range("AD3"), Order1:=xlAscending, _
Header:=xlGuess, OrderCustom:=1, MatchCase:=False, _
Lotation:=xlTopToBottom, DataOption1:=xlSortNormal _
Selection.EntireColumn.Hidden = True

Application.CutCopyMode = False
.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
End With

End Sub
--------------------


HTH


--
bhofsetz
------------------------------------------------------------------------
bhofsetz's Profile: http://www.excelforum.com/member.php...o&userid=18807
View this thread: http://www.excelforum.com/showthread...hreadid=380501

  #10   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Automatically starting a macro


I get a compile error....something is wrong with the code here....looks
to be this part of it....


Range("AB:AJ).Sort Key1:=.Range("AD3"), Order1:=xlAscending,
Header:=xlGuess, OrderCustom:=1, MatchCase:=False, _
Lotation:=xlTopToBottom, DataOption1:=xlSortNormal _
Selection.EntireColumn.Hidden = True

my head is starting to spin..:)


--
moorso
------------------------------------------------------------------------
moorso's Profile: http://www.excelforum.com/member.php...fo&userid=8966
View this thread: http://www.excelforum.com/showthread...hreadid=380501



  #11   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Automatically starting a macro


try putting the complete sheet reference before the sort key. I assumed
since it was within the With statement that it would pick that up but it
may not be in this case.

Add the red text to the existing line

Range("AB:AJ).Sort Key1:=Sheets("Crews").Range("AD3"),
Order1:=xlAscending

Give that a try and post back with results.


--
bhofsetz
------------------------------------------------------------------------
bhofsetz's Profile: http://www.excelforum.com/member.php...o&userid=18807
View this thread: http://www.excelforum.com/showthread...hreadid=380501

  #12   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Automatically starting a macro


No luck. Still getting a compile error. Could it have something to d
with the fact that the name "CREWSUM" is being called out in the middl
of the 'with' statement? I'm not real experienced at some of this a
you can probably tell

--
moors
-----------------------------------------------------------------------
moorso's Profile: http://www.excelforum.com/member.php...nfo&userid=896
View this thread: http://www.excelforum.com/showthread.php?threadid=38050

  #13   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Automatically starting a macro


"moorso" wrote in
message ...

Apparently vlookup has to work with sorted data in the ascending order
for it to work properly.


It doesn't need to be, you can use an extra parameter for unsorted data as
long as you don't want an approximate match.

=VLOOKUP(value, lookup_range, offset,False)


  #14   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Automatically starting a macro


What text is highlighted when you receive the compile error

--
bhofset
-----------------------------------------------------------------------
bhofsetz's Profile: http://www.excelforum.com/member.php...fo&userid=1880
View this thread: http://www.excelforum.com/showthread.php?threadid=38050

  #15   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Automatically starting a macro


I caught a couple of typos that I made, my appologies.

Try this:


Code:
--------------------
Sub Sort_Crews()
'
Sheets("Crews").Unprotect

With Sheets("Crews")
.Columns("S:AA").Copy
.Range("AB1").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False

Application.Goto Reference:="CREWSUM"
.Range("AB:AJ").Sort Key1:=.Range("AD3"), Order1:=xlAscending, _
Header:=xlGuess, OrderCustom:=1, MatchCase:=False, _
Lotation:=xlTopToBottom ', DataOption1:=xlSortNormal
.Range("AB:AJ").EntireColumn.Hidden = True

Application.CutCopyMode = False
.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
End With

End Sub
--------------------


--
bhofsetz
------------------------------------------------------------------------
bhofsetz's Profile: http://www.excelforum.com/member.php...o&userid=18807
View this thread: http://www.excelforum.com/showthread...hreadid=380501



  #16   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Automatically starting a macro


Nope....I'm at home and dont have the original code here, but have bee
trying it on some slightly different stuff. I wonder if the proble
might be that the code asks to select the named range "CREWSUM".
Wouldnt that necessarily take it to the sheet that the named range i
on which is the Crews sheet

--
moors
-----------------------------------------------------------------------
moorso's Profile: http://www.excelforum.com/member.php...nfo&userid=896
View this thread: http://www.excelforum.com/showthread.php?threadid=38050

  #17   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Automatically starting a macro


That is definitely a possibility. I'm not extreemly experienced wit
named ranges but I know you can use a named range from one worksheet o
another without having to switch sheets. What is the purpose of tha
line in your code? Have you tried commenting it out and run the cod
without that line

--
bhofset
-----------------------------------------------------------------------
bhofsetz's Profile: http://www.excelforum.com/member.php...fo&userid=1880
View this thread: http://www.excelforum.com/showthread.php?threadid=38050

  #18   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Automatically starting a macro


Bob Phillips Wrote:
"moorso" wrote in
message ...

Apparently vlookup has to work with sorted data in the ascending

order
for it to work properly.


It doesn't need to be, you can use an extra parameter for unsorted data
as
long as you don't want an approximate match.

=VLOOKUP(value, lookup_range, offset,False)


Oh baby, that just might do the trick! If I dont have to run a macro
to sort the data I should in business. I'll try it tomorrow on the
real spreadsheet, but I cant see why my problem isnt solved (the easier
way!). Thanks a million Bob and bhofsetz.


--
moorso
------------------------------------------------------------------------
moorso's Profile: http://www.excelforum.com/member.php...fo&userid=8966
View this thread: http://www.excelforum.com/showthread...hreadid=380501

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
Blank sheet does not automatically appear on starting Excel Andy Excel Discussion (Misc queries) 1 January 30th 09 02:39 PM
automatically open a specific worksheet when starting excel 2007 George3_1949 Excel Discussion (Misc queries) 1 November 9th 07 03:51 PM
Excel Launching and not automatically starting a new workbook joe Excel Discussion (Misc queries) 6 April 11th 07 08:26 PM
Starting a Makro automatically [email protected] Excel Discussion (Misc queries) 5 July 24th 06 12:19 PM
Automatically starting code Richard Excel Worksheet Functions 3 February 8th 06 02:33 AM


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