+ Reply to Thread
Results 1 to 7 of 7

Looping a Macro

Hybrid View

  1. #1
    Registered User
    Join Date
    05-28-2015
    Location
    Lakeland, FL
    MS-Off Ver
    Excel 13
    Posts
    66

    Looping a Macro

    How do I loop this Macro but instead of B2 use C2?

    Formula: copy to clipboard
    Sub Relocation_Priority()

    Dim ws As Worksheet: Set ws = Worksheets("Example")
    lRow = Worksheets(ws.Range("B2").Value).Range("A" & Rows.Count).End(xlUp).Row
    startRow = 4

    With Sheets("Example")
    For i = 2 To lRow
    .Range("A" & startRow).Resize(1).Value = Sheets(ws.Range("B2").Value).Range("A" & i).Value
    .Range("B" & startRow).Resize(1).Value = Sheets(ws.Range("B2").Value).Range("B" & i).Value
    .Range("E" & startRow).Resize(1).Value = Sheets(ws.Range("B2").Value).Range("E" & i).Value
    .Range("F" & startRow).Resize(1).Value = Sheets(ws.Range("B2").Value).Range("F" & i).Value
    .Range("G" & startRow).Resize(1).Value = Sheets(ws.Range("B2").Value).Range("G" & i).Value
    .Range("H" & startRow).Resize(1).Value = Sheets(ws.Range("B2").Value).Range("H" & i).Value
    .Range("I" & startRow).Resize(1).Value = Sheets(ws.Range("B2").Value).Range("I" & i).Value
    .Range("J" & startRow).Resize(1).Value = Sheets(ws.Range("B2").Value).Range("J" & i).Value
    .Range("K" & startRow).Resize(1).Value = Sheets(ws.Range("B2").Value).Range("K" & i).Value
    startRow = startRow + 1
    Next

    End With
    End Sub

  2. #2
    Valued Forum Contributor Gatti's Avatar
    Join Date
    06-08-2015
    Location
    Brasil, São Paulo, Ribeirão Preto
    MS-Off Ver
    365
    Posts
    346

    Re: Looping a Macro

    Well... changing B2 to C2?

  3. #3
    Registered User
    Join Date
    05-28-2015
    Location
    Lakeland, FL
    MS-Off Ver
    Excel 13
    Posts
    66

    Re: Looping a Macro

    I mean I would like to continue the Macro and right after it runs B2 I would like it to do the same exact thing for C2.

  4. #4
    Valued Forum Contributor Gatti's Avatar
    Join Date
    06-08-2015
    Location
    Brasil, São Paulo, Ribeirão Preto
    MS-Off Ver
    365
    Posts
    346

    Re: Looping a Macro

    So... what is the real problem here?

    You already know what has to be done, just "double" your code and replace B to C

    Not the best way though, but you have the code done

  5. #5
    Registered User
    Join Date
    05-28-2015
    Location
    Lakeland, FL
    MS-Off Ver
    Excel 13
    Posts
    66

    Re: Looping a Macro

    Yeah I understood after I replied the last time! and I know just the best way I knew how. Thank you!

  6. #6
    Registered User
    Join Date
    05-28-2015
    Location
    Lakeland, FL
    MS-Off Ver
    Excel 13
    Posts
    66

    Re: Looping a Macro

    If C2 is blank is there a way to stop the macro so it doesn't get an error?

  7. #7
    Valued Forum Contributor Gatti's Avatar
    Join Date
    06-08-2015
    Location
    Brasil, São Paulo, Ribeirão Preto
    MS-Off Ver
    365
    Posts
    346

    Re: Looping a Macro

    Put an If before the C2 code.

    This way:

    If sheet.range("$C$2").value <> "" Then
           'Your code here
    End if

+ 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. MACRO Looping Help
    By Kymera99 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 09-29-2015, 12:51 PM
  2. [SOLVED] Looping macro
    By lashellr in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-18-2015, 09:36 PM
  3. Macro (Looping)
    By daviieejay in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-01-2013, 05:05 PM
  4. Looping macro, possible?
    By ad9051 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-29-2012, 08:16 AM
  5. looping macro
    By dperry in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-02-2009, 06:26 PM
  6. Looping Macro
    By nickymac in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-17-2008, 09:18 AM
  7. Macro:Looping
    By cart0250 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-31-2006, 04:47 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