Make the current worksheet the active sheet.

JohnZ1156

Board Regular
Joined
Apr 10, 2021
Messages
160
Office Version
  1. 2021
Platform
  1. Windows
I have a macro that I would like to be able to run on different worksheets. I placed a "button" on Sheet1 and assigned this macro:
Problem is, I'd like to place the same button on Sheet2, Sheet3, etc. and run the same macro, except on the current sheet, not always on Sheet1.
I know the "fix" is somewhere in the line Set ws = Worksheets("Sheet1") but I don't know how to "fix" it.

VBA Code:
Option Explicit
Sub L_to_R()
    Application.ScreenUpdating = False
    Dim ws As Worksheet, c As Range
    Set ws = Worksheets("Sheet1")
    For Each c In ws.Range("A2", Cells(Rows.Count, "A").End(xlUp))
        If IsDate(c) Then c.Offset(, 1).Resize(, 5).Sort _
        Key1:=c, Order1:=xlAscending, Orientation:=xlLeftToRight
        ws.Sort.SortFields.Clear
    Next c
    Application.ScreenUpdating = True
End Sub
 
If someone can explain to me why this macro would not work on data formatted as an Excel Table, but did work when the same data was not formatted as an Excel Table, I'd love to understand.
I haven't tested it but probably your range is including cells outside of the range of the Table, a Table is an object not a format and so it breaks when you have cells that are both outside and within the Table.

Rory's post about sorting a range by a cell that is not part of the range still stands for post 3
 
Last edited:
Upvote 0

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
If someone can explain to me why this macro would not work on data formatted as an Excel Table
You can't sort left to right in a table, and you can't sort only part of a table.
 
Upvote 0
Solution

Forum statistics

Threads
1,215,069
Messages
6,122,952
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