+ Reply to Thread
Results 1 to 15 of 15

VBA Code to Get data from the other sheet and sort it

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    02-24-2015
    Location
    Egypt
    MS-Off Ver
    Office 365 ProPlus
    Posts
    774

    VBA Code to Get data from the other sheet and sort it

    Hello Everybody,
    Could you help me in my question, please?
    The file is about history of material in the store
    The file contains a code that gets data from "Current Material" sheet and put it in "Report" Sheet and sort it.
    But recently the code doesnot work well because the data is not organized ,run the code and go to the end of Report sheet.
    Note:The code is called "Getpiles" in module 1
    please help me
    Attached Files Attached Files
    Last edited by leprince2007; 01-01-2022 at 12:20 PM.

  2. #2
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,088

    Re: VBA Code to Get data from the other sheet and sort it

    The macro is using column HB but today there is nothing inside this column.
    Which column should be used ?
    - Battle without fear gives no glory - Just try

  3. #3
    Forum Contributor
    Join Date
    02-24-2015
    Location
    Egypt
    MS-Off Ver
    Office 365 ProPlus
    Posts
    774

    Re: VBA Code to Get data from the other sheet and sort it

    The code is called "Getpiles" in module 1 not macro1

  4. #4
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,088

    Re: VBA Code to Get data from the other sheet and sort it

    To help can you explain what the code is doing, what is the final purpose ?

  5. #5
    Forum Contributor
    Join Date
    02-24-2015
    Location
    Egypt
    MS-Off Ver
    Office 365 ProPlus
    Posts
    774

    Re: VBA Code to Get data from the other sheet and sort it

    The file is about store history of piles (material ,company and its start and end).
    "Current material" sheet:
    ABC Columns are fixed and every 4 columns after it are repeated.
    The code copies (ABC Columns & every 4 columns after it) from "Current Material" sheet to report sheet and then sort it by A column then f column.
    for example:
    1-ABC &DEFG
    2-then ABC &HIJK
    and so on
    In order to see the code results,press run code button in "Report" sheet.The data in first rows are good but at the bottom of sheet aren`t.
    please help me

  6. #6
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,088

    Re: VBA Code to Get data from the other sheet and sort it

    See here new code.
    Double check if the result is OK .. it is so different to the original one ...!
    Sub Getpiles2()
    
    Dim qt As Long, fil As Long, col1 As Long, x As Long, re As Long, lr As Long, llr As Long, mr As Range
    Dim ColSt As Long, ColEnd As Long
    Dim WkRg1 As Range, WkRg2 As Range
    'Begin Optimize
        On Error GoTo enda:
        Application.ScreenUpdating = False
        Application.EnableEvents = False
        Application.Calculation = xlCalculationManual
        'End Optimize
        
        ActiveSheet.UsedRange.Offset(1).Clear
        'Range("A1").CurrentRegion.Offset(1).Clear
        With Sheets("Current material")
            .Cells.UnMerge
            fil = 7
            qt = Application.CountIf(.Range("2:2"), "Finished Date") - 1
            .Range("A1").CurrentRegion.AutoFilter fil, Criteria1:="<>"
            lr = .Range("A" & Rows.Count).End(xlUp).Row
            Set WkRg1 = .Range("A3:C" & lr)
            For x = 1 To qt
                ColSt = 4 + (x - 1) * 4
                ColEnd = ColSt + 3
                Set WkRg2 = Range(.Cells(3, ColSt), .Cells(lr, ColEnd))
                llr = Range("A" & Rows.Count).End(xlUp).Row + 1
                WkRg1.Copy Range("A" & llr)
                WkRg2.Copy Range("D" & llr)
            Next
        End With
        
        lr = Range("A" & Rows.Count).End(xlUp).Row
        Range("G1:G" & lr).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
        
        Range("A1").CurrentRegion.Offset(2).Interior.Color = vbWhite
        lr = Range("A" & Rows.Count).End(xlUp).Row
        Set mr = ActiveSheet.Range("A2", "G" & lr)
        mr.Sort Key1:=Range("B2", "B" & lr), Order1:=xlAscending, key2:=Range("C2"), order2:=xlAscending, Header:=xlNo
        
        'Begin Optimize
    enda:
        Application.EnableEvents = True
        Application.ScreenUpdating = True
        Application.Calculation = xlCalculationAutomatic
        'End Optimize
        MsgBox "Job Done", vbExclamation, "INFORMATION"
        End Sub
    Last edited by PCI; 01-02-2022 at 09:53 AM.

  7. #7
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,088

    Re: VBA Code to Get data from the other sheet and sort it

    A new code to reduce file size
    Option Explicit
    
    
    Sub Getpiles2()
    
    Dim qt As Long, fil As Long, col1 As Long, x As Long, re As Long, lr As Long, llr As Long, mr As Range
    Dim ColSt As Long, ColEnd As Long
    Dim WkRg1 As Range, WkRg2 As Range
    'Begin Optimize
        On Error GoTo enda:
        Application.ScreenUpdating = False
        Application.EnableEvents = False
        Application.Calculation = xlCalculationManual
        'End Optimize
        
        ActiveSheet.UsedRange.Offset(1).Clear
        'Range("A1").CurrentRegion.Offset(1).Clear
        With Sheets("Current material")
            .Cells.UnMerge
            fil = 7
            qt = Application.CountIf(.Range("2:2"), "Finished Date") - 1
            .Range("A1").CurrentRegion.AutoFilter fil, Criteria1:="<>"
            lr = .Range("A" & Rows.Count).End(xlUp).Row
            Set WkRg1 = .Range("A3:C" & lr)
            For x = 1 To qt
                ColSt = 4 + (x - 1) * 4
                ColEnd = ColSt + 3
                Set WkRg2 = Range(.Cells(3, ColSt), .Cells(lr, ColEnd))
                llr = Range("A" & Rows.Count).End(xlUp).Row + 1
                WkRg1.Copy Range("A" & llr)
                WkRg2.Copy Range("D" & llr)
            Next
        End With
        
        lr = Range("A" & Rows.Count).End(xlUp).Row
        Range("G1:G" & lr).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
        
        ActiveSheet.UsedRange.Offset(1).Interior.Color = vbWhite
        lr = Range("A" & Rows.Count).End(xlUp).Row
        Set mr = ActiveSheet.Range("A2", "G" & lr)
        mr.Sort Key1:=Range("B2", "B" & lr), Order1:=xlAscending, key2:=Range("C2"), order2:=xlAscending, Header:=xlNo
        
        'Begin Optimize
    enda:
        Application.EnableEvents = True
        Application.ScreenUpdating = True
        Application.Calculation = xlCalculationAutomatic
        'End Optimize
        MsgBox "Job Done", vbExclamation, "INFORMATION"
        End Sub

  8. #8
    Forum Contributor
    Join Date
    02-24-2015
    Location
    Egypt
    MS-Off Ver
    Office 365 ProPlus
    Posts
    774

    Re: VBA Code to Get data from the other sheet and sort it

    The code works fine but the following line deletes empty rows and this result to reduce the range of any formula that depend on this sheet.
    Quote Originally Posted by PCI View Post
    Range("G1:G" & lr).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
    Please I need to fix this problem

  9. #9
    Forum Contributor
    Join Date
    02-24-2015
    Location
    Egypt
    MS-Off Ver
    Office 365 ProPlus
    Posts
    774

    Re: VBA Code to Get data from the other sheet and sort it

    Please help
    bump.....bump

  10. #10
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: VBA Code to Get data from the other sheet and sort it

    1) if you want to copy/paste
    Sub Getpiles()
        Dim rng As Range, r As Range, myArea As Range, LastR As Range
        Application.ScreenUpdating = False
        Set rng = Sheets("current material").Cells(1).CurrentRegion
        rng.Parent.AutoFilterMode = False
        Set myArea = rng.Rows(1).SpecialCells(2, 2)
        With Sheets("report")
            .[a3].CurrentRegion.Clear
            Set LastR = .[a3]
            For Each r In myArea
                If r <> "Errors" Then
                    rng.AutoFilter r.Column, "<>"
                    Union(rng.Offset(2).Columns("a:c"), rng.Offset(2).Columns(r.Column).Resize(, 4)).Copy LastR
                    rng.AutoFilter
                    Set LastR = .Range("a" & Rows.Count).End(xlUp)(2)
                End If
            Next
            .[a3].CurrentRegion.Sort .[b3], 1, , .[c3], 1
        End With
        Application.ScreenUpdating = True
    End Sub
    2) Using array
    Sub test()
        Dim a, b, i As Long, ii As Long, iii As Long, n As Long
        a = Sheets("current material").Cells(1).CurrentRegion.Value
        ReDim b(1 To UBound(a, 1) * UBound(a, 2), 1 To 7)
        With Sheets("report")
            .[a3].CurrentRegion.ClearContents
            For i = 3 To UBound(a, 1)
                a(i, 1) = "'" & a(i, 1)
                For ii = 4 To UBound(a, 2) Step 4
                    If a(i, ii) <> "" Then
                        n = n + 1
                        For iii = 1 To 3
                            b(n, iii) = a(i, iii)
                        Next
                        For iii = 4 To 7
                            b(n, iii) = a(i, ii + iii - 4)
                        Next
                    End If
                Next
            Next
            .[a3].Resize(n, 7) = b
        End With
    End Sub

  11. #11
    Forum Contributor
    Join Date
    02-24-2015
    Location
    Egypt
    MS-Off Ver
    Office 365 ProPlus
    Posts
    774

    Re: VBA Code to Get data from the other sheet and sort it

    Thanks jindon for your answer.
    I prefer using arrays because they are faster but the code result contain repeated data:
    the code should ignore last set of columns in "GN:GQ" from "Current material " sheet because they contain an equation to get last data in every pile.
    Note: Columns "GN:GQ" are not fixed because I may add more columns before them. So I need the code to ignore last 4 columns only
    thanks in advance

  12. #12
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: VBA Code to Get data from the other sheet and sort it

    Add one line in bold
                a(i, 1) = "'" & a(i, 1)
                For ii = 4 To UBound(a, 2) Step 4
                    If a(1, ii) = "Current" Then Exit For
                    If a(i, ii) <> "" Then

  13. #13
    Forum Contributor
    Join Date
    02-24-2015
    Location
    Egypt
    MS-Off Ver
    Office 365 ProPlus
    Posts
    774

    Re: VBA Code to Get data from the other sheet and sort it

    Thanks jindon for your quick answer.but the code doesnot get the data in last row (pile "14-9") from "Current material" sheet
    Please fix it
    Thanks in advance

  14. #14
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: VBA Code to Get data from the other sheet and sort it

    I've got one line of 14-9 at the bottom of result.

  15. #15
    Forum Contributor
    Join Date
    02-24-2015
    Location
    Egypt
    MS-Off Ver
    Office 365 ProPlus
    Posts
    774

    Re: VBA Code to Get data from the other sheet and sort it

    Sorry it works
    thank you very much
    Solved

+ 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] Get Sheet Names - Remove Sort from this code
    By g1terra in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 08-14-2018, 12:07 AM
  2. [SOLVED] vba code to sort data in two sheets and copy and paste sort data in output sheet
    By UPA in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-03-2017, 05:13 AM
  3. [SOLVED] Refer to Code Name of Sheet not Name of sheet within sort
    By forrestgump1980 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-20-2016, 11:29 AM
  4. Code For A Custom Sort Using A List On Another Sheet
    By rockyw in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-06-2016, 08:51 PM
  5. [SOLVED] user form tab index and sort code for sheet 1
    By mezza89 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-07-2014, 08:58 AM
  6. [SOLVED] How to sort the data by item code first, then sort them by date?
    By Zecond in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 08-04-2013, 01:55 AM
  7. help with vba code that will run a macro that will sort by name and create a new sheet
    By robinwoodard in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-25-2012, 01:04 AM

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