Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 06-20-2014, 10:56 PM
shilabrow shilabrow is offline How To make any open sheet in WorkBK run Macro. Windows Vista How To make any open sheet in WorkBK run Macro. Office 2007
Advanced Beginner
How To make any open sheet in WorkBK run Macro.
 
Join Date: Apr 2014
Posts: 45
shilabrow is on a distinguished road
Default How To make any open sheet in WorkBK run Macro.


I recorded a macro in Sheet 1 and I have about 3 Sheets in the workbook. But I notice that I have to open Worksheet 1 for the macro to run else I get error. What can I do to make sure that once the workbook is open regardless of which sheet am in, my macro chosen will still run and execute properly. All help appreciated! Thanks much.
Reply With Quote
  #2  
Old 06-21-2014, 07:02 AM
whatsup whatsup is offline How To make any open sheet in WorkBK run Macro. Windows 7 64bit How To make any open sheet in WorkBK run Macro. Office 2010 32bit
Competent Performer
 
Join Date: May 2014
Posts: 137
whatsup will become famous soon enough
Default

Hi

Keep in mind: Recording a macro is a feature very helpful to get an idea what is required or to figure out methods, properties, ... whatsoever. But the recording also records all your selections you did, and at least that part you have to transform to another syntax.

Now I don't know your macro, but whatever you want the macro to perform you have to address explicity where it should be performed. The right thing would be, to enter a formula in Sheet2:
Code:
Sub EnterAFormula()
ThisWorkbook.Sheets("Sheet2").Range("C3").Formula = "=A1"
End Sub
Though it's not necessary to address "Thisworkbook" if your "Sheet2" is within the active workbook. The same applies to the address "Sheets("Sheet2"), if the Range is within the active sheet.
So probably by now you know what to change in your macro. If not, just post your code, and I will show it on your example.
Reply With Quote
  #3  
Old 06-22-2014, 09:13 PM
shilabrow shilabrow is offline How To make any open sheet in WorkBK run Macro. Windows Vista How To make any open sheet in WorkBK run Macro. Office 2007
Advanced Beginner
How To make any open sheet in WorkBK run Macro.
 
Join Date: Apr 2014
Posts: 45
shilabrow is on a distinguished road
Default

Thanks Whatsup. I am a bit confused with the entering a formula in sheet 2 and putting the code in. Like I said If sheet 1 is not open where the saved macro is (which I recorded), I get error - I am looking for a way to make the macro run regardless of which sheet is open. Your assistance in getting this right will be appreciated. Below is my sample code, kindly modify to make it clearer. Thanks

Sub Trials()
'
' Trials Macro
'

'
Selection.AutoFilter
ActiveSheet.ListObjects("Table_Query_from_MS_Acces s_Database").Range. _
AutoFilter Field:=6, Criteria1:="Owner"
ActiveSheet.ListObjects("Table_Query_from_MS_Acces s_Database").Range. _
AutoFilter Field:=13, Criteria1:="WA"
Cells.Select
Range("Table_Query_from_MS_Access_Database[[#Headers],[First Name]]").Activate
Selection.Copy
Sheets("Result").Select
Range("A1").Select
ActiveSheet.Paste
Range("A1").Select
End Sub
Reply With Quote
  #4  
Old 06-22-2014, 10:41 PM
whatsup whatsup is offline How To make any open sheet in WorkBK run Macro. Windows 7 64bit How To make any open sheet in WorkBK run Macro. Office 2010 32bit
Competent Performer
 
Join Date: May 2014
Posts: 137
whatsup will become famous soon enough
Default

QueryTables - hm I saw your other thread afterwards...

Anyway, try if this works:
Code:
Sub Trials()
'
' Trials Macro
'
 '
With Sheets(1)
    .Range("A3").AutoFilter 'replace Range A3 by the first cell you're autofilter starts
    .ListObjects("Table_Query_from_MS_Acces s_Database").Range. _
        AutoFilter Field:=6, Criteria1:="Owner"
    .ListObjects("Table_Query_from_MS_Acces s_Database").Range. _
        AutoFilter Field:=13, Criteria1:="WA"
    .Range("Table_Query_from_MS_Access_Database[[#Headers],[First Name]]").Copy _
        Destination:=Sheets("Result").Range("A1")
End With
End Sub
Basically it's important that you can't use ActiveSheet in a macro if you start the macro from another sheet - obvious isn't it? Therefore you have to refer explicitly to the sheets name, or as above the sheets position in your workbook.
Reply With Quote
  #5  
Old 06-22-2014, 11:44 PM
macropod's Avatar
macropod macropod is offline How To make any open sheet in WorkBK run Macro. Windows 7 32bit How To make any open sheet in WorkBK run Macro. Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Quote:
Originally Posted by whatsup View Post
Basically it's important that you can't use ActiveSheet in a macro if you start the macro from another sheet - obvious isn't it?
Unless, of course, your code activates the sheet concerned (e.g. ThisWorkBook.Sheets("MySheet").Activate). But even that requires the sheet to be named for activation. Not a recommended way to program - it's both inefficient and error-prone.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #6  
Old 06-23-2014, 07:14 PM
shilabrow shilabrow is offline How To make any open sheet in WorkBK run Macro. Windows Vista How To make any open sheet in WorkBK run Macro. Office 2007
Advanced Beginner
How To make any open sheet in WorkBK run Macro.
 
Join Date: Apr 2014
Posts: 45
shilabrow is on a distinguished road
Default

Thanks Whatsup - your bottom explanation spoke volume. I am just picking up with Macro and programming and thanks for Helping! I just named the sheet and referenced that, which helped.

Macropod - I appreciate your input.
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
How To make any open sheet in WorkBK run Macro. How to make an add-in/global macro hanvyj Excel Programming 4 12-14-2012 03:23 AM
Make row in one sheet equal column of another hypertyper Excel 6 01-31-2012 03:48 PM
confirmation popup on excel sheet macro mark-gabb Excel Programming 3 12-22-2011 01:16 AM
Debug for macro run through button only when sheet protected leahca Excel Programming 0 11-24-2011 04:47 AM
copy cell from sheet 2 to sheet 3 macro slipperyjim Excel Programming 1 02-18-2010 01:31 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 11:07 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft