Making the VBA code affect a specific range of pages

Saher Naji

Board Regular
Joined
Dec 19, 2019
Messages
76
Office Version
  1. 2013
Platform
  1. Windows
Hello,
I have this very simple code

VBA Code:
Sub SortByTime()
'
' SortByTime Macro
' Sort by Time
'
' Keyboard Shortcut: Ctrl+Shift+M
'
    Range("C4:J43").Select
    ActiveWindow.SmallScroll Down:=-36
    ActiveWorkbook.Worksheets("Jan_1").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Jan_1").Sort.SortFields.Add2 Key:=Range("G4:G43") _
        , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Jan_1").Sort
        .SetRange Range("C3:J43")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub

what I'm trying to do is make this code affect on 365 sheets, (not all of the workbook sheets) I have 30 sheets for each month,
I did not know how to edit this code,
I tried to add the sheets like this,
With ActiveWorkbook.Worksheets("Jan_1"+"Jan_2"+"Jan_3"+"Jan_4"+"Jan_5"+.... etc ).Sort

but it did not work

Thanks in advance
 

Attachments

  • sort.png
    sort.png
    70.8 KB · Views: 15
The error message in your video indicates the problem is still merged cells. What did the debug.print messages from the debug code I recommended in my last post tell you?
11-9-2022 8-42-34 AM.png


Are you aware of what the "immediate" window in the VBE debugger is? You were not using it in your video.
 
Upvote 0

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
1668010149869.png


Thanks you very much,
That's great, I found it, it's Feb_29 sheet, but it was hidden tab
Now I just need to stop moving to Feb_1 after clicking on the VBA button, I want to stay in the same sheet
1668010263955.png



You are amazing !
 

Attachments

  • 1668009939843.png
    1668009939843.png
    98.4 KB · Views: 10
Upvote 0
after clicking on the VBA button, I want to stay in the same sheet

You said in your original post that you wanted the macro to act on all the sheets. If you just want it to act on the active sheet. Then revert to something like this:
VBA Code:
Sub SortActiveSheet()
    Dim WS As Worksheet

    Set WS = ActiveSheet

    With WS.Sort
        If Not WS.Range("C3:J43").MergeCells Then
            .SortFields.Clear
            .SortFields.Add2 Key:=WS.Range("G4:G43"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
            .SetRange WS.Range("C3:J43")
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        Else
            MsgBox "Error - sort range contains merged cells", vbCritical, "Sorting worksheet: " & WS.Name
        End If
    End With
End Sub
 
Upvote 0
Solution
I used some parts from your last code, every thing is working well now
You are right, It's better to work on the active sheet one by one, not necessary to make the code run in all sheets each time
thank you very much

This is the updated code:

VBA Code:
Sub SortActiveSheet()
    Dim WS As Worksheet

    Set WS = ActiveSheet

    With WS.Sort
        If Not WS.Range("C4:J43").MergeCells Then
            .SortFields.Clear
            .SortFields.Add2 Key:=WS.Range("G3:G43"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
            .SetRange WS.Range("C3:J43")
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        Else
            MsgBox "Error - sort range contains merged cells", vbCritical, "Sorting worksheet: " & WS.Name
        End If
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,703
Messages
6,173,973
Members
452,540
Latest member
haasro02

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