+ Reply to Thread
Results 1 to 12 of 12

Better way than using a do until loop?

Hybrid View

mtt23 Better way than using a do... 09-05-2012, 05:03 AM
arlu1201 Re: Better way than using a... 09-05-2012, 06:33 AM
HaHoBe Re: Better way than using a... 09-05-2012, 06:44 AM
mtt23 Re: Better way than using a... 09-05-2012, 06:44 AM
mtt23 Re: Better way than using a... 09-05-2012, 06:58 AM
mtt23 Re: Better way than using a... 09-05-2012, 07:42 AM
arlu1201 Re: Better way than using a... 09-05-2012, 08:14 AM
JosephP Re: Better way than using a... 09-05-2012, 08:29 AM
mtt23 Re: Better way than using a... 09-05-2012, 09:03 AM
mtt23 Re: Better way than using a... 09-05-2012, 09:17 AM
JosephP Re: Better way than using a... 09-05-2012, 09:19 AM
mtt23 Re: Better way than using a... 09-05-2012, 09:22 AM
  1. #1
    Registered User
    Join Date
    03-08-2012
    Location
    Notts
    MS-Off Ver
    Excel 2010
    Posts
    71

    Question Better way than using a do until loop?

    Good Morning,

    In my excel sheet I have 2 columns of data, (previously exported from ms project earlier on in the macro) that I want to filter data out of.
    At the moment, I have tried to create a loop which searches column B starting at "B4" until it has either; found the term "Engineering" or comes to the end of the list without finding it, at which point the loop stops.
    If the term "Engineering" is found, the data in the cell 1 to the right is copied into cell "C20".

    Here is the code i put together below:

    Sub Filter()
    
        Range("B4").Select
    Do Until Selection.Value = ""
    If Selection.Value = "Engineering" Then
       Range("C20").Value = Selection.Offset(0, 1).Value
    Else
            Selection.Offset(1, 0).Select
    End If
    Loop
    
    End Sub
    At the moment the code just keeps looping or gets stuck on the term engineering if it is found.
    Can anyone improve upon this code or post/suggest an alternative to using such a loop? If done differently, the range to be checked/looped between is ("B4:B14").

    Any help greatly appreciated.
    Kind regards
    Last edited by arlu1201; 09-05-2012 at 06:13 AM. Reason: Replaced quote tags with code tags.

  2. #2
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Better way than using a do until loop?

    Will your list always be from B4 to B14? Or are there chances that more data is added, so your list can have any number of rows?
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  3. #3
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Better way than using a do until loop?

    Hi, mtt23,

    Sub Filter()
    
    Dim lngRowCounter As Long
    
    lngRowCounter = 4
        
    Do Until Cells(lngRowCounter, "B").Value = ""
      If Cells(lngRowCounter, "B").Value = "Engineering" Then
        Range("C20").Value = Cells(lngRowCounter, "C").Value
        Exit Do
      End If
      lngRowCounter = lngRowCounter + 1
    Loop
    
    End Sub
    Better way? Apply the Autofilter, check if there is data available, copy first line of visible cells under the heading, end Autofilter.

    Maybe check if there is a hit with what you search for by using WorksheetFunction.Countif(Range(B4:B14"), "Engineering") which will deliver zero for no hit and greater 0 for at least one hit.

    Ciao,
    Holger
    Last edited by HaHoBe; 09-05-2012 at 06:47 AM.

  4. #4
    Registered User
    Join Date
    03-08-2012
    Location
    Notts
    MS-Off Ver
    Excel 2010
    Posts
    71

    Re: Better way than using a do until loop?

    There will never be more than 10 cells down to check. There may possibly be less than 10, which was where I was trying to bring in the;
    Do Until Selection.Value = ""
    But I presume it wouldn't make a difference if I got it to just check all cells from ("B4:B14") anyway? May mean I don't need to use a loop?

  5. #5
    Registered User
    Join Date
    03-08-2012
    Location
    Notts
    MS-Off Ver
    Excel 2010
    Posts
    71

    Re: Better way than using a do until loop?

    Ciao Holger,

    Thanks for the reply, autofilter is out of the question for that column because I have another table further down the page that it would interfere with.
    I will give the code a try;... it looks very simular to mine, just different method of cell location.

    Grazie

  6. #6
    Registered User
    Join Date
    03-08-2012
    Location
    Notts
    MS-Off Ver
    Excel 2010
    Posts
    71

    Re: Better way than using a do until loop?

    I tested your code but for me it just kept looping round without moving anywhere when I stepped through it.


  7. #7
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Better way than using a do until loop?

    What if you find multiple cells containing the word "Engineering", where should the value of 1 cell to the right go?

  8. #8
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Better way than using a do until loop?

    Holger's code oughta work-you could also use a for next loop
    Sub Filter()
    
    Dim lngRowCounter As Long
    
    For lngRowCounter = 4 to 14
      If lcase(Cells(lngRowCounter, "B").Value) = "engineering" Then
        Range("C20").Value = Cells(lngRowCounter, "C").Value
        Exit For
      End If
    Next lngRowCounter
    
    End Sub
    Josie

    if at first you don't succeed try doing it the way your wife told you to

  9. #9
    Registered User
    Join Date
    03-08-2012
    Location
    Notts
    MS-Off Ver
    Excel 2010
    Posts
    71

    Re: Better way than using a do until loop?

    Arlette, there would be no chance that there would be more than 1 cell containing the word engineering within the range.

  10. #10
    Registered User
    Join Date
    03-08-2012
    Location
    Notts
    MS-Off Ver
    Excel 2010
    Posts
    71

    Re: Better way than using a do until loop?

    I've cracked it! I made a few alterations to how the loop is controlled/conditions:

    Sub Filter()
    
        Range("B4").Select
    Do
    If Selection.Value = "1a_Eng (Mechanical)" Then
       Range("C20").Value = Selection.Offset(0, 1).Value
    Else
            Selection.Offset(1, 0).Select
    End If
    Loop Until Selection.Value = "" Or Range("C20").Value = Selection.Offset(0, 1).Value
    
    End Sub
    It loops until a blank cell is reached at the end, or the engineering value is found in cell C20.
    Last edited by mtt23; 09-05-2012 at 10:04 AM. Reason: replaced quote tags with code tage

  11. #11
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Better way than using a do until loop?

    I reckon I can see now why the suggested codes didn't work ;-)

  12. #12
    Registered User
    Join Date
    03-08-2012
    Location
    Notts
    MS-Off Ver
    Excel 2010
    Posts
    71

    Re: Better way than using a do until loop?

    Yeah - they just kept looping through.

    Thanks for your input guys - all due some rep

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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.6.0 RC 1