+ Reply to Thread
Results 1 to 9 of 9

Copying to first empty row?

Hybrid View

  1. #1
    Forum Contributor terrypin's Avatar
    Join Date
    01-06-2010
    Location
    East Grinstead, UK
    MS-Off Ver
    MS Office 365
    Posts
    548

    Copying to first empty row?

    I finally have the copying aspects of my macro working, after much help here thanks. To discuss the next stage I've attached a much simplified version of both source and destination workbooks used.

    The macro currently copies data from Source-Simplified.xlsm to fixed row 8 of WalkIndex-Extract.xlsm
    Sub Extract_Simplest()
    
    Dim wi As Workbook
    Set wi = Workbooks("WalkIndex-Extract.xlsm")
    
    Dim wb As Workbook
    Set wb = Workbooks("Source-Simplified.xlsm")
    
    'Track name
    wb.Worksheets("Track Data").Range("B5").Copy _
    Workbooks("WalkIndex-Extract.xlsm").Sheets("Target").Range("C8")
      
    'Main FW Heading (C6 & B3 in formula)
    wb.Worksheets("Track Data").Range("B27").Copy
    wi.Sheets("Target").Range("D8").PasteSpecial Paste:=xlPasteValues
    
    End Sub
    I want to edit that to copy instead to the first row in which col A is empty, row 4 in my example. The first run would therefore enter two cells into row 4, instead of row 8. And so on, as the number of rows increases.

    This code finds the first empty row OK.

    Sub FirstEmptyRowA1()
    'Find the first empty row in Column A
    Dim lngLastRow As Long
    lngLastRow = Cells(Rows.Count, "A").End(xlUp).Row
    
    Dim FirstEmpty As Long
    FirstEmpty = lngLastRow + 1
    
    MsgBox (FirstEmpty)
    
    End Sub
    But attempts to use 'FirstEmpty' in Sub Extract_Simplest() have so far defeated me and any help would be appreciated please
    Attached Files Attached Files
    Terry, East Grinstead, UK
    Excel 365, Win 10 Pro, i7 PC, 4.0 GHz

  2. #2
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,958

    Re: Copying to first empty row?

    Option Explicit
    
    Sub Extract_Simplest()
    Dim nr As Long, wi As Workbook, wb As Workbook, ws As Worksheet
    Set wb = Workbooks("Source-Simplified.xlsm")
    Set wi = Workbooks("WalkIndex-Extract.xlsm")
    Set ws = wi.Sheets("Target")
    With ws
        nr = .Cells(.Rows.Count, "C").End(xlUp).Row + 1
        wb.Worksheets("Track Data").Range("B5").Copy ws.Range("C" & nr)
        wb.Worksheets("Track Data").Range("B27").Copy
        ws.Range("C" & nr + 1).PasteSpecial xlPasteValues
    End With
    Application.CutCopyMode = False
    End Sub
    Last edited by Sintek; 01-07-2020 at 12:55 PM.
    Good Luck...
    I don't presume to know what I am doing, however, just like you, I too started somewhere...
    One-day, One-problem at a time!!!
    If you feel I have helped, please click on the [★ Add Reputation] to left of post window...
    Also....Add a comment if you like!!!!
    And remember...Mark Thread as Solved...
    Excel Forum Rocks!!!

  3. #3
    Forum Contributor terrypin's Avatar
    Join Date
    01-06-2010
    Location
    East Grinstead, UK
    MS-Off Ver
    MS Office 365
    Posts
    548

    Re: Copying to first empty row?

    Many thanks @sintek, appreciate the fast reply.

    Your code correctly copies source B5 to C4, but B27 should go to D4, not C5. However, you would not have been able to spot this by running your macro because I must have saved the source while B27 was empty, sorry!

    As you've done the hard part I was able to make the two minor edits and it now works fine, thanks. I've attached updates of the sheets.

    Sub Extract_Simplest_sintek_edited()
    Dim nr As Long, wi As Workbook, wb As Workbook, ws As Worksheet
    Set wb = Workbooks("Source-Simplified.xlsm")
    Set wi = Workbooks("WalkIndex-Extract.xlsm")
    Set ws = wi.Sheets("Target")
    With ws
        nr = .Cells(.Rows.Count, "C").End(xlUp).Row + 1
        wb.Worksheets("Track Data").Range("B5").Copy ws.Range("C" & nr)
        wb.Worksheets("Track Data").Range("B27").Copy
        ws.Range("D" & nr).PasteSpecial xlPasteValues
    End With
    Application.CutCopyMode = False
    End Sub
    Next minor step will be to add a new line each time, to keep TOTALS below the last entry. And then the slog of extending the simplified to the full version. But I'm optimistic of having a working macro soon, which I can then apply to a hundred or two source files.
    Attached Files Attached Files

  4. #4
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,958

    Re: Copying to first empty row?

    TOTALS below the last entry
    Totals of what...Nothing to sum?

  5. #5
    Forum Contributor terrypin's Avatar
    Join Date
    01-06-2010
    Location
    East Grinstead, UK
    MS-Off Ver
    MS Office 365
    Posts
    548

    Re: Copying to first empty row?

    Quote Originally Posted by sintek View Post
    Totals of what...Nothing to sum?
    That's just text at present, for this simplified version. The full macro counts all rows from row 2 downward to the last one.

    I've added a line that keeps the space.

    Sub Extract_Simplest_InsertRow()
    Dim nr As Long, wi As Workbook, wb As Workbook, ws As Worksheet
    Set wb = Workbooks("Source-Simplified.xlsm")
    Set wi = Workbooks("WalkIndex-Extract.xlsm")
    Set ws = wi.Sheets("Target")
    With ws
        nr = .Cells(.Rows.Count, "C").End(xlUp).Row + 1
        wb.Worksheets("Track Data").Range("B5").Copy ws.Range("C" & nr)
        wb.Worksheets("Track Data").Range("B27").Copy
        ws.Range("D" & nr).PasteSpecial xlPasteValues
    End With
    Application.CutCopyMode = False
    'Insert row below to keep a few rows free before TOTALS
    ActiveCell.Offset(1).EntireRow.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromRightOrAbove
    End Sub

  6. #6
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,958

    Re: Copying to first empty row?

    Okey dokie...Thanks for rep +

  7. #7
    Forum Contributor terrypin's Avatar
    Join Date
    01-06-2010
    Location
    East Grinstead, UK
    MS-Off Ver
    MS Office 365
    Posts
    548

    Re: Copying to first empty row?

    This line, to insert a blank row after the last entry was pasted, worked fine in the simplified version I posted earlier. But in the full version it now highlights 'xlFormatFromRightOrAbove' with the error "Compile error: Variable not defined".

    No variable was defined before. And isn't this a parameter, not a variable anyway?

    ActiveCell.Offset(1).EntireRow.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromRightOrAbove

  8. #8
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,958

    Re: Copying to first empty row?

    Think you should upload a workable file with your actual requirement for this TOTAL...Your solution might not be the best alternative...
    Perhaps you should start a new thread depicting your requirement, as it no longer pertains to this thread...

  9. #9
    Forum Contributor terrypin's Avatar
    Join Date
    01-06-2010
    Location
    East Grinstead, UK
    MS-Off Ver
    MS Office 365
    Posts
    548

    Re: Copying to first empty row?

    Not exactly sure how, but I found a winning combination after a couple of hours of experiment and happily that snag is now resolved thanks.

    You’re right, I realise now that I should have started a new thread, given that I’d already set this one as solved. There are a few others and I’ll post separately if I can’t fix them myself.

+ 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] Copying values into empty cells below
    By kban in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-08-2017, 12:06 PM
  2. Copying into empty cells
    By butlerar in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-28-2014, 09:29 PM
  3. [SOLVED] Copying to next empty row
    By 4evra in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-25-2013, 07:17 PM
  4. Copying to Next Empty Row
    By Pasha81 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-10-2009, 01:27 PM
  5. Copying a value to the next empty cell
    By Arney in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 09-11-2009, 12:57 PM
  6. copying a range to first empty row is copying over data
    By jseufert in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-13-2009, 03:45 AM
  7. copying empty cells as empty
    By sjhonny in forum Excel General
    Replies: 2
    Last Post: 01-17-2007, 06:52 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