+ Reply to Thread
Results 1 to 6 of 6

Problem pasting data into new workbook when worksheet has no records

  1. #1
    Registered User
    Join Date
    10-11-2012
    Location
    Tennessee
    MS-Off Ver
    Excel 2010
    Posts
    5

    Problem pasting data into new workbook when worksheet has no records

    I have a macro that opens another workbook, copies the data, and then pates the data into the bottom of the existing workbook. My problem is when the report I am copying from yields no records the macro errors on the paste command because it tries to copy the entire worksheet since there is no data to stop the Selection.End(xlDown).Select command. Is there a way I can tell the macro if Cell A2 is blank to skip to another step?

    ----------------------This is where I copy the data
    Range("A2").Select
    ActiveCell.Name = "Begin"
    Range("A1").Select
    Selection.End(xlDown).Select
    ActiveCell.Offset(0, 13).Select
    ActiveCell.Name = "End"
    Range("Begin:End").Select
    Selection.Copy

    ---------------------This is where I go to the other workbook and find the end of the existing data.

    ActiveWindow.ActivateNext

    Range("A1").Select
    Selection.End(xlDown).Select
    ActiveCell.Offset(1, 0).Select
    ActiveCell.Name = "Begin"
    ActiveCell.Offset(0, 13).Select
    ActiveCell.Name = "End"
    Range("Begin:End").Select

    -------------------------This is where I try to paste the data from the first workbook to the bottom the of data on the second workbook. This is where I get the error if there was no data on the first workbook.
    Selection.PasteSpecial Paste:=xlPasteAllUsingSourceTheme, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False

  2. #2
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Problem pasting data into new workbook when worksheet has no records

    Please use code tags with your code.

    This is where I get the error if there was no data on the first workbook.
    Please Login or Register  to view this content.
    If you are copying from the first book, the error must be on the second book, otherwise it would have shown the error when you try to copy from the first book. If you are selecting where you want to post the copied data in to book, it does not matter if the book is empty. You are instructing the code to copy it to specific position.

  3. #3
    Registered User
    Join Date
    10-11-2012
    Location
    Tennessee
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: Problem pasting data into new workbook when worksheet has no records

    Thank you for the reply. I will make sure and use the code tags going forward.

    You are correct I am getting the error on the second workbook when I try to paste the data from the first workbook. It is because when the first workbook is blank (which occurs when there is not data for the current days report) the command to go from A1 to the bottom takes the copy all the way down from cell A1 to cell A1048576 because there is no cell data sooner. Then when it goes to paste in the other workbook it errors because there is not enough room to paste that many rows. I need something to stop the copy if cell A2 is blank.

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644
    One thing you could try is using xlUp rather than Excel down.

    Something like this perhaps.
    Please Login or Register  to view this content.
    If posting code please use code tags, see here.

  5. #5
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Problem pasting data into new workbook when worksheet has no records

    I am not sure if I am following you. When you copy from the first sheet, the cursor will highlight all cells supposed to be copied. And then you go to the destination book, then select the cell where you want to copy to. If you wan to copy starting from A2, put the cursor on A2, if it is A1, put the cursor on A1 and so on. I suspect the error is because the copy and pasting areas are not the same, so to avoid this, when you paste do not select all cells, just one will do.
    The easy way would be to adjust your code, but has many selections.
    Like

    Please Login or Register  to view this content.
    Will tell the code the last data in column A, so to copy in to next empty row

    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    10-11-2012
    Location
    Tennessee
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: Problem pasting data into new workbook when worksheet has no records

    Thank you both for the feedback. I was able to solve my problem by using IF statement to say if the value of A2 is blank to call the next macro, thereby skipping the error I was running into.

    Please Login or Register  to view this content.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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