Consulting

Results 1 to 10 of 10

Thread: Do While Loop Help

  1. #1
    VBAX Contributor
    Joined
    Nov 2007
    Posts
    144
    Location

    Do While Loop Help

    Hey people,

    I am trying to write some code that will find a value in a selected column, and once it is found, do some formatting on that row.

    The steps are meant to be as follows:

    Find last active row - This bit works fine

    Starting in Row 3 Column 2, search down until cell value is "Leadership & Management Total"

    Once value is found, select that cell and then select over to the last active column in that row.

    The problem:

    I had a previous Do Loop Until code that worked fine but i had to modify it as the headings I was looking for were not always there (depending on data). This threw up an error as the loop kept on going until the end of the sheet.

    This code is meant to be conditional - i.e. run the code, find the value, if value is there, do teh formatting bit, if value is not there, exit do loop.

    [vba]
    a = 3
    Do While a <= lastactiverow
    If Cells(a, 2) = "Leadership & Management Total" Then
    Cells(a, 2).Select
    'You can ignore this formatting bit, as i know this works
    'Range(Selection, Cells(a, LastCol)).Select
    'With Selection.Interior
    '.ColorIndex = 15
    '.Pattern = xlSolid
    '.PatternColorIndex = xlAutomatic
    'End With
    'Selection.Font.ColorIndex = 2
    'With Selection
    '.HorizontalAlignment = xlLeft
    '.VerticalAlignment = xlBottom
    '.WrapText = False
    '.Orientation = 0
    '.AddIndent = False
    '.IndentLevel = 0
    '.ShrinkToFit = False
    '.ReadingOrder = xlContext
    '.MergeCells = False
    'End With
    Else
    Exit Do
    End If
    a = a + 1
    Loop
    [/vba]

    Hope someone can sort this out

    And as always, thanks for the help
    Last edited by f2e4; 05-23-2008 at 02:33 AM.

  2. #2
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    Well i don't know if you have lots of the same title on the sheet but for a one off this will work:
    [VBA]
    Dim Rng As Range
    Set Rng = Range(Cells(3, 2), Cells(Rows.Count, 2).End(xlUp))
    With ActiveSheet.Range(Rng.Address)
    Cells.Find(What:="Leadership & Management Total", After:=ActiveCell, _
    LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, _
    SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).Activate
    End With
    [/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)

  3. #3
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    On second thoughts this will loop through all the cells in the range and find occurences, just change the MsgBox statement for your copy statement!
    [VBA]
    Public Sub FindMyWord()
    Dim rFound As Variant, Rng As Range
    Set Rng = Range(Cells(3, 2), Cells(Rows.Count, 2).End(xlUp))
    With Rng
    Set rFound = .Find("Leadership & Management Total", LookIn:=xlValues)
    If Not rFound Is Nothing Then
    FirstAddress = rFound.Address
    Do
    If Not rFound.Address = FirstAddress Then LastAddress = rFound.Address
    Set rFound = .FindNext(rFound)
    MsgBox rFound.Address
    Loop While Not rFound Is Nothing And rFound.Address <> FirstAddress
    End If
    End With
    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)

  4. #4
    VBAX Contributor
    Joined
    Nov 2007
    Posts
    144
    Location
    Quote Originally Posted by Simon Lloyd
    Well i don't know if you have lots of the same title on the sheet but for a one off this will work:
    [vba]
    Dim Rng As Range
    Set Rng = Range(Cells(3, 2), Cells(Rows.Count, 2).End(xlUp))
    With ActiveSheet.Range(Rng.Address)
    Cells.Find(What:="Leadership & Management Total", After:=ActiveCell, _
    LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, _
    SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).Activate
    End With
    [/vba]
    Hi Simon,

    Thanks for the reply.

    I'm trying out your code now and it does work with finding the value - they are only one-off values.

    But now i'm not sure how to do my range selection for that whole row up to the last active column like before:

    [vba]
    Range(Selection, Cells(a, LastCol)).Select
    [/vba]
    Last edited by f2e4; 05-23-2008 at 03:36 AM.

  5. #5
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    perhaps[VBA]
    Range(ActiveCell, Cells(a, LastCol)).Select
    [/VBA]Subscript 9 suggests that the particular sheet you are referencing doesnt exist, check spelling and/or spaces in the name.
    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)

  6. #6
    VBAX Contributor
    Joined
    Nov 2007
    Posts
    144
    Location
    Quote Originally Posted by Simon Lloyd
    perhaps[vba]
    Range(ActiveCell, Cells(a, LastCol)).Select
    [/vba]Subscript 9 suggests that the particular sheet you are referencing doesnt exist, check spelling and/or spaces in the name.
    Yeah the subscript thing was a spelling mistake by me

    The code below worked before as 'a' was stored as a row reference but now with your new code:

    [VBA]
    Dim Rng As Range
    Set Rng = Range(Cells(3, 2), Cells(Rows.Count, 2).End(xlUp))
    With ActiveSheet.Range(Rng.Address)
    Cells.Find(What:="Leadership & Management Total", After:=ActiveCell, _
    LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, _
    SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).Activate
    End With
    [/VBA]

    'a' is not used.

    What can i replace 'a' with to put in the new row reference:

    [VBA]Range(ActiveCell, Cells(a, LastCol)).Select[/VBA]

    I tried this for the hell of it but this selects everything:

    [VBA]Range(ActiveCell, Cells(Rows.Count, LastCol)).Select[/VBA]

  7. #7
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    Because the code i supplied "Activates" a cell a now becomes ActiveCell.Row
    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
    VBAX Contributor
    Joined
    Nov 2007
    Posts
    144
    Location
    yep that works fine now

    I have a total of 5 headings:

    Leadership & Management Total
    Projects Total
    Prospects (Live) Total
    Prospects (Potential) Total
    Other Total

    So i have had to duplicate your code for each one of these headings

    However, my final problem is now that if one of these headings is not on this particular report, i get an error.

    Is there any way to add a condition so that if the heading is not in that report, then skip that bit of code?

  9. #9
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    Firstly have the headings in a list on another sheet and use a for next loop to go through them all, secondly you need to add error handling in the form of On Error Goto or using an If statement, have a little experiment with it.
    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)

  10. #10
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    Your loop looks like this:
    [VBA]
    Dim Rng As Range ,Rng1 As Range,MyCell as Range
    Set Rng = Range(Cells(3, 2), Cells(Rows.Count, 2).End(xlUp))
    Set Rng1 = Range......enter the range of your list
    With ActiveSheet.Range(Rng.Address)
    For Each MyCell In Rng1
    Cells.Find(What:="Leadership & Management Total", After:=ActiveCell, _
    LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, _
    SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).Activate
    Next MyCell
    End With
    [/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)

Posting Permissions

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