VBA First 5 rows for each ID change

Stephen_IV

Well-known Member
Joined
Mar 17, 2003
Messages
1,168
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Good morning,

I need to check the first 5 values for each ID change. If the first 5 rows are either (EA or MA) then it receives an X if not then blank. PS, I have about 150,000 rows. Thanks in advance!

First 5 of each group.xlsm
ABCDEF
1111EA111X
2111AB222
3111EA333X
4111EA444X
5111EA555
6111MA
7111AB
8222AB
9222EA
10222EA
11222EA
12222EA
13222EA
14333EA
15333EA
16333EA
17333EA
18333EA
19333EA
20444MA
21444MA
22444EA
23444EA
24444EA
25444EA
26555EA
27555AB
28555MA
29555MA
30555MA
31555AB
Sheet2
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
  1. Why does 111 get an X when the second value is "AB"?
 
Upvote 0
Sorry Peter_SSs you are right it should not have an X
 
Upvote 0
Give this a try.

VBA Code:
Sub EAMAx5_1()
  Dim a As Variant, b As Variant
  Dim i As Long, j As Long, k As Long, r As Long
  Dim Curr As Variant
  
  a = Range("A1", Range("B" & Rows.Count).End(xlUp)).Value
  ReDim b(1 To UBound(a), 1 To 2)
  For i = 1 To UBound(a)
    If a(i, 1) <> Curr Then
      Curr = a(i, 1)
      For j = 0 To 4
        k = k - (a(i + j, 2) = "EA" Or a(i + j, 2) = "MA")
        If a(i + k, 1) <> Curr Then Exit For
      Next j
      r = r + 1
      b(r, 1) = Curr
      b(r, 2) = IIf(k = 5, "X", "")
      i = i + j - 1
      k = 0
    End If
  Next i
  Range("E1:D" & r).Value = b
End Sub

My sample data and results:

Stephen_IV.xlsm
ABCDE
1111EA111
2111AB222
3111EA333X
4111EA444X
5111EA555
6111MA
7111AB
8222AB
9222EA
10222EA
11222EA
12222EA
13222EA
14333EA
15333EA
16333EA
17333EA
18333EA
19333EA
20444MA
21444MA
22444EA
23444EA
24444EA
25444EA
26555EA
27555AB
28555MA
29555MA
30555MA
31555AB
Sheet1
 
Upvote 0
Ahhhh yes!!! Thank you so much! Works perfect. Thanks again! I appreciate your help!!
 
Upvote 0
@Peter_SSs does the code design for office version 365 . I use 2019 and shows error subscript ou of range in this line
VBA Code:
If a(i + k, 1) <> Curr Then
 
Upvote 0
Ahhhh yes!!! Thank you so much! Works perfect. Thanks again! I appreciate your help!!
You're welcome. Thanks for the follow-up. :)


@Peter_SSs does the code design for office version 365 . I use 2019 and shows error subscript ou of range in this line
VBA Code:
If a(i + k, 1) <> Curr Then
The code should work fine in 2019. There must be something different about your data or layout. Test the code with the exact data in post 1 or 4. You can copy/paste it to a blank worksheet using this icon at the top-left of the mini-sheet.
1636324049257.png
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,751
Members
448,989
Latest member
mariah3

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