Results 1 to 2 of 2

Thread: Loop in copy paste code

  1. #1

    Thread Starter
    Member
    Join Date
    Jan 2009
    Posts
    33

    Loop in copy paste code

    Hello

    I wrote a code that copies the cells from a specific range in Excel and pastes them to a specific Bookmark in Word.

    The code looks something like this:

    Application.Goto ActiveWorkbook.Sheets("Sheet1").Range("A1:W15")
    Selection copy
    WordDoc.Bookmarks("Bookmark1").Select
    WordApp.Selection.Paste
    Application.Goto ActiveWorkbook.Sheets("Sheet2").Range("A1:W15")
    Selection copy
    WordDoc.Bookmarks("Bookmark2").Select
    WordApp.Selection.Paste


    The problem now is that allthought I wrote the code allready the code now has ended being 120 pages or something and it is very Mouse Wheel to browse through

    Do you think there is a way to loop the process? Can I use variables or something else where the ranges in excel and where the bookmarks are written in the code?




    Thank you very much

  2. #2
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Loop in copy paste code

    Logically, Yes, you can do it. Programatically, it depends on how are your ranges placed. What are the names of the bookmark. Does the name of the bookmark follow a pattern etc... etc...

    Few Tips

    Sheets("Sheet1") can also be written as Sheets(1)
    Range("A1") can also be written as Range("A" & i)
    same goes with bookmark...

    See the following code. The code doesn't do anything... It is just to show you how to work with your code...

    Code:
    Sub Example()
        i = 2
        j = 5
        a = "Sheet" & i '<-- "Sheet2"
        Sheets(a).Range("A" & i).Copy '<--- Range("A2")
        Bmark = "Bookmark" & i '<--- "Bookmark2"
        Rng = Range("A" & i & ":B" & j).Address '<--- Range("A2:B5")
    End Sub
    Try it out. If you still get stuck... Yell This forum will definitely help you out. It always does
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width