+ Reply to Thread
Results 1 to 22 of 22

Copy tables from excel to Wd VBA

  1. #1
    Registered User
    Join Date
    01-23-2019
    Location
    constanta
    MS-Off Ver
    WIN 10
    Posts
    16

    Copy tables from excel to Wd VBA

    Hi! I am new to VBA and I have a question. I have the following code, with it to copy a table which has a header (A1 to F1) from each sheet (GestiuneCCS; AlimATM; RidDepAng) to a designated bookmark (1;2;3…) in Wd. It works “fine”, but in case I don’t have information in one table the macro brings in word whole table blank, and I don’t want that, I want it to bring only the Header of that table. Attach the macro. How can I correct the macro? Thanks
    Please Login or Register  to view this content.

  2. #2
    Forum Expert PaulM100's Avatar
    Join Date
    10-09-2017
    Location
    UK
    MS-Off Ver
    Office 365
    Posts
    2,108

    Re: Copy tables from excel to Wd VBA

    Replace the part in red with the one below. Do the same for the other sheets. Untested.
    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    Click the * to say thanks.

  3. #3
    Registered User
    Join Date
    01-23-2019
    Location
    constanta
    MS-Off Ver
    WIN 10
    Posts
    16

    Re: Copy tables from excel to Wd VBA

    Hi, Paul,
    I have an "Run-time error "9" Subscript out of range on row LastCol = Sheets(“GestiuneSSC”).Range("A1").End(xlToRight).Column

    Please help!

  4. #4
    Registered User
    Join Date
    01-23-2019
    Location
    constanta
    MS-Off Ver
    WIN 10
    Posts
    16

    Re: Copy tables from excel to Wd VBA

    Also, from time to time I have an err "4601" Cannot activate appl, on row: .Activate ' make it the top pane, bring it to the front.

  5. #5
    Forum Expert PaulM100's Avatar
    Join Date
    10-09-2017
    Location
    UK
    MS-Off Ver
    Office 365
    Posts
    2,108

    Re: Copy tables from excel to Wd VBA

    Attach a Excel sample containing data into those tables.

    1. Make sure that your sample data are REPRESENTATIVE of your real data. The use of unrepresentative data is very frustrating and can lead to long delays in reaching a solution.

    2. Make sure that your desired results are also shown (mock up the results manually).

    3. Make sure that all confidential data is removed or replaced with dummy data first (e.g. names, addresses, E-mails, etc.).

    4. Try to avoid using merged cells as they cause lots of problems.

    Unfortunately the attachment icon doesn't work at the moment, so to attach an Excel file you have to do the following: just before posting, scroll down to Go Advanced and then scroll down to Manage Attachments. Now follow the instructions at the top of that screen.

  6. #6
    Registered User
    Join Date
    01-23-2019
    Location
    constanta
    MS-Off Ver
    WIN 10
    Posts
    16

    Re: Copy tables from excel to Wd VBA

    Hi, Paul, first of all, sorry for my late reply.
    1/2) If I understand correctly, have attach the samples (Centralizare pt esantion si camere.xlsm - this is where I have macro) and how I want the final result in word (Final result), each table correspond to an inserted bookmark.
    3) No confidential data in samples.
    4) No merged cells.
    Thanks again for all the help!
    Last edited by Fane1981; 01-25-2019 at 04:37 AM. Reason: erase

  7. #7
    Forum Expert PaulM100's Avatar
    Join Date
    10-09-2017
    Location
    UK
    MS-Off Ver
    Office 365
    Posts
    2,108

    Re: Copy tables from excel to Wd VBA

    I have no experience in word, so I have not replicated your dotm template. Try this as in your macro to copy the range until last not blank cell, assuming that your data range is into column A:G
    Please Login or Register  to view this content.
    Last edited by PaulM100; 01-25-2019 at 05:41 AM.

  8. #8
    Registered User
    Join Date
    01-23-2019
    Location
    constanta
    MS-Off Ver
    WIN 10
    Posts
    16

    Re: Copy tables from excel to Wd VBA

    On row: Sub ExportExcelDataToWordDocument()
    Compile err: Expected End With

  9. #9
    Forum Expert PaulM100's Avatar
    Join Date
    10-09-2017
    Location
    UK
    MS-Off Ver
    Office 365
    Posts
    2,108

    Re: Copy tables from excel to Wd VBA

    Updated in post #7, above in red

  10. #10
    Registered User
    Join Date
    01-23-2019
    Location
    constanta
    MS-Off Ver
    WIN 10
    Posts
    16

    Re: Copy tables from excel to Wd VBA

    on row: With WB1.Sheets("GestiuneSSC")
    Err "424" Object required

  11. #11
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,908

    Re: Copy tables from excel to Wd VBA

    Please Login or Register  to view this content.
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  12. #12
    Registered User
    Join Date
    01-23-2019
    Location
    constanta
    MS-Off Ver
    WIN 10
    Posts
    16

    Re: Copy tables from excel to Wd VBA

    err on line: .Selection.GoTo what:=-1, Name:="bookmark1" ' -1 means "wdgotobookmark"
    "438" object doesn't support this property or method.

  13. #13
    Registered User
    Join Date
    01-23-2019
    Location
    constanta
    MS-Off Ver
    WIN 10
    Posts
    16

    Re: Copy tables from excel to Wd VBA

    This is how my macro look at this point
    Please Login or Register  to view this content.

  14. #14
    Forum Expert PaulM100's Avatar
    Join Date
    10-09-2017
    Location
    UK
    MS-Off Ver
    Office 365
    Posts
    2,108

    Re: Copy tables from excel to Wd VBA

    Change this part
    Please Login or Register  to view this content.
    to
    Please Login or Register  to view this content.

  15. #15
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,726

    Re: Copy tables from excel to Wd VBA

    Cross-posted at: https://www.mrexcel.com/forum/genera...es-word-4.html without any acknowledgement here of the help you've already been given there.
    Also cross-posted at: https://www.excelforum.com/excel-pro...to-wd-vba.html
    Evidently you don't care about the cross-posting rules here (http://www.excelforum.com/forum-rule...rum-rules.html) or on any other forum.
    Cheers,
    Paul Edstein
    [Fmr MS MVP - Word]

  16. #16
    Registered User
    Join Date
    01-23-2019
    Location
    constanta
    MS-Off Ver
    WIN 10
    Posts
    16

    Re: Copy tables from excel to Wd VBA

    Macro, this is not cross-post, as you can see it is another macro, another issue.

  17. #17
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,726

    Re: Copy tables from excel to Wd VBA

    For all practical purposes, it's essentially the same. Or are you just telling us you can't be bothered learning anything from the help you've been given elsewhere???

  18. #18
    Registered User
    Join Date
    01-23-2019
    Location
    constanta
    MS-Off Ver
    WIN 10
    Posts
    16

    Re: Copy tables from excel to Wd VBA

    @mrexcel, for that macro I had no resolve.
    So, please, can you help me with this one?!
    Thank you!
    Last edited by Fane1981; 01-25-2019 at 07:58 AM. Reason: add

  19. #19
    Registered User
    Join Date
    01-23-2019
    Location
    constanta
    MS-Off Ver
    WIN 10
    Posts
    16

    Re: Copy tables from excel to Wd VBA

    @Paul:
    Same error, same line.

    Range("A1", "G" & LastRow).Select
    Selection.Copy
    Selection.GoTo what:=-1, Name:="bookmark1" ' -1 means "wdgotobookmark" >>>>THIS LINE
    .Selection.Paste 'paste from the clipboard to the Word Doc.

  20. #20
    Registered User
    Join Date
    01-23-2019
    Location
    constanta
    MS-Off Ver
    WIN 10
    Posts
    16

    Re: Copy tables from excel to Wd VBA

    I managed to make some changes to the macro, now it works for table 3, the one with no data in it, in word I have only the Header.
    BUT, for the tables 1 an 2 (GestiuneSSC, AlimATM), with data in them, it copyies only the tables blank-no data.
    @Paul/@Macro, Please help!
    Part of macro with issues:
    'as before, copy the whole table from sheet to clipboard.
    Set WB = ThisWorkbook
    With WB.Sheets("GestiuneSSC")
    LastRow = .Range("A:G").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    Range("A1", "G" & LastRow).Select
    Selection.Copy
    wdWordApp.Selection.GoTo what:=-1, Name:="bookmark1" ' -1 means "wdgotobookmark"
    wdWordApp.Selection.Paste 'paste from the clipboard to the Word Doc.

    End With


    '****************
    'as before, copy the whole table from sheet to clipboard.
    Set WB = ThisWorkbook
    With WB.Sheets("AlimATM")
    LastRow = .Range("A:F").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    Range("A1", "F" & LastRow).Select
    Selection.Copy
    wdWordApp.Selection.GoTo what:=-1, Name:="bookmark2" ' -1 means "wdgotobookmark"
    wdWordApp.Selection.Paste 'paste from the clipboard to the Word Doc.

  21. #21
    Registered User
    Join Date
    01-23-2019
    Location
    constanta
    MS-Off Ver
    WIN 10
    Posts
    16

    Re: Copy tables from excel to Wd VBA

    i made some trials on the macro, and I have other issue.
    Now it copyies only the info from table1 (GestiuneSSC) - ONLY if I have some info in that tables (Table2 AlimATM&Table3 RisDepAngajati), into corresponding bookmarks (Bookmark2&Bookmark3).
    If the tables AlimATM&RidDepAngajati are empty, then it copies only the Header in corresponding bookmarks.
    If I have information in tables (AlimATM&RidDepAngajati), then it copies the data from table1 (GestiuneSSC), but only on the number of rows with info.

    Help needed!

    MACRO

    'as before, copy the whole table from sheet to clipboard.
    Set WB = ThisWorkbook
    With WB.Sheets("GestiuneSSC")
    LastRow = .Range("A:F").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    Range("A1", "F" & LastRow).Select
    Selection.Copy
    wdWordApp.Selection.GoTo what:=-1, Name:="bookmark1" ' -1 means "wdgotobookmark"
    wdWordApp.Selection.Paste 'paste from the clipboard to the Word Doc.

    End With


    '****************
    'as before, copy the whole table from sheet to clipboard.
    Set WB = ThisWorkbook
    With WB.Sheets("AlimATM")
    LastRow = .Range("A:F").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    Range("A1", "F" & LastRow).Select
    Selection.Copy
    wdWordApp.Selection.GoTo what:=-1, Name:="bookmark2" ' -1 means "wdgotobookmark"
    wdWordApp.Selection.Paste 'paste from the clipboard to the Word Doc.

    End With

    '****************
    'as before, copy the whole table from sheet to clipboard.
    Set WB = ThisWorkbook
    With WB.Sheets("DepRidAngajati")
    LastRow = .Range("A:F").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    Range("A1", "F" & LastRow).Select
    Selection.Copy
    wdWordApp.Selection.GoTo what:=-1, Name:="bookmark3" ' -1 means "wdgotobookmark"
    wdWordApp.Selection.Paste 'paste from the clipboard to the Word Doc.

    End With
    Last edited by Fane1981; 01-25-2019 at 10:15 AM. Reason: ADD

  22. #22
    Registered User
    Join Date
    01-23-2019
    Location
    constanta
    MS-Off Ver
    WIN 10
    Posts
    16

    Re: Copy tables from excel to Wd VBA

    Hi, Paul!
    I managed to make the code to work. Thank you so, so much for all the support! Have a nice day!

+ 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. Copy tables from outlook to excel
    By jpo1976 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-02-2018, 08:52 AM
  2. [SOLVED] Copy excel sheets to Word Tables
    By oneillp1 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-25-2013, 12:00 PM
  3. How to write a macro to copy 2 excel tables to be pasted to another excel worksheet??
    By deirdre_mclaren in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-28-2013, 06:03 AM
  4. Copy tables from excel to ppt in the same format
    By kapilgadhire in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-31-2012, 05:26 AM
  5. Copy pasting tables from Excel to Powerpoint
    By jsohng in forum Excel General
    Replies: 1
    Last Post: 12-10-2009, 05:29 PM
  6. [SOLVED] How to copy from Adobe with tables to Excel with columns intact
    By Shengli in forum Excel General
    Replies: 0
    Last Post: 07-15-2006, 02:00 AM
  7. [SOLVED] Copy Word tables to Excel
    By Don in forum Excel General
    Replies: 3
    Last Post: 02-13-2005, 01:08 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