New Excel Forum

This forum has been moved to TeachExcel.com

Ask all future questions in the New Excel Forum.

ExcelKey

Loops

Macros, VBA, Excel Automation, etc.

Loops

Postby dmaner » Thu Jan 19, 2012 2:58 pm

I am trying to figure out the best way to add a loop to my code. I would like for it to go through my entire code until column C no longer has any cells that say 'Employer ID:'.

Any suggestions?

Code: Select all
Windows("Employee Activity Roll Up.xlsx").Activate
Sheets("Sheet1").Select

Cells.Find(What:="Employee ID:", After:=ActiveCell, LookIn:=xlFormulas, _
        LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False).Offset(0, 0).Select
Selection.ClearContents

ActiveCell.Offset(0, -1).Select
Selection.Copy
Windows("NA GOLD adherence and activity graphs.xlsm").Activate
Range("B3").Select
    ActiveCell.Offset(1, 0).Select
    Do While Not IsEmpty(ActiveCell)
    ActiveCell.Offset(1, 0).Select
    Loop

    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
  • 0

dmaner
Rookie
 
Posts: 7
Joined: Jan 9, 2012
Reputation: 0

Re: Loops

Postby Don » Thu Jan 19, 2012 4:05 pm

Hi!

You could put the relevant code inside of this for loop with an if statement:

Code: Select all
Sub test_loop()

For i = 1 To Range("C" & Rows.Count).End(xlUp).Row

    If Cells(i, 3).Value = "Employer ID:" Then
   
        'PUT DESIRED CODE HERE
   
    End If
   
Next i

End Sub



Just replace 'PUT DESIRED CODE HERE with the relevant macro code that you want to run.

hope this helps :)
  • 0

Don
Moderator
 
Posts: 733
Joined: Dec 4, 2011
Reputation: 2
Excel Version: 2010

Re: Loops

Postby dmaner » Thu Jan 19, 2012 4:28 pm

Works like a charm, thank you!!
  • 0

dmaner
Rookie
 
Posts: 7
Joined: Jan 9, 2012
Reputation: 0


Return to Macros and VBA Questions

Who is online

Users browsing this forum: No registered users and 243 guests