Loop Ends After First Match

Riddlemethis

New Member
Joined
Apr 20, 2021
Messages
14
Office Version
  1. 2013
Platform
  1. Windows
Script below:

Dim rng As Range
Set rng = Range("BJ5:BJ500")

Sheets("Daily Runboard").Activate
For Each cell In rng
If cell.Value = "MCC" Then
cell.Select
ActiveCell.Offset(0, -59).Select
ActiveCell.Resize(11, 22).Copy
Sheets("H Runs").Select
Range("A" & Rows.Count).End(xlUp).Offset(1).Select
ActiveCell.PasteSpecial xlPasteValues
ActiveCell.PasteSpecial xlPasteFormats
End If
Next

End Sub

So works perfectly at the first cell.value match in the range where it selects the cell, offsets where i need it too, changes range size, copy's, move sheets and pastes correctly ( text and format) too the next relevant empty cell in the sheet i need it too. Problem being it seems to circle the loop but not go down the range correctly after the first cycle where it works OK.. After the first match it seems to cycle through where it no longer meets the correct cell.value but yet it should?
In this specific data dump the next cells down is blank in the range all the way down to where the next cell in BJ5:BJ500 with a vlaue also contains the correct cell.value "MCC" but it passes through the loop when I step by step it as in it doesn't reference the other cells in the rng

What am i doing wrong?
 
Last edited:

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
On your first match, your code selects Sheets("H Runs").

On the second match, your code tries: cell.Select

But cell is on Sheets("Daily Runboard"), and you need to activate the sheet before you can select a cell on that sheet. (You have Sheets("Daily Runboard").Activate before you start the loop).

If your code isn't erroring, then presumably you have an On Error Resume Next somewhere above? It's bad coding practice to do this.

But in any event, you can get rid of all the .Activate and .Select. It's inefficient, and prone to error relying on which sheet/cell is Active.

VBA Code:
Sub Test()

    Dim r As Range
    Dim LastRow As Long
        
    LastRow = Sheets("H Runs").Range("A" & Rows.Count).End(xlUp).Row
    
    For Each r In Sheets("Daily Runboard").Range("BJ5:BJ500")
        If r.Value = "MCC" Then
            r.Offset(0, -59).Resize(11, 22).Copy
            With Sheets("H Runs").Range("A" & LastRow + 1)
                .PasteSpecial xlPasteValues
                .PasteSpecial xlPasteFormats
            End With
            LastRow = LastRow + 11
        End If
    Next r

End Sub
 
Upvote 0
Hi @StephenCrump thanks for your help here, it is appreciated.

So I tried your code which works fine as long as my first value = "MCC" is in the first cell in the range aka "BJ5". It then however moves to Next r and cycles through but doesn't find any other matches in the range even though they exist? Also won't find any matches if the value in "BJ5" is not "MCC".

Any ideas on what I'm doing wrong?
 
Upvote 0
I've tested using:

ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAHAIAJAKALAMANAOAPAQARASATAUAVAWAXAYAZBABBBCBDBEBFBGBHBIBJ
1
2
3
4
5
6
7
8
9Alpha1Omega1MCC
10Alpha1Omega1
11Alpha1Omega1
12Alpha1Omega1
13Alpha1Omega1
14Alpha1Omega1
15Alpha1Omega1
16Alpha1Omega1
17Alpha1Omega1
18Alpha1Omega1
19Alpha1Omega1
20
21
22
23
24Alpha2Omega2MCC
25Alpha2Omega2
26Alpha2Omega2
27Alpha2Omega2
28Alpha2Omega2
29Alpha2Omega2
30Alpha2Omega2
31Alpha2Omega2
32Alpha2Omega2
33Alpha2Omega2
34Alpha2Omega2
35
Daily Runboard


Before running code:

AB
1Blah
2Blah
3Blah
4Blah
5Blah
6Blah
7
8
H Runs


After running code:

ABCDEFGHIJKLMNOPQRSTUVW
1Blah
2Blah
3Blah
4Blah
5Blah
6Blah
7Alpha1Omega1
8Alpha1Omega1
9Alpha1Omega1
10Alpha1Omega1
11Alpha1Omega1
12Alpha1Omega1
13Alpha1Omega1
14Alpha1Omega1
15Alpha1Omega1
16Alpha1Omega1
17Alpha1Omega1
18Alpha2Omega2
19Alpha2Omega2
20Alpha2Omega2
21Alpha2Omega2
22Alpha2Omega2
23Alpha2Omega2
24Alpha2Omega2
25Alpha2Omega2
26Alpha2Omega2
27Alpha2Omega2
28Alpha2Omega2
29
H Runs


which I think is what you intended?

Note that this line is an exact match, and that the VBA string comparison is case sensitive:

VBA Code:
If r.Value = "MCC" Then

You could allow for variations such as "mcc" or "Mcc " by testing:

Code:
If UCase(Trim(r.Value)) = "MCC" Then
 
Upvote 0
ahh ok, apologies i probably should of mentioned this or not been lazy and given sample data. So I think where it's going wrong is that there are gaps in between the cells in column "BJ:BJ" so it seems to end when it reaches a blank cell.
I tested your code again manually changing the data dump to have no blanks and it works so my bad. Is there anyway to change it to check through the full range? Or if it helps at all the only cell entry filled out would be exactly 11 cells below the one above starting at "BJ5"
 
Upvote 0
So I think where it's going wrong is that there are gaps in between the cells in column "BJ:BJ" so it seems to end when it reaches a blank cell.
No, that's not the problem. My sample data also has gaps in BJ:BJ. The code checks every cell in the range BJ5:BJ500, blank or not.

Or if it helps at all the only cell entry filled out would be exactly 11 cells below the one above starting at "BJ5"
I did wonder whether your data was regular. In this case it will make sense to step through the rows in increments of 11, i.e. testing BJ5, BJ16, BJ27 .. etc.

But first, let's try to understand why the existing code isn't working for you. Is my sample working correctly? What's different about your data?
 
Upvote 0
Actual example range attached. So i've tested it originally assuming value of the cell not actually being "MCC" and being value of left C5 was going to be the problem by changing the cell value to "MCC" however either way of actual text or 'left c5' only works if the first value in the range specified is "MCC". It does however work either way but won't search any further down the range after that.


DRB Test.xlsm
BJ
1
2
3
4
5MOS
6
7
8
9
10
11
12
13
14
15
16MCC
17
18
19
20
21
22
23
24
25
26
27FRE
28
29
30
31
32
33
34
35
36
37
38MCC
39
40
41
42
43
44
45
46
47
48
49MCC
50
51
52
53
54
55
56
57
58
59
60FRE
61
62
63
64
65
66
67
68
69
70
71MCC
72
73
74
75
76
77
78
79
80
81
Daily RunBoard
Cell Formulas
RangeFormula
BJ5,BJ71,BJ60,BJ49,BJ38,BJ27,BJ16BJ5=LEFT(C5,3)
 
Upvote 0
Using your column BJ (values or formula), my code copies four ranges to Sheets("H Runs"): C16:X26, C38:X48, C49:X59, and C71:X81.

At minimum, you must have values in cells C16, C38, C49 and C71, i.e. given that =Left(C16,3) returns "MCC" in cell BJ16 etc. So at minimum, you should see four "MCC*" copied to column A in Sheets("H Runs")?

Are you saying that for the example you've posted, nothing gets copied to Sheets("H Runs")?
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,175
Members
453,021
Latest member
Justyna P

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