+ Reply to Thread
Results 1 to 2 of 2

Combine 2 Macros to Consolidate Data from Multiple Sheets and then Delete Blank Rows

  1. #1
    Registered User
    Join Date
    07-24-2014
    Location
    Plano, Texas
    MS-Off Ver
    2013
    Posts
    19

    Question Combine 2 Macros to Consolidate Data from Multiple Sheets and then Delete Blank Rows

    Hello, this is my first time using a Macro and I am hoping to get some help to make it quicker/more efficient.

    I have multiple sheets that i am combining data from (if the sheet is not the destination sheet, is visible and starts with WBS)... unfortunately the sheets are templates so i have formulas down to row 204 so the macro copies all of the cells with formulas, which are returning "". The macro copies/pastes values and formats and deletes conditional formatting.

    Then.. I have another macro that goes through the new destination sheet and deletes all of the blank rows

    Is there a way to combine these so it can all be done with one button?

    Also, is there a way to get my column headers (only from the first sheet) -- i do not want to repeat column headers from each sheet, so for now i just skipped this - no column hearders on my data extract.


    Private Sub CommandButton1_Click()

    Dim Firstrow As Long
    Dim Lastrow As Long
    Dim Lrow As Long
    Dim CalcMode As Long
    Dim ViewMode As Long

    With Application
    CalcMode = .Calculation
    .Calculation = xlCalculationManual
    .ScreenUpdating = False
    End With

    'We use the ActiveSheet but you can replace this with
    'Sheets("MySheet")if you want
    With Sheets("Pricing")

    'We select the sheet so we can change the window view
    .Select

    'If you are in Page Break Preview Or Page Layout view go
    'back to normal view, we do this for speed
    ViewMode = ActiveWindow.View
    ActiveWindow.View = xlNormalView

    'Turn off Page Breaks, we do this for speed
    .DisplayPageBreaks = False

    'Set the first and last row to loop through
    Firstrow = .UsedRange.Cells(1).Row
    Lastrow = .UsedRange.Rows(.UsedRange.Rows.Count).Row

    'We loop from Lastrow to Firstrow (bottom to top)
    For Lrow = Lastrow To Firstrow Step -1

    'We check the values in the A column in this example
    With .Cells(Lrow, "J")

    If Not IsError(.Value) Then

    If .Value = "" Then .EntireRow.Delete
    'This will delete each row with the Value "ron"
    'in Column A, case sensitive.

    End If

    End With

    Next Lrow

    End With

    ActiveWindow.View = ViewMode
    With Application
    .ScreenUpdating = True
    .Calculation = CalcMode
    End With

    End Sub




    Private Sub Consolidate_Click()

    Dim sh As Worksheet
    Dim DestSh As Worksheet
    Dim Last As Long
    Dim shLast As Long
    Dim CopyRng As Range
    Dim StartRow As Long

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

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

    'Add a worksheet with the name "Pricing"
    Set DestSh = ActiveWorkbook.Worksheets.Add
    DestSh.Name = "Pricing"

    'Fill in the start row
    StartRow = 25

    'loop through all worksheets and copy the data to the DestSh
    For Each sh In ActiveWorkbook.Worksheets

    'Loop through all worksheets except the RDBMerge worksheet and the
    'Information worksheet, you can ad more sheets to the array if you want.
    If sh.Name <> DestSh.Name And sh.Visible = True And UCase(Left(sh.Name, 3)) = "WBS" Then

    'Find the last row with data on the DestSh and sh
    Last = Lastrow(DestSh)
    shLast = Lastrow(sh)

    'If sh is not empty and if the last row >= StartRow copy the CopyRng
    If shLast > 0 And shLast >= StartRow Then

    'Set the range that you want to copy
    Set CopyRng = sh.Range(sh.Rows(StartRow), sh.Rows(shLast))

    '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
    CopyRng.Copy
    With DestSh.Cells(Last + 1, "A")
    .PasteSpecial 8 ' Column width
    .PasteSpecial xlPasteValues
    .PasteSpecial xlPasteFormats
    Application.CutCopyMode = False
    End With

    End If

    End If
    Next

    Sheets("Pricing").Cells.FormatConditions.Delete

    ExitTheSub:

    Application.Goto DestSh.Cells(1)

    With Application
    .ScreenUpdating = True
    .EnableEvents = True
    End With
    End Sub

  2. #2
    Registered User
    Join Date
    07-24-2014
    Location
    Plano, Texas
    MS-Off Ver
    2013
    Posts
    19

    Re: Combine 2 Macros to Consolidate Data from Multiple Sheets and then Delete Blank Rows

    Note: I start copying from row 25 because there is stuff above row 25 on each worksheet that should not be pulled into my data extract. Also, if there is a way to stop my extract at row R that would be great!!

+ 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. How to allow users to add/delete rows/cloumns in sheets with macros.
    By damienchew in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-13-2014, 12:08 AM
  2. [SOLVED] delete all rows in all sheets where column B is blank
    By higginmi in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-01-2013, 11:48 AM
  3. Macro Command Button: Combine Save As CSV and Delete Unsed Rows Macros
    By mustng_sally in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-15-2013, 10:41 AM
  4. [SOLVED] How to Delete Multiple Consecutive Blank Rows - Delete all Blank Rows
    By raw_geek in forum Excel Programming / VBA / Macros
    Replies: 18
    Last Post: 11-16-2012, 03:17 PM
  5. Replies: 20
    Last Post: 10-19-2012, 04:35 PM
  6. Consolidate 2 spreadsheets by row with same cell data, delete unchanged rows
    By crist in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-28-2010, 04:46 PM
  7. Want to combine columns in rows where data exists from multiple sheets into one
    By bugmenot in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-14-2009, 08:27 PM
  8. Consolidate data from rows in many sheets to summary sheet?
    By Dynelor in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 06-08-2008, 07:34 PM

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