ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Link cell contents to a Macro? (https://www.excelbanter.com/excel-programming/371598-link-cell-contents-macro.html)

Dave

Link cell contents to a Macro?
 
I'd like to call a macro that sorts a list simply by clicking on a header cell.
Sort Key1 = the cell that the user clicks on.
Can one call a macro by clicking on a cell or the contents of a cell?
If so, how?
Thanks very much.


Die_Another_Day

Link cell contents to a Macro?
 
Yes, Right-Click the sheet in question. Select "View Code". Use this
event:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Address < Range("A1").Address Then 'Change A1 for your Cell
Exit Sub
End If
'Your Code to sort
End Sub

HTH

Charles Chickering

Dave wrote:
I'd like to call a macro that sorts a list simply by clicking on a header cell.
Sort Key1 = the cell that the user clicks on.
Can one call a macro by clicking on a cell or the contents of a cell?
If so, how?
Thanks very much.



Gary''s Student

Link cell contents to a Macro?
 
This can be accomplished by using a Worksheet_SelectionChange macro. This
macro will run whenever a given cell is selected.

If you are not familiar with even macros, see:



http://cpearson.com/excel/events.htm
--
Gary's Student


"Dave" wrote:

I'd like to call a macro that sorts a list simply by clicking on a header cell.
Sort Key1 = the cell that the user clicks on.
Can one call a macro by clicking on a cell or the contents of a cell?
If so, how?
Thanks very much.


Jim Thomlinson

Link cell contents to a Macro?
 
You could try something like this. This code is placed in the sheet and not
in a module where recorded macro's are. Right click the sheet tab in Excel
and select View Code. Paste the following:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Target, Range("B1:E1")) Is Nothing Then
Range("A1:E100").Sort Key1:=Target, Order1:=xlAscending, Header:=xlYes
End If
End Sub

When a cell B1:E1 is selected it sorts the range A1:E100
--
HTH...

Jim Thomlinson


"Dave" wrote:

I'd like to call a macro that sorts a list simply by clicking on a header cell.
Sort Key1 = the cell that the user clicks on.
Can one call a macro by clicking on a cell or the contents of a cell?
If so, how?
Thanks very much.


Dave

Link cell contents to a Macro?
 
Thank you very much. This works well.

"Jim Thomlinson" wrote:

You could try something like this. This code is placed in the sheet and not
in a module where recorded macro's are. Right click the sheet tab in Excel
and select View Code. Paste the following:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Target, Range("B1:E1")) Is Nothing Then
Range("A1:E100").Sort Key1:=Target, Order1:=xlAscending, Header:=xlYes
End If
End Sub

When a cell B1:E1 is selected it sorts the range A1:E100
--
HTH...

Jim Thomlinson


"Dave" wrote:

I'd like to call a macro that sorts a list simply by clicking on a header cell.
Sort Key1 = the cell that the user clicks on.
Can one call a macro by clicking on a cell or the contents of a cell?
If so, how?
Thanks very much.


Dave

Link cell contents to a Macro?
 
Thank you for the link. It was very helpful.

"Gary''s Student" wrote:

This can be accomplished by using a Worksheet_SelectionChange macro. This
macro will run whenever a given cell is selected.

If you are not familiar with even macros, see:



http://cpearson.com/excel/events.htm
--
Gary's Student


"Dave" wrote:

I'd like to call a macro that sorts a list simply by clicking on a header cell.
Sort Key1 = the cell that the user clicks on.
Can one call a macro by clicking on a cell or the contents of a cell?
If so, how?
Thanks very much.


Dave

Link cell contents to a Macro?
 
Thanks for the tip. Very helpful.

"Die_Another_Day" wrote:

Yes, Right-Click the sheet in question. Select "View Code". Use this
event:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Address < Range("A1").Address Then 'Change A1 for your Cell
Exit Sub
End If
'Your Code to sort
End Sub

HTH

Charles Chickering

Dave wrote:
I'd like to call a macro that sorts a list simply by clicking on a header cell.
Sort Key1 = the cell that the user clicks on.
Can one call a macro by clicking on a cell or the contents of a cell?
If so, how?
Thanks very much.





All times are GMT +1. The time now is 11:57 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com