+ Reply to Thread
Results 1 to 4 of 4

Do Loop with Variable Range

Hybrid View

  1. #1
    Registered User
    Join Date
    07-16-2013
    Location
    operation
    MS-Off Ver
    Excel 2010
    Posts
    15

    Do Loop with Variable Range

    The following macro is in a file that I inherited and I would like to change it so that it will stop after reaching the last row of data (blank after that). I am new to Macros and it looks like there is a lot of junk in this macro, but right now I just want to limit it to the data range which changes daily when this is run. Any help is appreciated.
    Sub Subs_insert()
    '
    ' Subs_insert Macro
    '
    
    '
    Dim startLocation As Integer
    startLocation = 2
    endLocation = 225 * 13 + 225
    rowLocation = 3
    
    Do
    
        Windows("Hotlist Template V2_1.xlsm").Activate
        
        Range("B" & startLocation).Select
        Selection.Copy
        Windows("Hotlist V2_0 Tools.XLSX").Activate
        Sheets("Family Tool").Select
        
        Range("B2").Select
        Range("B2").Activate
        
        ActiveSheet.Paste
        Range("C2:C14").Select
        Application.CutCopyMode = False
        Windows("Hotlist Template V2_1.xlsm").Activate
        Windows("Hotlist V2_0 Tools.XLSX").Activate
        Sheets("Family Tool").Select
        Range("C9").Select
        Windows("Hotlist Template V2_1.xlsm").Activate
        Rows(rowLocation).Select
        Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
        Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
        Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
        Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
        Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
        Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
        Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
        Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
        Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
        Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
        Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
        Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
        Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
        Range("B" & startLocation).Select
        Selection.Copy
        Windows("Hotlist V2_0 Tools.xlsx").Activate
        Sheets("Family Tool").Select
        
        Range("B2").Select
        ActiveSheet.Paste
        Range("C2:AF14").Select
        Application.CutCopyMode = False
        Selection.Copy
        Windows("Hotlist Template V2_1.xlsm").Activate
        Range("B" & startLocation).Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
      '  Windows("Hotlist V2_0 Tools.xlsx").Activate
      '  Sheets("Family Tool").Select
        
      '  Range("D2:AF14").Select
      '  Application.CutCopyMode = False
      '  Selection.Copy
      '  Windows("Hotlist Template V2_1.xlsm").Activate
      '  Range("D" & startLocation).Select
      '  Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
      '  Windows("LCD_DSI.XLS").Activate
      '  Application.CutCopyMode = False
        Windows("Hotlist Template V2_1.xlsm").Activate
        
        startLocation = startLocation + 14
        rowLocation = rowLocation + 14
        
    Loop Until startLocation > endLocation
    End Sub
    Last edited by Austex_egger; 07-24-2013 at 03:36 PM.

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Do Loop with Variable Range

    The last row of data in which sheet in which workbook?

    PS Can you use code tags when posting code?
    If posting code please use code tags, see here.

  3. #3
    Registered User
    Join Date
    07-16-2013
    Location
    operation
    MS-Off Ver
    Excel 2010
    Posts
    15

    Re: Do Loop with Variable Range

    Sorry about that. The last row of data in "Hotlist Template v2_1"

    Thanks for the tip on using Code Tags!

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Do Loop with Variable Range

    This would loop through column B starting at 2 and stepping 13 rows each iteration.

    I've shown how you could use I in the first few lines.

    I can't figure out the rest of the code, it's seems to jump about a fair bit, so I'm not sure how it would fit in there.

    Dim wbTemplate As Workbook
    Dim wbTools As Workbook
    Dim startLocation As Range
    Dim rowLocation As Range
    Dim I As Long
    
        For I = 2 To wbTemplate.Range("B" & Rows.Count).End(xlUp).Row Step 14
        
            Set startLocation = wbTemplate.ActiveSheet.Range("B" & I)
            Set rowLocation = startLocation.Offset(1)
            
            startLocation.Copy wbTools.Sheets("Family Tool").Range("B2")
    
       Next I

+ 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. Why did an inner loop variable start overwriting the outer loop range suddenly?
    By 111StepsAhead in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-16-2012, 03:24 PM
  2. [SOLVED] Referencing variable Range in a loop
    By Samirkc in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-14-2006, 07:55 AM
  3. How to reference variable range in a loop
    By Samirkc in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-13-2006, 11:15 AM
  4. [SOLVED] Setting a range within a loop variable for copy/paste
    By Craig in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-21-2005, 12:10 PM
  5. [SOLVED] Setting a range within a loop variable for copy/paste
    By Craig in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 12-20-2005, 06:25 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