+ Reply to Thread
Results 1 to 4 of 4

Thread: Understanding Step - 1 syntax in for loop. VBA

  1. #1
    Valued Forum Contributor
    Join Date
    10-21-2011
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    423

    Understanding Step - 1 syntax in for loop. VBA

    Hi there,

    I got this code from the website, and I am having trouble interpreting what the "step - 1" represents. I have seen this construct frequently these days, but I don't understand how it works.

    The code is supposed to highlight duplicates in color red according its original description.

    Sub DupsRed() 
    Application.ScreenUpdating = False
    Rng = Selection.Rows.Count
    For i = Rng To 1 Step -1
    myCheck = ActiveCell
    ActiveCell.Offset(1, 0).Select
    For j = 1 To i
    If ActiveCell = myCheck Then
    Selection.Font.Bold = True
    Selection.Font.ColorIndex = 3
    End If
    ActiveCell.Offset(1, 0).Select
    Next j
    ActiveCell.Offset(-i, 0).Select
    Next i
    Application.ScreenUpdating = True
    End Sub
    Thanks

  2. #2
    Forum Guru Domski's Avatar
    Join Date
    12-14-2009
    MS-Off Ver
    What does it matter?
    Posts
    3,933

    Re: Understanding Step - 1 syntax in for loop. VBA

    Step - 1 literally means it will work through a loop count backwards one at a time.

    I'm not sure that it's necessary in this case.

    Dom
    "May the fleas of a thousand camels infest the crotch of the person who screws up your day and may their arms be too short to scratch..."

    Use code tags when posting your VBA code: [code] Your code here [/code]

    Remember, saying thanks only takes a second or two. Click the little star to give some Rep if you think an answer deserves it.

  3. #3
    Valued Forum Contributor
    Join Date
    10-21-2011
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    423

    Re: Understanding Step - 1 syntax in for loop. VBA

    thank you Dom

  4. #4
    Valued Forum Contributor OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,647

    Re: Understanding Step - 1 syntax in for loop. VBA

    Just to add a bit more info:

    The Step n part is optional - if omitted it defaults to Step 1.
    The n part can be any number, positive or negative - it does not have to be an integer.

    Some demonstrations
    Sub demo_step_loops()
        Dim n As Long
        Dim d As Double
        Dim strTemp As String
        For n = 1 To 10 Step 1
            strTemp = strTemp & vbLf & n
        Next n
        MsgBox Mid$(strTemp, 2)
        strTemp = ""
        For n = 10 To 1 Step -2
            strTemp = strTemp & vbLf & n
        Next n
        MsgBox Mid$(strTemp, 2)
        strTemp = ""
        For d = 10 To 1 Step -0.5
            strTemp = strTemp & vbLf & d
        Next d
        MsgBox Mid$(strTemp, 2)
    End Sub
    I would generally advise not using Step 0 though...
    Good luck.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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.2.0