Consulting

Results 1 to 16 of 16

Thread: Looping through Range Vs Ranges

  1. #1
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,064
    Location

    Looping through Range Vs Ranges

    As I understand it, one can loop through a range, cell by cell, by using "For each cell in Range". However if you want to loop through more than one range, you need to use ".cells".

    Would someone kindly step forward and provide me with the breakdown on this,please? Sort of "Looping 101", in ordinary english.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Let me give you a hint Ted, and see if that addresses where your mind is going.

    Lookup Areas in VBA help.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    As I understand it, [VBA]For Each cell in Range[/VBA] is looping through the default property of Range. (Which is .Item)
    Other Properties have different defaults and the collection or array to loop through must be specified.

    I can't find the list of default properties in the VBA help, but I've seen it once.

  4. #4
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,064
    Location
    Areas isn't where I'm trying to head Bob. I have included a workbook showing what I'm chasing.

    I am using data validation to select one of four values, ( "<LOR", "<AL", ">AL", ">MRL"), but now require to apply conditional formatting, so that if the left character of the value in the cell equals ">" then the font turns red.

    I had initially been thinking about using a macro to loop through the ranges, but in hindsight I might be better off just using CF.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  5. #5
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Why not use Conditional Formatting

    Formula Is =LEFT(A1,1)=">"
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  6. #6
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,064
    Location
    Thanks MD, I'm on it as we speak
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  7. #7
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    Ted i havent looked at your example but conditional formatting is the way to go =LEFT(D3,1)="<"
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  8. #8
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    Wow! it was crowded in that post!
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  9. #9
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,064
    Location
    Further to the above request, I'm guessing that I'll have to use vba here, since I'll be using this on a sheet where each of the rows will be cleared and reset every so often.

    [VBA]Private Sub Worksheet_SelectionChange(ByVal Target as Range)
    Dim rngDataval As Range
    Dim c As Range

    Set Dataval = Range ( "B1:B5","D15")

    For Each C in Range
    If Left(C,1)=">" Then
    .Font.ColorIndex = 3
    End If
    Next
    End Sub[/VBA]

    Doesn't seem right somehow?
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  10. #10
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    errrr....Ted theres a fair few typos there think you need to invest in option explicit! and you would be better off using the target intersect to apply your formatting.
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  11. #11
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,064
    Location
    Back to this issue...

    As this is a Worksheet_Change type event for which I already have in place to do other things required, I am looking to call this sub. Testing is for column K at this stage but eventually will be for columns K,M,O,Q,S & U.

    [VBA]Sub DoResults(Target As Range)
    Dim ResCell As Range
    Dim rngResults As Range
    ' determines the initial font colour
    With Range("K3:K177")
    .Font.ColorIndex = xlAutomatic
    End With

    For Each ResCell In Range("K3:K177")
    If Left(ResCell.Value, 1) = ">" Then
    With Range("K3:K177")
    .Font.ColorIndex = 3
    End With
    End If
    Next
    End Sub[/VBA]

    Doesn't work as it errors out at line If Left(ResCell.Value, 1) = ">" Then

    What is the alternative?
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  12. #12
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    I'm not sure what it is you want ted but this works:
    [VBA]
    Sub DoResults()
    Dim ResCell As Range
    Dim rngResults As Range
    ' determines the initial font colour
    With Range("K3:K177")
    .Font.ColorIndex = xlAutomatic
    End With

    For Each ResCell In Range("K3:K177")
    If Left(ResCell.Value, 1) = ">" Then
    ResCell.Font.ColorIndex = 3
    End If
    Next
    End Sub
    [/VBA]
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  13. #13
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,064
    Location
    Currently I am unable to get this code of Simon's to trigger. Therefore I'm assuming it must then be in the earlier section of code.
    [VBA]
    Option Explicit
    Option Compare Text
    Const gsPassword As String = "Shona"

    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim testFlag As Boolean
    Dim ResCell As Range
    Dim rngResults As Range

    Me.Unprotect (gsPassword)
    If Target.Cells.Count > 1 Then Exit Sub
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    If Target.Column = 11 And Target.Row > 1 Then
    'Determine the result type
    If Left(ResCell.Value, 1) = ">" Then DoResults
    End If
    If Target.Column = 23 And Target.Row > 1 Then
    'Copy data to BucketHistory and clear data from worksheet
    If Target = "Cleared" Then DoClear Target

    If Target = "Hold" Then DoHold Target
    End If
    Application.EnableEvents = True
    Application.ScreenUpdating = True
    On Error Resume Next
    testFlag = Evaluate(Environ("Ted Testing"))
    If Not testFlag Then
    Me.Protect (gsPassword)
    End If
    On Error GoTo 0

    End Sub
    Sub DoResults(ByVal Target As Range)
    Dim ResCell As Range
    Dim rngResults As Range
    ' determines the initial font colour
    With Range("K3:K177")
    .Font.ColorIndex = xlAutomatic
    End With

    For Each ResCell In Range("K3:K177")
    If Left(ResCell.Value, 1) = ">" Then
    ResCell.Font.ColorIndex = -16776961
    End If
    Next
    End Sub
    [/VBA]

    The purpose of the sub is to change the font colour of any cell within the range K3:K177 if its value starts with ">"
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  14. #14
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Hi Ted,
    ResCell has not been set when you test it
    [VBA] 'Determine the result type
    If Left(ResCell.Value, 1) = ">" Then DoResults
    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  15. #15
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    To make your code suit the multiple column scenario

    [VBA]
    If Target.Row > 1 Then
    Select Case Target.Column
    Case 11, 13, 15, 17, 19, 21
    If Left(Target, 1) = ">" Then DoResults Target.Column
    Case 23
    'Copy data to BucketHistory and clear data from worksheet
    If Target = "Cleared" Then DoClear Target
    If Target = "Hold" Then DoHold Target
    End Select
    End If

    Sub DoResults(Col As Long)
    Dim ResCell As Range
    Dim rngResults As Range

    Set rngResults = Range(Cells(3, Col), Cells(177, Col))
    ' determines the initial font colour
    With rngResults
    .Font.ColorIndex = xlAutomatic
    End With

    For Each ResCell In rngResults
    If Left(ResCell.Value, 1) = ">" Then
    ResCell.Font.ColorIndex = -16776961
    End If
    Next
    End Sub

    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  16. #16
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,064
    Location
    Thank you MD.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

Posting Permissions

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