+ Reply to Thread
Results 1 to 6 of 6

Macro to copy row if target cell is not blank

  1. #1
    Registered User
    Join Date
    09-07-2014
    Location
    Australia
    MS-Off Ver
    2014
    Posts
    13

    Macro to copy row if target cell is not blank

    Hi Guys,

    I've a workbook that contains multiple worksheets with exactly the same structure. I want to combine all the worksheets into one by copying the entire row if cells in column "C" in not blank - need to start from row 10.

    Another complication is that in each of the worksheets there are values in cells (C1, C2, C3) which i need to copy them in three different columns in front of all copied rows from each sheet.

    Any help will be much appreciated.

    Thanks.

  2. #2
    Forum Expert p24leclerc's Avatar
    Join Date
    07-05-2010
    Location
    Québec
    MS-Off Ver
    Excel 2021
    Posts
    2,081

    Re: Macro to copy row if target cell is not blank

    Please supply us with a sample workbook with examples of what you want to achieve.
    Detailed examples of data you want to transfer and how they will be placed in the summary sheet.
    We need to know where the data comes from and where they go.
    Pierre Leclerc
    _______________________________________________________

    If you like the help you got,
    Click on the STAR "Add reputation" icon at the bottom.

  3. #3
    Forum Expert
    Join Date
    11-26-2013
    Location
    Colac, Victoria, Australia
    MS-Off Ver
    Excel 2016
    Posts
    1,309

    Re: Macro to copy row if target cell is not blank

    Hi John,

    This is a quick attempt in the absence of an example of the file.

    It assumes you have a "Summary" sheet, and you are copying small amounts of data.

    Please Login or Register  to view this content.
    If the worksheets are large, and/or you find this a little slow, we could speed it up by reading into an array, however, from what I remember from the last time I helped you, your worksheets are not huge, so this quick bit of code should do the trick.

    I hope this helps, please let me know!

    Regards,

    David


    - Please click on the *Add Reputation button at the bottom of helpful responses.

    Please mark your thread as SOLVED:
    - Click Thread Tools above your first post, select "Mark your thread as Solved".


  4. #4
    Registered User
    Join Date
    09-07-2014
    Location
    Australia
    MS-Off Ver
    2014
    Posts
    13

    Re: Macro to copy row if target cell is not blank

    Thanks Pierre and David,

    I've managed to get 90% of the code but i'm missing the part of filling in the first three columns in the summary sheet with the values in cells C1 & C2 & C3 in each sheet.

    the below code is doing everything except that it fills the three columns but only for the first row for each range of data brought from each sheet.

    i think the part needs to be updated is the part in Red below:

    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 CopyDataWithoutHeaders()
    Dim sh As Worksheet
    Dim DestSh As Worksheet
    Dim Last As Long
    Dim shLast As Long
    Dim CopyRng As Range
    Dim StartRow As Long
    Dim FirstRow As Long
    Dim rng1 As Range

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

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

    ' Add a new summary worksheet.
    Set DestSh = ActiveWorkbook.Worksheets.Add
    DestSh.Name = "RDBMergeSheet"

    ' Fill in the start row.
    StartRow = 10

    ' Loop through all worksheets and copy the data to the
    ' summary worksheet.
    For Each sh In ActiveWorkbook.Worksheets
    If sh.Name <> DestSh.Name Then

    ' Find the last row with data on the summary
    ' and source worksheets.
    Last = LastRow(DestSh)
    shLast = LastRow(sh)

    ' If source worksheet is not empty and if the last
    ' row >= StartRow, copy the range.



    If shLast > 0 And shLast >= StartRow Then
    'Set the range that you want to copy
    Set CopyRng = sh.Range("C10:Q300")
    Set CopyRng1 = sh.Range("C1:C3")

    ' Test to see whether there are enough rows in the summary
    ' worksheet to copy all the data.
    If Last + CopyRng.Rows.count > DestSh.Rows.count Then
    MsgBox "There are not enough rows in the " & _
    "summary worksheet to place the data."
    GoTo ExitTheSub
    End If

    ' This statement copies values and formats.
    CopyRng.Copy
    With DestSh.Cells(Last + 1, "D")
    .PasteSpecial xlPasteValues
    .PasteSpecial xlPasteFormats
    Application.CutCopyMode = False
    End With

    CopyRng1.Copy
    With DestSh.Cells(Last + 1, "A")
    .PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=True
    Application.CutCopyMode = False
    End With

    End If

    End If

    On Error Resume Next
    Columns("D").SpecialCells(xlCellTypeBlanks).EntireRow.Delete

    FirstRow = 1



    Sheets("RDBMergeSheet").Range("A&FirstRow:A" & Range("D" & Rows.count).End(xlUp).Row) = ws.Cells(1, "C")
    Sheets("RDBMergeSheet").Cells(Range(FirstRow, Cells(Rows.count, "D").End(xlUp).Row), 2) = ws.Cells(2, "C")
    Sheets("RDBMergeSheet").Cells(Range(FirstRow, Cells(Rows.count, "D").End(xlUp).Row), 3) = ws.Cells(3, "C")

    FirstRow = FirstRow + Cells(Rows.count, "D").End(xlUp).Row

    Next

    ExitTheSub:

    Application.Goto DestSh.Cells(1)

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

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


    End Sub

  5. #5
    Forum Expert p24leclerc's Avatar
    Join Date
    07-05-2010
    Location
    Québec
    MS-Off Ver
    Excel 2021
    Posts
    2,081

    Re: Macro to copy row if target cell is not blank

    Your last post does not comply with rule #3. You have to enclose your code inside CODE TAGS which you can import just by clicking the # icon just above this editing window. Place your code between the code tags.
    We can't answer you if you don't change this before.

  6. #6
    Forum Expert
    Join Date
    11-26-2013
    Location
    Colac, Victoria, Australia
    MS-Off Ver
    Excel 2016
    Posts
    1,309

    Re: Macro to copy row if target cell is not blank

    Hi John,
    I agree with Pierre! It i very difficult to read your macro as posted. Perhaps you could also include a sample workbook - it would be easier to understand what you are trying to do!

    From what I could see of the macro(s) you posted it seems unnecessarily complex, but difficult to test without data.

    Did you try my macro? It does what you described, but with far fewer lines of code. The main difference being it uses a "Summary" sheet rather than "RDBMergeSheet".

    Regards,

    David

+ 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] Insert Date/Time Stamp if Target Cell is Blank
    By howardjo in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 03-12-2014, 07:13 AM
  2. Replies: 3
    Last Post: 10-29-2013, 12:06 AM
  3. Recoding macro to add all cells above a target cell until there is a blank row.
    By PatrickDC in forum Excel Programming / VBA / Macros
    Replies: 18
    Last Post: 10-22-2013, 06:56 AM
  4. How to have a cell return blank when the target cell it is copying is blank
    By DanielWinning in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 08-23-2013, 11:14 AM
  5. Replies: 5
    Last Post: 08-20-2013, 08:10 AM
  6. Replies: 1
    Last Post: 10-28-2012, 01:13 AM
  7. Copy data but leave target cell blank
    By MarcoAUA in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-17-2012, 02:27 PM
  8. Macro to copy column until a blank cell
    By VBAnoob88 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-22-2010, 08:45 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