+ Reply to Thread
Results 1 to 3 of 3

Looping code with variable changing with each iteration

  1. #1
    Registered User
    Join Date
    12-06-2021
    Location
    Scotland
    MS-Off Ver
    365 (Mac)
    Posts
    9

    Looping code with variable changing with each iteration

    Hello,

    I am fairly new to VBA. I need to write a macro that goes into 50 different files, copies a set range and pastes that in a single file.

    I think I have got most of it sorted, I just need to figure out how to loop it 50 times.

    I have managed to get this to work by manually repeating a section of code, however I know there must be a simple way than repeating the same section of code 50 times.

    Can anyone help with how I can loop the code the desired number of times, each time going into a different file. i.e. changing DBname for each iteration.

    Please Login or Register  to view this content.
    I understand how to loop code with n iterations but I don't know to change the DBname variable with each iteration.

    Thanks in advance for any assistance you can provide!

  2. #2
    Forum Expert XLent's Avatar
    Join Date
    10-13-2010
    Location
    Northumberland, UK
    MS-Off Ver
    various
    Posts
    2,704

    Re: Looping code with variable changing with each iteration

    You could store the DBnames within an Array (or reference them from a range on a worksheet etc), then iterate the Array and repeat the code using the iterated value, e.g.

    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    12-06-2021
    Location
    Scotland
    MS-Off Ver
    365 (Mac)
    Posts
    9

    Re: Looping code with variable changing with each iteration

    That works perfectly, thank you!

+ 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. Run an entire loop instead of iteration by iteration when stepping into code?
    By Norcal1 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-24-2019, 04:20 PM
  2. Changing sheet looping code to reduce time required?
    By MrStevie in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-18-2018, 11:14 AM
  3. [SOLVED] change variable on every iteration in loop
    By JJGF in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-12-2015, 10:00 PM
  4. Finding the value of a variable through iteration
    By MechE818 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-08-2014, 06:18 PM
  5. Excel 2007 : Iteration with 2 changing variables
    By DamirDz in forum Excel General
    Replies: 5
    Last Post: 08-24-2011, 06:20 PM
  6. Dynamic Iteration: looping through changing number of rows
    By Esection in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-10-2010, 11:50 AM
  7. [SOLVED] (1) VARIABLE IN POSTING CODE (2) LOOPING
    By Robert in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-14-2005, 02:05 AM

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