+ Reply to Thread
Results 1 to 5 of 5

Unable to create new Workbook and copy data into it

  1. #1
    Registered User
    Join Date
    12-17-2012
    Location
    Poland
    MS-Off Ver
    2013
    Posts
    57

    Unable to create new Workbook and copy data into it

    Hey Guys,

    so I'm trying to have the output data (created earlier in the same Macro) copied to a new Workbook which then would be saved in the same path as the Macro and named using current (system) month. My code is this:

    Please Login or Register  to view this content.
    targetSheet1 is a value defined earlier in the code and means simply the Sheet in which the output (which I want copied to a new Workbook) is located.

    When running the macro I get the "Object variable or with block variable not set" error and the newWorkbook = Workbooks.add is highlighted.

    The new Workbook gets created, but obviously the macro is interrupted, so nothing is copied and the file is not saved.

    I was afraid this would happen as I'm not entirely familiar with working with new workbooks... Can someone explain what happened here and how to correct it?

  2. #2
    Valued Forum Contributor xlbiznes's Avatar
    Join Date
    02-22-2013
    Location
    Bahrain
    MS-Off Ver
    Excel 2007
    Posts
    1,223

    Re: Unable to create new Workbook and copy data into it

    Try this code with the changes made.

    Please Login or Register  to view this content.
    Happy Computing ,

    Xlbiznes.

    To show your appreciation please click *

  3. #3
    Registered User
    Join Date
    12-17-2012
    Location
    Poland
    MS-Off Ver
    2013
    Posts
    57

    Re: Unable to create new Workbook and copy data into it

    Hey,

    thanks for the suggestions. It does not show an error now and creates the new file, and saves it, and names it with the month, BUT...

    It only copies Range A1:AG1...

    Shouldn't this line:
    Please Login or Register  to view this content.
    Determine which row in column B is the last (data input to the macro varies, so it will never be exactly same number of rows) and apply the row number to the column AG? At least that was my intention, but now it only seems to copy A1 until AG1...


    EDIT

    I've figured it out. I think the variable "targetSheet1" wasn't used properly anymore (earlier in the code it's simply being defined as ActiveWorkbook, but since a new Workbook is created, it probably gets confusing as to which Workbook should be treated as Active... Just my guess though.).

    In case anyone needed it, below I paste the code with my adjustments:

    Please Login or Register  to view this content.
    So thanks xlbiznes for the "New workbook" and "Set" prefix clarification!
    Last edited by mintymike; 04-22-2014 at 02:46 PM.

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: Unable to create new Workbook and copy data into it

    That code will actually find the last cell in column B in the active sheet of the new workbook, to get the last row in column B in targetSheet1 you need to add a worksheet reference.
    Please Login or Register  to view this content.
    If posting code please use code tags, see here.

  5. #5
    Registered User
    Join Date
    12-17-2012
    Location
    Poland
    MS-Off Ver
    2013
    Posts
    57

    Re: Unable to create new Workbook and copy data into it

    Yeah, thought so, Norie but thanks for confirming that! I've edited my original reply and posted the adjusted code. But your line is actually much easier (and shorter), so I'll use it.

    Thanks again.

    Marking this solved.

+ 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. Unable to create data form
    By rkauf in forum Excel General
    Replies: 1
    Last Post: 02-07-2014, 10:29 AM
  2. create new workbook, copy data, rename sheet.
    By simeonmein in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-10-2013, 02:00 AM
  3. [SOLVED] Unable to copy data from source excel to destination excel workbook
    By Lui23 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 09-21-2012, 07:22 AM
  4. Copy data from workbook, create new workbook, paste data to new worbook?
    By mpkavanagh in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 02-16-2011, 06:39 PM
  5. Unable to copy data from one workbook to another
    By sisterwolf in forum Excel General
    Replies: 1
    Last Post: 09-10-2010, 05:55 PM

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