VBA - Trigger a macro when ANY cell in column A is selected

tlc_in_OK

Board Regular
Joined
Jun 27, 2011
Messages
56
I haven't found exactly what I need on the boards, so hope someone can help.

I have a macro that refreshes a dataset from SQL, then creates a new sheet with some of that data placed and formatted as desired. My issue is that after that macro runs, I need to run another macro when any cell in column A is selected. I could do this normally by using Worksheet_SelectionChange(ByVal Target As Range), however, since the data gets inserted and changed in my worksheet when refreshing it, this is triggering the macro without actually selecting an individual cell.

So my questions are. . .how can I make a macro run by selecting a single cell, but ONLY make that possible after the data is refreshed and formatted with my first macro? And how do I make any cell in column A the trigger for the macro, instead of a specific cell?

Thanks in advance for any assistance.
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
One way to do what you have asked for is to use a global variable as a "signaling device" between the two macros. The declaration for the global variable would be done at the beginning of the Module containing your two macros on a line all by itself... it is NOT placed inside any Sub or Function procedures. Then you have the first macro set it to True and have the second macro test it and reset it to False as the end. The generalize structure of what I am talking about would look like this...

Code:
Dim OkayToRun As Boolean

Sub FirstMacro()
  '
  '  Your existing code
  '
  OkayToRun = True
End Sub

Sub SecondMacro()
  If Not OkayToRun Then Exit Sub
  '
  '  Your existing code
  '
  OkayToRun = False
End Sub
Just make sure each macro executes that last line of code before exiting (in case you have Exit Sub statements located internally within your code).
 
Upvote 0
Thanks Rick- I see where you're going, but I don't understand how that would allow me to "activate" all of the cells in column A of my sheet so that, on selection of any one cell, it would run the second macro.
 
Upvote 0
Thanks Rick- I see where you're going, but I don't understand how that would allow me to "activate" all of the cells in column A of my sheet so that, on selection of any one cell, it would run the second macro.
I thought you would use the Worksheet_SelectionChange event to control the selection, maybe using EnableEvents=False at the beginning of it and EnableEvents=True at the end of it and, I'm guessing, call you first macro from within it. Notice I said "thought", "maybe" and "guessing"... that is because you have not posted anywhere near enough information for us to be able to give you specifics. The best we can do is try to guide you to a solution.
 
Upvote 0
Ok, I guess I need to give additional information.

My workbook contains a hidden worksheet ("Hidden") and a visibile worksheet ("CurrentProjects"). Data is refreshed into the hidden worksheet, then my first macro takes some of the data, formats it and arranges it into the visible worksheet, shown below:
Excel 2010
ABCDE
2DetailNumberStreetAddressCityStateProvinceProjectName
3August
4332530data1city1GAproject1
5332121data2city2NJproject2
6332103data3city3MAproject3
7332341data4city4OKproject4
8332278data5city5ALproject5
9331940data6city6TXproject6
10
11September
12332344data9city9NMproject9
13332343data10city10AZproject10
14332300data11city11ARproject11

<colgroup><col><col><col><col><col><col></colgroup><thead>
</thead><tbody>
</tbody>
CurrentProjects

Now I need to select any of the data cells in column A and have that trigger a second macro that will create another sheet with the value of the selected cell as the worksheet name, then do some other calculations and such (which I can handle). I originally used the Worksheet_SelectionChange event, but it fired when my first macro put data into the sheet and messed everything up. I attempted to use Rick's solution, but it seemed to throw it into an endless loop and the second macro would never fire. I also need to run the second macro, then go back to the original sheet and run it again if desired by selecting a different cell.
 
Upvote 0

Forum statistics

Threads
1,215,066
Messages
6,122,948
Members
449,095
Latest member
nmaske

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top