+ Reply to Thread
Results 1 to 6 of 6

Sorting Active Page in a workbook with multiple pages and code quits working

  1. #1
    Registered User
    Join Date
    11-22-2017
    Location
    Texas, United States
    MS-Off Ver
    2016
    Posts
    5

    Sorting Active Page in a workbook with multiple pages and code quits working

    Private Sub Worksheet_Calculate()

    Dim Xrg As Range
    Set Xrg = Worksheets("Monday").Range("F4:F50")

    If Not Intersect(Xrg, Worksheets("Monday").Range("F4:F50")) Is Nothing
    Then

    Application.Run "DriverSchedule.xlsm!Sort1"

    End If
    Set Xrg = Nothing
    End Sub
    I am checking for any change to the F column on sheet1″Monday” if a change happens than I sort using this code: Sub Sort1()

    Range("N3:O50").Select
    Selection.ClearContents
    Range("E3:F50").Select
    Selection.Copy
    Range("N3").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
    Application.CutCopyMode = False
    ActiveWorkbook.Worksheets("Monday").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Monday").Sort.SortFields.Add Key:=Range("O4:O50") _
    , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Monday").Sort
    .SetRange Range("N3:O50")
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
    End With
    Range("A4").Select
    End Sub
    This code is located in Module1 and sorts perfectly until I add the next sheets code

    Private Sub Worksheet_Calculate()

    Dim Xrg As Range
    Set Xrg = Worksheets("Tuesday").Range("F4:F50")

    If Not Intersect(Xrg, Worksheets("Tuesday").Range("F4:F50")) Is Nothing
    Then

    Application.Run "DriverSchedule.xlsm!Sort2"

    End If
    Set Xrg = Nothing
    End Sub
    Now this code is suppose to be looking for a change to column F of sheet2″Tuesday” than if there is a change it calls Module2″sort2″

    Sub Sort2()

    Range("N3:O50").Select
    Selection.ClearContents
    Range("E3:F50").Select
    Selection.Copy
    ActiveWindow.SmallScroll Down:=-18
    Range("N3").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
    Application.CutCopyMode = False
    ActiveWorkbook.Worksheets("Tuesday").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Tuesday").Sort.SortFields.Add Key:=Range("O4:O50") _
    , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Tuesday").Sort
    .SetRange Range("N3:O50")
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
    End With
    Range("A4").Select
    End Sub
    Which than sorts sheet2″Tuesday” column F and works fine also.

    Now the problem is that sheet1″Monday” no longer sorts. I have six sheets to represent Monday through Saturday and I need to sort if there is a change detected in column F but I cant seem to get all the sheets to sort if I add code under the sheet windows and even if I make separate modules for each sheet.

    I need to detect a change in column F on whatever day(sheet) I am working on than copy and sort two columns(Columns E and F) to another location on that same sheet.

    Any ideas?

    So its like the code is crosstalking among the pages. I can sort on which ever page I last add code but the code quits working on all previous pages.

  2. #2
    Forum Expert
    Join Date
    10-06-2017
    Location
    drevni ruchadlo
    MS-Off Ver
    old
    Posts
    2,151

    Re: Sorting Active Page in a workbook with multiple pages and code quits working

    Gosh, there are terrible rains in Texas, some sort of flood have washed away all formatting of the vba code

    Check if it (below) will work according to your expectations:

    1. Macro "Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)" paste in "ThisWorkbook" module

    Please Login or Register  to view this content.
    2. Macro "Sub Sort(shtNme As String)" paste in "Module1" module

    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    11-22-2017
    Location
    Texas, United States
    MS-Off Ver
    2016
    Posts
    5

    Re: Sorting Active Page in a workbook with multiple pages and code quits working

    Good Morning

    Thank you for making me laugh with your "Texas rains" comment and point well taken.

    I have copied and pasted the code to the proper locations.
    I than closed and saved the workbook.
    When I reopen the workbook and try to run the code it doesn't run.
    I than tried to step through the code and it won't do that either.
    It only brings up a marco window which has no marcos in it.
    The code will compile with out a problem but if it wont allow me to step through the code. I do not have a clue how to debug this issue.
    I am using Excel 2016

    Once again thanks for your help

  4. #4
    Registered User
    Join Date
    11-22-2017
    Location
    Texas, United States
    MS-Off Ver
    2016
    Posts
    5

    Re: Sorting Active Page in a workbook with multiple pages and code quits working

    I have figured out that the code is running but it is skipping over the Call sort line of code. not sure how to fix

  5. #5
    Registered User
    Join Date
    11-22-2017
    Location
    Texas, United States
    MS-Off Ver
    2016
    Posts
    5

    Re: Sorting Active Page in a workbook with multiple pages and code quits working

    Please Login or Register  to view this content.
    This seems to fix the problem
    Last edited by Daisy509th; 11-27-2017 at 02:41 PM.

  6. #6
    Forum Expert
    Join Date
    10-06-2017
    Location
    drevni ruchadlo
    MS-Off Ver
    old
    Posts
    2,151

    Re: Sorting Active Page in a workbook with multiple pages and code quits working

    1. Try this example of a workbook below
    2. I used "F4:F50" range because you had it in your macro, but you can type another one
    Attached Files Attached Files

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] vba code from closed workbook to update active sheet not working as requested
    By JEAN1972 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-22-2017, 09:35 AM
  2. Replies: 2
    Last Post: 05-17-2016, 12:23 PM
  3. Replies: 0
    Last Post: 10-19-2015, 04:35 PM
  4. Replies: 0
    Last Post: 07-30-2014, 11:25 AM
  5. [SOLVED] Code to detect previous active workbook instead of current active workbook
    By kosherboy in forum Excel Formulas & Functions
    Replies: 10
    Last Post: 02-25-2014, 01:58 AM
  6. Replies: 0
    Last Post: 06-12-2013, 07:34 PM
  7. Macro quits working after any error
    By Rick_Stanich in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-03-2009, 11:59 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1