current worksheet correct syntax

cthompson

Board Regular
Joined
Jan 31, 2011
Messages
80
Hi Everyone,

I am trying to create a button that will sort a specific area on a worksheet, there are approximately 140 identical tabs (worksheets) and I wanted to have a button invoke a macro to sort the same area on the current active worksheet.

When i use the macro recorder it always inserts the sheet that i created it on, but i want to modify the VBA to reference whatever sheet is currently being viewed. The button exist on all 140 sheets.

Below is the attempt to change the syntax to work with the current sheet, not just the one that I was using to record the macro.

Thanks again for the help.
Chris

Code:
Sub Macrotest()
'
' Macrotest Macro
'
Dim ssheet
ssheet = ActiveWorkbook.ActiveSheet()
'
    Range("B126:G166").Select
    ActiveWorkbook.Worksheets("ssheet").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("ssheet").Sort.SortFields.Add2 Key:=Range( _
        "D126:D166"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    With ActiveWorkbook.Worksheets("ssheet").Sort
        .SetRange Range("B126:G166")
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub
 
Last edited by a moderator:

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
30,523
Office Version
365
Platform
Windows
How about
Code:
Sub Macrotest()
    '
    ' Macrotest Macro
    '
    With ActiveSheet.Sort
        .SortFields.Clear
        .SortFields.Add2 Key:=Range("D126:D166"), SortOn:=xlSortOnValues, Order:=xlAscending, _
            DataOption:=xlSortNormal
        .SetRange Range("B126:G166")
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub
 

Forum statistics

Threads
1,078,435
Messages
5,340,250
Members
399,361
Latest member
Linford

Some videos you may like

This Week's Hot Topics

  • Problem with Radio Button's format control
    I am creating an employee evaluation template (a sample is below) Column A is the category Column B, C D, E and F will be ratings (unacceptable...
  • Last Display on userform to a Listbox
    [CODE=vba] lstdisplay.ColumnCount = 15 lstdisplay.RowSource = "A1:O600000" [/CODE] So when i do this it Displays everything on the sheet i am...
  • Rename and move files to a new location
    Dear all, I have an excel file with the following information. The actual file name is at column A but i want to rename it using the following...
  • Help with True/False Formula
    Hello! Am stumped how to fix this formula, in which my result returns 'True', but it should return False. =IF(AG2=True...
  • Clear extra characters from a provided range of cells
    Dear All, I have following code which gives me desired output to remove extra characters from a provided range. But it takes too much time when...
  • Help with Current and highest streaks
    Hi there, I've just joined the forum and this is my first post. I've already spent quite a bit of time searching the net and this forum for a...
Top