#1   Report Post  
gbeard
 
Posts: n/a
Default Macro Button

I have placed a macro button on my sheet and I would like it to sort A2:P150
of SHEET2 through SHEET7. I would like it to sort the data on those sheets
by the column that is called in cell C3 of SHEET1.
Is this possible?
BTW, I'm a recreational user of VB, not a programmer. So, simply answering
yes probably won't help me much :-)


--
Gary (I know there's an Excel Programming group that this might be better
posted in, but I enjoy working with the people in this group and thought I'd
post it here first) Beard


  #2   Report Post  
Bernie Deitrick
 
Posts: n/a
Default

Gary,

Yes. Assuming: your headers are in row 2, the value in cell C3 of SHEET1 is
numeric (3 for column C, 5 for E, etc.), and you want to sort ascending, try
this:

Sub GarySheetSorter()
Dim i As Integer
For i = 2 To 7
Worksheets("SHEET" & i).Range("A2:P150").Sort _
Key1:=Range("A2").Item(1, Worksheets("SHEET1").Range("C3").Value), _
Order1:=xlAscending, Header:=xlYes, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
Next i
End Sub

P.S. The "people in this group" are the same as the people in the
programming group: we just tend to be nicer over here ;-)

HTH,
Bernie
MS Excel MVP


"gbeard" wrote in message
.. .
I have placed a macro button on my sheet and I would like it to sort

A2:P150
of SHEET2 through SHEET7. I would like it to sort the data on those

sheets
by the column that is called in cell C3 of SHEET1.
Is this possible?
BTW, I'm a recreational user of VB, not a programmer. So, simply

answering
yes probably won't help me much :-)


--
Gary (I know there's an Excel Programming group that this might be better
posted in, but I enjoy working with the people in this group and thought

I'd
post it here first) Beard




  #3   Report Post  
gbeard
 
Posts: n/a
Default

Bernie,
I get the following error when I run this:

"runtime error 9"
"subscript out of range"

So, I changed the following, but get the same results.
My headers are in row 1, so I changed the macro to A1:P150. I also changed
the Key1:=Range("A2") to Key1:=Range("A1").
I also had 9 sheets so I changed the For i=2 to 7 to For i=2 to 8.

Any ideas what might be amiss?

Thanks for your help,
Gary Beard


  #4   Report Post  
Bernie Deitrick
 
Posts: n/a
Default

Gary,

Any ideas what might be amiss?


Yeah, I'm lazy, and stupid, and forgot about needing to fully qualify the
key range. Use this, fully tested:

Sub GarySheetSorter2()
Dim i As Integer
For i = 2 To 8
With Worksheets("SHEET" & i)
.Range("A1:P150").Sort _
Key1:=.Range("A1").Item(1, Worksheets("SHEET1").Range("C3").Value), _
Order1:=xlAscending, Header:=xlYes, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
End With
Next i
End Sub


HTH,
Bernie
MS Excel MVP


"gbeard" wrote in message
. ..
Bernie,
I get the following error when I run this:

"runtime error 9"
"subscript out of range"

So, I changed the following, but get the same results.
My headers are in row 1, so I changed the macro to A1:P150. I also

changed
the Key1:=Range("A2") to Key1:=Range("A1").
I also had 9 sheets so I changed the For i=2 to 7 to For i=2 to 8.

Any ideas what might be amiss?

Thanks for your help,
Gary Beard




  #5   Report Post  
gbeard
 
Posts: n/a
Default

Bernie,
I'm still getting the same error message and the debugger is highlighting
the line:

With Worksheets("SHEET" & i)


Gary Beard




  #6   Report Post  
Bernie Deitrick
 
Posts: n/a
Default

Gary,

Are your sheets named exactly (no extra spaces)

SHEET2, SHEET3, SHEET4, SHEET5, SHEET6, SHEET7, SHEET8?

HTH,
Bernie
MS Excel MVP


"gbeard" wrote in message
. ..
Bernie,
I'm still getting the same error message and the debugger is highlighting
the line:

With Worksheets("SHEET" & i)


Gary Beard




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
2 part question - macro / command button John Excel Discussion (Misc queries) 3 April 16th 05 09:00 PM
delete a macro button is grayed out. Stan at University of Illinois Excel Worksheet Functions 1 March 10th 05 10:28 PM
Assigning a macro to a "button" Yvon Excel Discussion (Misc queries) 6 February 8th 05 10:58 PM
How to assign a macro to a commnd button Mickey Blue Eyes Excel Discussion (Misc queries) 2 January 4th 05 02:15 PM
assign a macro to a control button ewan72 Excel Discussion (Misc queries) 1 December 3rd 04 02:04 PM


All times are GMT +1. The time now is 05:32 AM.

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"