简体   繁体   中英

Sorting dynamic ranges on multiple excel sheets

When I run my macro it sorts either one tab or the other, not both.

I have a workbook in which I am importing data from other two other workbooks that are auto-generated from a report. I copy the entire sheet from the report generated worksheet and paste it into an "Import" worksheet, and I do this for both reports. I want to take the information from the Imprt worksheets, and pull out only in information I need.

I am using a macro to pull the columns that have the information required because the columns are always static. For Example:

    Dim sourceColumn As Range, targetColumn As Range, Tracker As Workbook

    Set sourceColumn = Workbooks("Book1.xlsm").Worksheets("Import1").Columns("AZ")
    Set targetColumn = Workbooks("Book1.xlsm").Worksheets("Macro1").Columns("A")

    sourceColumn.Copy Destination:=targetColumn

I repeat this code snippet until I have all of the information I want from Import1 in Macro1, and all the information I want from Import2 to Macro2, and all in the proper order (Name first, and then additional information from there). This works perfect.

Next, I want to sort each Macro worksheet so that all of the information is sorted by the names in Column A, alphabetically. Since the ranges will not always be the same length I am using the following code to set a Dynamic Range, account for the headers, sort by the names in Column A. There are never blank columns or rows being pulled that need to be accounted for.

Dim sheet1 As Worksheet, StartCell1 As Range, Selection1 As Range
Dim sheet2 As Worksheet, StartCell2 As Range, Selection2 As Range

Set sheet1 = Workbooks("Book1.xlsm").Worksheets("Macro1")
Set StartCell1 = Range("A1")

Set Selection1 = StartCell1.CurrentRegion
Selection1.Select

Selection1.Sort Key1:=Range("A1"), Order1:=xlAscending, Header:=xlYes, Orientation:=xlSortColumns

Set sheet2 = Workbooks("Book1.xlsm").Worksheets("Macro2")
Set StartCell2 = Range("A1")

Set Selection2 = StartCell2.CurrentRegion
Selection2.Select

Selection2.Sort Key1:=Range("A1"), Order1:=xlAscending, Header:=xlYes, Orientation:=xlSortColumns

This always pulls and populates both Macro sheets as expected. The problem I run into is that when I run the Macro once, it sorts the first tab by name, and not the second. Then when I run the Macro again, it unsorts the first tab and sorts the second tab.

I plan to pull the data from both of these Macro tabs into a main Worksheet that will be presented to my end users, and running a CountIf and various other automated calculations to show them meaningful information pulled from these reports based on their names. I need both Macro sheets to sort by name and stay sorted so that I can automate these analytics.

How to I get this Macro to sort the first tab, and them move to the second tab and sort that one too?

Try

Option Explicit

Sub test()

    Dim sheet1 As Worksheet, StartCell1 As Range, Selection1 As Range
    Dim sheet2 As Worksheet, StartCell2 As Range, Selection2 As Range

    Set sheet1 = Workbooks("Book1.xlsb").Worksheets("Macro1")
    Set StartCell1 = sheet1.Range("A1")

    Set Selection1 = StartCell1.CurrentRegion

    Selection1.Sort Key1:=StartCell1, Order1:=xlAscending, Header:=xlYes, Orientation:=xlSortColumns

    Set sheet2 = Workbooks("Book1.xlsb").Worksheets("Macro2")
    Set StartCell2 = sheet2.Range("A1")

    Set Selection2 = StartCell2.CurrentRegion


    Selection2.Sort Key1:=StartCell2, Order1:=xlAscending, Header:=xlYes, Orientation:=xlSortColumns

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM