+ Reply to Thread
Results 1 to 3 of 3

Looping through multiple cell ranges

Hybrid View

  1. #1
    Registered User
    Join Date
    05-26-2015
    Location
    Copenhagen
    MS-Off Ver
    2010
    Posts
    28

    Looping through multiple cell ranges

    Hello i have the following VBA code:

    Sub Print_area()
        
        
        Range("B2:U42").Select
        ActiveWorkbook.Names.Add Name:="LL_1K", RefersToR1C1:="=LLBA!R2C2:R42C21"
        Range("W2:AP42").Select
        ActiveWorkbook.Names.Add Name:="LL_1B", RefersToR1C1:="=LLBA!R2C23:R42C42"
        
        
        Range("B44:U84").Select
        ActiveWorkbook.Names.Add Name:="LL_2K", RefersToR1C1:="=LLBA!R44C2:R84C21"
        Range("W44:AP84").Select
        ActiveWorkbook.Names.Add Name:="LL_2B", RefersToR1C1:="=LLBA!R44C23:R84C42"
    
    End Sub
    What i want to do is i want to create a loop (i suppose) to complete the task above a number of times.

    So lets say that i want to loop through i=1:X
    Then the name LL_1B, is LL_iB,
    The range needs to be upped 42 for each loop so Range("B(2+(i-1)*42) : U(42+(i-1)*42) ")
    etc

    How do i write this in code that VBA understand?

  2. #2
    Valued Forum Contributor
    Join Date
    11-12-2014
    Location
    Nottingham
    MS-Off Ver
    2013
    Posts
    383

    Re: Looping through multiple cell ranges

    Try these

    For i = 1 To X
    
    Next i
    Name:="LL_" & i & "B"
    Range("B" & 2 + (i - 1) * 42, "U" & 42 + (i - 1) * 42)
    !If all your troubles are solved, then so is the thread. Show this by marking it so using thread tools at the top of the page.
    If I helped do this, then please show your appreciation by awarding rep points.
    <------ Button for that is over there

  3. #3
    Registered User
    Join Date
    05-26-2015
    Location
    Copenhagen
    MS-Off Ver
    2010
    Posts
    28

    Re: Looping through multiple cell ranges

    Thanks a bunch,
    Final code is:

    Sub Print_area()
        
        X = 40
        For i = 1 To X
    
    
        
        Range("B" & 2 + (i - 1) * 42, "U" & 42 + (i - 1) * 42).Select
        ActiveWorkbook.Names.Add Name:="LL_" & i & "K", RefersToR1C1:="=LLBA!R" & 2 + (i - 1) * 42 & "C2:R" & 42 + (i - 1) * 42 & "C21"
        
        Range("W" & 2 + (i - 1) * 42, "AP" & 42 + (i - 1) * 42).Select
        ActiveWorkbook.Names.Add Name:="LL_" & i & "B", RefersToR1C1:="=LLBA!R" & 2 + (i - 1) * 42 & "C23:R" & 42 + (i - 1) * 42 & "C42"
        
    
         Next i
        
    End Sub

+ 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. Looping on a multiple ranges of data
    By Trent136 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-21-2014, 12:10 AM
  2. [SOLVED] Looping through cell ranges in multiple worksheets and prevent saving if cell value empty
    By Spritz in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 04-22-2013, 11:24 AM
  3. [SOLVED] Looping through ranges in worksheet
    By stevow in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 06-19-2012, 11:06 AM
  4. Looping Named Ranges
    By jensenleesen in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-23-2011, 10:07 PM
  5. looping calculation between two ranges
    By form in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-15-2010, 11:43 AM
  6. Looping through dynamic ranges from multiple worksheets
    By TechWrangler in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-08-2006, 03:20 PM
  7. [SOLVED] Looping in VB with cell ranges
    By Freeman in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 01-22-2006, 08:20 AM
  8. Looping Ranges
    By purple_m in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-14-2005, 08:24 AM

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