#1   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 36
Default VB Macro question

Dear experts,

I have the following macro to sort Cells.

--------------------------------------------------------
Sub Macro2()
'
' Macro2 Macro
'
' Keyboard Shortcut: Ctrl+x
'
Rows("4:385").Select
ActiveWorkbook.Worksheets("Aug").Sort.SortFields.C lear
ActiveWorkbook.Worksheets("Aug").Sort.SortFields.A dd
Key:=Range("A4:A385"), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("Aug").Sort.SortFields.A dd
Key:=Range("B4:B385"), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortTextAsNumbers
With ActiveWorkbook.Worksheets("Aug").Sort
.SetRange Range("A4:AD385")
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub
-------------------------------------

I would to change the constant "Aug" to the selected/active Sheet, so that I
wont have to change "Aug" all the time.

What should I change "Aug" to??

Thanks in advance.




  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 8,520
Default VB Macro question

Replace
Worksheets("Aug")

with
ActiveSheet

to look like the below for all lines

ActiveWorkbook.ActiveSheet.Sort.SortFields.Clear
OR
ActiveSheet.Sort.SortFields.Clear

If this post helps click Yes
---------------
Jacob Skaria


"apache007" wrote:

Dear experts,

I have the following macro to sort Cells.

--------------------------------------------------------
Sub Macro2()
'
' Macro2 Macro
'
' Keyboard Shortcut: Ctrl+x
'
Rows("4:385").Select
ActiveWorkbook.Worksheets("Aug").Sort.SortFields.C lear
ActiveWorkbook.Worksheets("Aug").Sort.SortFields.A dd
Key:=Range("A4:A385"), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("Aug").Sort.SortFields.A dd
Key:=Range("B4:B385"), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortTextAsNumbers
With ActiveWorkbook.Worksheets("Aug").Sort
.SetRange Range("A4:AD385")
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub
-------------------------------------

I would to change the constant "Aug" to the selected/active Sheet, so that I
wont have to change "Aug" all the time.

What should I change "Aug" to??

Thanks in advance.




  #3   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 9,101
Default VB Macro question

Since there may be a different number of rows in each worksheet this code is
simplier and more flexible.

Sub Macro2()
'
' Macro2 Macro
'
' Keyboard Shortcut: Ctrl+x
'
With ActiveWorkbook.ActiveSheet
LastRow = .Range("A" & Rows.count).end(xlup).row
if Lastrow = 4 then
.Rows("4:" & LastRow").Sort _
header:=xlguess, _
Key1:=Range("A4"), _
Order1:=xlAscending
end if
End With

End Sub


"apache007" wrote:

Dear experts,

I have the following macro to sort Cells.

--------------------------------------------------------
Sub Macro2()
'
' Macro2 Macro
'
' Keyboard Shortcut: Ctrl+x
'
Rows("4:385").Select
ActiveWorkbook.Worksheets("Aug").Sort.SortFields.C lear
ActiveWorkbook.Worksheets("Aug").Sort.SortFields.A dd
Key:=Range("A4:A385"), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("Aug").Sort.SortFields.A dd
Key:=Range("B4:B385"), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortTextAsNumbers
With ActiveWorkbook.Worksheets("Aug").Sort
.SetRange Range("A4:AD385")
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub
-------------------------------------

I would to change the constant "Aug" to the selected/active Sheet, so that I
wont have to change "Aug" all the time.

What should I change "Aug" to??

Thanks in advance.




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
Macro question rmcove Excel Discussion (Misc queries) 5 March 5th 09 03:01 PM
IF question or MACRO ? Which to use DMK Excel Worksheet Functions 2 September 17th 08 04:51 PM
MACRO QUESTION Eddy Stan Excel Worksheet Functions 2 February 28th 06 01:08 PM
Macro question Terry Bennett Excel Worksheet Functions 9 January 22nd 06 10:39 PM
Macro Question carl Excel Worksheet Functions 1 August 3rd 05 07:56 PM


All times are GMT +1. The time now is 02:49 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"