Results 1 to 7 of 7
  1. #1
    2 Star Lounger
    Join Date
    Mar 2001
    Location
    San Clemente, California, USA
    Posts
    130
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Looping (Excel 97)

    Hi everyone,

    My VBA books go over looping, but don't give me an example for what I need to do.

    Column U consists of text and the amount of rows it contains changes depending on the data I receive

    I need to put the following formula in column T =right(U2,3) until there is no more data in column U

    My question is how do I loop through column U unitil it hits the end while putting the fomula in column T?

    Thanks from a VBA Newbie..

  2. #2
    Uranium Lounger
    Join Date
    Dec 2000
    Location
    Salt Lake City, Utah, USA
    Posts
    9,508
    Thanks
    0
    Thanked 6 Times in 6 Posts

    Re: Looping (Excel 97)

    Something like a for next loop:

    Sub formulainsert()
    Dim rngCell As Range, rngTextCells As Range
    On Error Resume Next ' in case no text cells are found
    Set rngTextCells = ActiveSheet.Columns("U").SpecialCells(xlCellTypeCo nstants, 2)
    If rngTextCells Is Nothing Then Exit Sub ' no text cells found
    For Each rngCell In rngTextCells
    rngCell.Offset(0, -1).FormulaR1C1 = "=RIGHT(RC[1],3)" 'set the formula
    Next rngCell
    Set rngTextCells = Nothing
    End Sub
    -John ... I float in liquid gardens
    UTC -7ąDS

  3. #3
    2 Star Lounger
    Join Date
    Mar 2001
    Location
    San Clemente, California, USA
    Posts
    130
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Re: Looping (Excel 97)

    Thank you John! You are my savior for the day [img]/forums/images/smilies/smile.gif[/img]

    I have a question about:

    Set rngTextCells = ActiveSheet.Columns("U").SpecialCells(xlCellTypeCo nstants, 2)

    What does .SpecialCells(xlCellTypeConstants, 2) do?

    Thanks again!

  4. #4
    Uranium Lounger
    Join Date
    Dec 2000
    Location
    Salt Lake City, Utah, USA
    Posts
    9,508
    Thanks
    0
    Thanked 6 Times in 6 Posts

    Re: Looping (Excel 97)

    SpecialCells is well documented in the VBA Help, but I think the easiest way to explain is that it's the same as using the "GoTo, Special" command in the spreadsheet environment, and I often record quick-&-dirty "goto, special" macros to remind myself what the Methods' parameters are.
    Attached Images Attached Images
    -John ... I float in liquid gardens
    UTC -7ąDS

  5. #5
    Gold Lounger
    Join Date
    Feb 2001
    Location
    Dublin, Ireland, Republic of
    Posts
    2,697
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: Looping (Excel 97)

    If all the cells in column U contain text you could also use something like :

    [T2].Formula = "=RIGHT(U2,3)"
    [T2].AutoFill Destination:=Range([T2], [U2].End(xlDown).Offset(0, -1))

    If you have many rows of data it may prove somewhat quicker.

    Andrew C

  6. #6
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Weert, Limburg, Netherlands
    Posts
    4,812
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Looping (Excel 97)

    Or:

    Range([T2], [U2].End(xlDown).Offset(0, -1)).Formula="=RIGHT(U2,3)"
    Jan Karel Pieterse
    Microsoft Excel MVP, WMVP
    www.jkp-ads.com
    Professional Office Developers Association

  7. #7
    2 Star Lounger
    Join Date
    Mar 2001
    Location
    San Clemente, California, USA
    Posts
    130
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Re: Looping (Excel 97)

    Thank you,

    Those short codes, worked great, and I can understand and apply them in the future!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •