+ Reply to Thread
Results 1 to 6 of 6

Runtime error 1004 range of object with VBA code for copying all worksheets to word

  1. #1
    Registered User
    Join Date
    03-29-2014
    Location
    Vermont
    MS-Off Ver
    Excel 2010
    Posts
    11

    Runtime error 1004 range of object with VBA code for copying all worksheets to word

    Below is the code I copied and tried to modify to select only the info from cells A1 to the last cell in column G. I get a runtime error 1004 range of object worksheet failed error message. It looks like I need assistance in the selection of the range of information line of code. I can't use xltoright because some of the cells are blank in the last row.

    Thanks for looking at this.

    Sub CopyWorksheetsToWord()
    ' requires a reference to the Word Object library:
    ' in the VBE select Tools, References and check the Microsoft Word X.X object library
    Dim wdApp As Word.Application, wdDoc As Word.Document, ws As Worksheet
    Application.ScreenUpdating = False
    Application.StatusBar = "Creating new document..."
    Set wdApp = New Word.Application
    Set wdDoc = wdApp.Documents.Add
    For Each ws In ActiveWorkbook.Worksheets
    With ActiveSheet
    Application.StatusBar = "Copying data from " & ws.Name & "..."
    ws.Range("A1:G1", Range("A1").End(xlDown).Range("G1").End(xlDown)).Copy
    wdDoc.Paragraphs(wdDoc.Paragraphs.Count).Range.InsertParagraphAfter
    wdDoc.Paragraphs(wdDoc.Paragraphs.Count).Range.Paste
    Application.CutCopyMode = False
    wdDoc.Paragraphs(wdDoc.Paragraphs.Count).Range.InsertParagraphAfter
    ' insert page break after all worksheets except the last one
    If Not ws.Name = Worksheets(Worksheets.Count).Name Then
    With wdDoc.Paragraphs(wdDoc.Paragraphs.Count).Range
    .InsertParagraphBefore
    .Collapse Direction:=wdCollapseEnd
    .InsertBreak Type:=wdPageBreak
    End With
    End If
    End With
    Next ws
    Set ws = Nothing
    Application.StatusBar = "Cleaning up..."
    ' apply normal view
    With wdApp.ActiveWindow
    If .View.SplitSpecial = wdPaneNone Then
    .ActivePane.View.Type = wdNormalView
    Else
    .View.Type = wdNormalView
    End If
    End With

  2. #2
    Valued Forum Contributor
    Join Date
    09-01-2013
    Location
    Dallas, TX
    MS-Off Ver
    Excel 2010
    Posts
    324

    Re: Runtime error 1004 range of object with VBA code for copying all worksheets to word

    This line that you have:

    Please Login or Register  to view this content.
    Should be:

    Please Login or Register  to view this content.


    Shelton A.
    If Helpful, Add Reputaion!

  3. #3
    Registered User
    Join Date
    03-29-2014
    Location
    Vermont
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: Runtime error 1004 range of object with VBA code for copying all worksheets to word

    Shelton,
    I changed the range to your suggestion and the output displays just first two rows of each worksheet, from cells A1:G2.
    Cindy

  4. #4
    Valued Forum Contributor
    Join Date
    09-01-2013
    Location
    Dallas, TX
    MS-Off Ver
    Excel 2010
    Posts
    324

    Re: Runtime error 1004 range of object with VBA code for copying all worksheets to word

    Hello cindy. Does column G end at G2?

  5. #5
    Valued Forum Contributor
    Join Date
    09-01-2013
    Location
    Dallas, TX
    MS-Off Ver
    Excel 2010
    Posts
    324

    Re: Runtime error 1004 range of object with VBA code for copying all worksheets to word

    If so, if you know your longest column then change the last G in Range("G" & Rows.Count) to something like A or whatever your longest column is. I've change it to A here:


    Changed from:
    Formula: copy to clipboard
    Please Login or Register  to view this content.


    to

    Formula: copy to clipboard
    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    03-29-2014
    Location
    Vermont
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: Runtime error 1004 range of object with VBA code for copying all worksheets to word

    Morning Shelton,
    This latest range selection you suggested will work.

    The number of rows in each worksheet varies. And the last column of information needed is in G. Thank you. Have a great day.
    Cindy

+ 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] Runtime Error '1004' Method 'Range' of object '_Worksheet' failed
    By ovalstar in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-20-2013, 02:15 PM
  2. Runtime error '1004' - Method 'Range' of object '_worksheet' falied
    By vijaykumarnachapalli in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-06-2013, 12:58 AM
  3. Runtime error '1004' - Method 'Range' of object '_worksheet' falied
    By vijaykumarnachapalli in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 05-05-2013, 02:45 PM
  4. runtime error 1004 method range of object global failed
    By dreamz in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-26-2006, 03:25 PM
  5. [SOLVED] runtime error 1004 method range of object '_global failed
    By valdesd in forum Excel General
    Replies: 2
    Last Post: 10-06-2005, 03:05 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