Quantcast
Channel: Excel IT Pro Discussions forum
Viewing all articles
Browse latest Browse all 11829

Excel Combining Macro

$
0
0

I have this macro that takes data from several sheets and combines it into one sheet. The problem I have with it is once the sheet is created, it needs to be added to my data model for some pivoting. Problem is, this code doesn't update, but rather recreates a new sheet every time. I am not a coder so Im not sure how to make this update an existing sheet.  Secondary to that it doesn't stop when it reaches the last cell with data. I have to pick a selection of cells so it currently looks in the first 50 cells. I mean I don't mind it returning blank rows, its just not ideal.  Any help would be greatly appreciated.

Sub CopyDataWithoutHeaders()
    Dim sh As Worksheet
    Dim DestSh As Worksheet
    Dim Last As Long
    Dim CopyRng As Range

    With Application
        .ScreenUpdating = False
        .EnableEvents = False
    End With

    'Delete the sheet "All Software" if it exist
    Application.DisplayAlerts = False
    On Error Resume Next
    ActiveWorkbook.Worksheets("All Software").Delete
    On Error GoTo 0
    Application.DisplayAlerts = True

    'Add a worksheet with the name "All Software"
    Set DestSh = ActiveWorkbook.Worksheets.Update
    DestSh.Name = "All Software"

    'loop through all worksheets and copy the data to the DestSh
    For Each sh In ActiveWorkbook.Worksheets
        If IsError(Application.Match(sh.Name, _
    Array(DestSh.Name, "Product Totals", "Devices", "Charts", "BTI Suite", "License POs"), 0)) Then

            StartRow = 2
           
            'Find the last row with data on the DestSh and sh
            Last = LastRow(DestSh)
                              
           
            'Fill in the range that you want to copy
             Set CopyRng = sh.Range("A1:C50")
            
            'Test if there enough rows in the DestSh to copy all the data
            If Last + CopyRng.Rows.Count > DestSh.Rows.Count Then
                MsgBox "There are not enough rows in the Destsh"
                GoTo ExitTheSub
            End If

            'This example copies values/formats, if you only want to copy the
            'values or want to copy everything look at the example below this macro
         CopyRng.Copy
            With DestSh.Cells(Last + 1, "A")
                .PasteSpecial xlPasteValues
                .PasteSpecial xlPasteFormats
                Application.CutCopyMode = False
            End With


            'Optional: This will copy the sheet name in the H column
            DestSh.Cells(Last + 1, "D").Resize(CopyRng.Rows.Count).Value = sh.Name
End If
       
    Next

ExitTheSub:

    Application.Goto DestSh.Cells(1)

    'AutoFit the column width in the DestSh sheet
    DestSh.Columns.AutoFit

    With Application
        .ScreenUpdating = True
        .EnableEvents = True
    End With
End Sub
Function LastRow(sh As Worksheet)
    On Error Resume Next
    LastRow = sh.Cells.Find(What:="*", _
                            After:=sh.Range("A1"), _
                            Lookat:=xlPart, _
                            LookIn:=xlFormulas, _
                            SearchOrder:=xlByRows, _
                            SearchDirection:=xlPrevious, _
                            MatchCase:=False).Row
    On Error GoTo 0
End Function


Function LastCol(sh As Worksheet)
    On Error Resume Next
    LastCol = sh.Cells.Find(What:="*", _
                            After:=sh.Range("A1"), _
                            Lookat:=xlPart, _
                            LookIn:=xlFormulas, _
                            SearchOrder:=xlByColumns, _
                            SearchDirection:=xlPrevious, _
                            MatchCase:=False).Column
    On Error GoTo 0
End Function

Sub CombineNoHeaders()

End Sub


Viewing all articles
Browse latest Browse all 11829

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>