+ Reply to Thread
Results 1 to 2 of 2

Cannot use .Find commands as part of mini loops inside a larger loop

  1. #1
    Registered User
    Join Date
    12-02-2016
    Location
    London, England
    MS-Off Ver
    2013
    Posts
    1

    Cannot use .Find commands as part of mini loops inside a larger loop

    Hello all,

    I am trying to write a loop with two mini internal loops as part of a larger process.

    The whole major looped process is located in one sheet (“Set up”).

    It is intended to take a value (starts at 1) in cell B1 and search for that value in a fixed range E3:E500. I use a formula in cell C1 to determine if that search value exists in the range or not. If not, it increases the value in B1 by 1 and searches again, increasing the value by 1 each time (1 then 2 then 3 etc.) within the first mini loop until it finds the value in search range. When it finds the first instance of the value it copies the data in same row offset by 1 to 14 columns to right of the found value. So far so good although I had to use an alternative to the .find function to get it to work (not shown here).

    The process is then designed to then take the copied data and find the LAST instance of the same value in the same search range and paste the data into the corresponding offset columns to the right. This is where I have hit a dead end, as I can't find any other function than .find to get and select the location of the last instance in the range without using the .find command.

    The second mini loop is then intended to find each of the instances in the range of the search value and clear the contents of the offset column until there's only one instance left (the last instance against which the data has just been copied). This is dictated by a formula in cell C1 which does the count of the value and some other criteria which I couldn't write the code for. Once there's only 1 instance of the value left, it is then supposed to exit the second loop and increase the search value in B1 by 1 and start the whole loop again to find the new value. Every time I run it the debug highlights the following rows of code which use the find command.

    grprange.Find(grpnumber).Activate

    Set myC = grprng.Find(grpnumber, , , , , xlPrevious)

    It throws up the following error message "Run-time error '91': Object variable or With block variable not set". I assume this is because the find function is now fragmenting the loop functions.

    Below is code, please let me know where / why I am going wrong and if there’s an alternative (I am sure there is) which someone of my limited vba skills could use.

    Thanks

    Dim countocc As Range
    Dim endproc As Range
    Dim grprng As Range
    Dim grpnumber As String
    Dim grpnum As Range

    grpnumber = ThisWorkbook.Worksheets("Set Up").Range("$B$1")
    Set grpnum = ThisWorkbook.Worksheets("Set Up").Range("$B$1")

    Set endproc = ThisWorkbook.Worksheets("Set Up").Range("$A$1")
    Set countocc = ThisWorkbook.Worksheets("Set Up").Range("$C$1")

    ‘main loop start
    Do While grpnum <= endproc

    ‘mini loop 1 start

    Do While countocc = 0

    grpnum.Value = grpnum + 1

    Loop

    Set grprng = ThisWorkbook.Worksheets("Set Up").Range("E3:E500")
    grpnumber = ThisWorkbook.Worksheets("Set Up").Range("$B$1")

    grprange.Find(grpnumber).Activate

    Range(ActiveCell.Offset(0, 1), ActiveCell.Offset(0, 14)).Copy

    Dim myC As Range
    Set myC = grprng.Find(grpnumber, , , , , xlPrevious)

    myC.Activate

    ActiveCell.Offset(0, 1).Select

    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False

    grprng.Find(grpnumber).Select

    Range(ActiveCell.Offset(0, 1), ActiveCell.Offset(0, 14)).Select
    Selection.ClearContents

    ‘mini loop 2 start

    Dim r As Range
    Dim v As Variant

    For Each r In Intersect(Range("E2:E500"), ActiveSheet.UsedRange)
    v = r.Value
    If Range("$C$1") = 1 Then Exit For
    If InStr(v, grpnumber) > 0 Then
    r.Select
    Range(Selection.Offset(0, 1), Selection.Offset(0, 14)).ClearContents
    End If

    Next r

    Range("B1").Select
    Selection.Value = Range("B1") + 1

    Loop

  2. #2
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Cannot use .Find commands as part of mini loops inside a larger loop

    Welcome to the Forum.

    unfortunately your post will upset our moderators do few people will respond.

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code between [CODE]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Click on Edit to open your thread, then highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here

    (This thread should receive no further responses until this moderation request is fulfilled, as per Forum Rule 7)
    My General Rules if you want my help. Not aimed at any person in particular:

    1. Please Make Requests not demands, none of us get paid here.

    2. Check back on your post regularly. I will not return to a post after 4 days.
    If it is not important to you then it definitely is not important to me.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Replies: 1
    Last Post: 11-04-2016, 06:50 PM
  2. ReDim array inside nested loops
    By puzzlelover22 in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 10-13-2016, 11:20 AM
  3. Replies: 2
    Last Post: 11-07-2014, 10:45 AM
  4. [SOLVED] find the part of text and delete the row and loop continues
    By vimalanathk in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 09-03-2014, 11:23 AM
  5. [SOLVED] How to restrict pop of message box inside multiple times when it is inside a for loop?
    By timmu in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-26-2014, 06:00 AM
  6. Find/Replace Macro Error 1004 part way though loop?
    By macronoob123 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 10-03-2011, 06:34 PM
  7. Loops to find blanks then loop to find populated
    By Bevy in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-01-2006, 03:55 PM

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