Need Help Please - VBA code will not stop looping . . .

Lidsavr

Active Member
Joined
Jan 10, 2008
Messages
330
I am using Excel 2007 and writing code that both Excel 2007 and Excel 2003 users will run.

I am trying to select a range of part numbers in Column D and it finds the part number correctly and then in looping selects the three other instance of the same part number, but then instead of stopping the loop, continues to select all the way to the spreadsheet. I need it to stop with the last instance of the part number that is designated. In my test case, the part number is I am designating is: ARCT04371. I know there a four instances of this part in the spreadsheet (It makes it a good test of the program)

Here is the subroutine code I have developed so far. I do not know why it doesn't stop looping the do loop section is in red:

Code:
'   [COLOR="Green"]The user selects a part number from the list box and it is placed
'   into cell D1 on worksheet Bd_Pns[/COLOR]    
    Range(Selection, Selection.End(xlDown)).Select
    Set SRng = Selection
    Range("A1").Select
    With UserForm3.ListBox1
        .BoundColumn = 1
        .ColumnCount = 1
        .ControlSource = "'Bd_Pns'!$D$1"   'places the selection in cell D1
        .IntegralHeight = True
        .MultiSelect = 0
        .RowSource = SRng.Address
    End With
    If TypeName(Selection) = "Range" Then
        UserForm3.Show
    End If
    Sheets("Bd_PNs").Select
    Range("D1").Select
    MyVar1 = ActiveCell.Value

[COLOR="green"]'   Finds MyVar1 on the part number worksheet and selects
'   all instances[/COLOR]
    Sheets("ARCT00232").Select
    Range("D:D").Find(What:=MyVar1, After:=ActiveCell, LookIn:=xlFormulas, _
        LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False).Activate
        ActiveCell.Select
[COLOR="Red"]    Do
        If ActiveCell.Offset(1, 0).Value = MyVar1 Then
            Range(Selection, Selection.Offset(1, 0)).Select
        End If
    Loop Until ActiveCell.Offset(1, 0).Value <> MyVar1[/COLOR]

[COLOR="Green"]'   The problem is the code keeps selecting after the four instances of the 
'   part number.[/COLOR]

What am I missing here? I've spent over two hours trying different commands and syntax without success. Any help with the code is appreciated.

Thank you,

Charles
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Charles

I don't think you are actually moving anywhere.

This will find all the instances of the var1 after the first instance and create a reference rngAll to them.
Code:
    Set rng = ActiveCell
    
    Set rngAll = rng
 
    Do
 
        If rng.Offset(1).Value = var1 Then
            Set rngAll = Union(rngAll, rng.Offset(1))
        End If

        Set rng = rng.Offset(1)
 
    Loop Until rng.Value <> var1
 
    rngAll.Select
 
Upvote 0
Norie,
Thank you very much. I will test this code out this afternoon. It may meet my needs in other ways as well.

I appreciate the help!

Charles
 
Upvote 0
Charles

No problem.:)

Something I forgot to mention is that I've assumed that the values you are looking for are contiguous, eg in a 'block'.
 
Upvote 0

Forum statistics

Threads
1,224,506
Messages
6,179,158
Members
452,892
Latest member
yadavagiri

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top