VBA macro to delete rows with first and third repeating variable

LambChoptheKid

New Member
Joined
Dec 10, 2022
Messages
35
Office Version
  1. 365
Platform
  1. Windows
Hello, I am trying to create a macro that will handle data that often includes 3x repeated timestamps. For those repeated timestamps, I need to keep the second iteration and discard the rows with the first and the third.

Example:
pic1.png



These columns will continue in this manner with three distinct values for each timestamp. Sometimes there is only one value for the timestamp, these values should be left alone

Is there a way to write a macro that will delete the first and third repeat?

Example of what I want the sheet to look like after running the macro:
pic2.png



Idea is to keep the second value and delete the rows with the first and third. I've written some VBA but I'm really scratching my head trying to make something work here. Also I have no way to use XL2BB so outside of a screenshot I can't give much more than what I've already provided.

If I have been unclear in my post I'd be happy to clarify.

If anyone is able to provide some insight as to how to tackle this, any guidance will be greatly appreciated!
 
Last edited:

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Maybe a formula option:
Book1
ABCDE
1Date TimeDate Time
207/01/2023 01:15a07/01/2023 01:15b
307/01/2023 01:15b07/01/2023 01:30e
407/01/2023 01:15c07/01/2023 01:45h
507/01/2023 01:30d07/01/2023 02:00j
607/01/2023 01:30e07/01/2023 02:15l
707/01/2023 01:30f
807/01/2023 01:45g
907/01/2023 01:45h
1007/01/2023 01:45i
1107/01/2023 02:00j
1207/01/2023 02:15k
1307/01/2023 02:15l
1407/01/2023 02:15m
Sheet1
Cell Formulas
RangeFormula
D2:E6D2=LET( rng,A2:B14, r,INDEX(rng,,1), s,SCAN(1,r,LAMBDA(a,b,IF(b<>OFFSET(b,-1,0),1,a+1))), k,IF(COUNTIF(r,r)=1,0,IF(s=2,0,"")), FILTER(rng,k=0))
Dynamic array formulas.
 
Upvote 0
Try this:
Asssuming you want the entire row deleted
VBA Code:
Sub Test_Me()
'Modified 9/4/2023 4:52 AM DST
Application.ScreenUpdating = False
Dim i As Long
Dim Lastrow As Long
Lastrow = Cells(Rows.Count, "B").End(xlUp).Row

For i = Lastrow To 1 Step -1
 If Cells(i, 2).Value = 1 Or Cells(i, 2).Value = 3 Then Rows(i).Delete
Next
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Maybe a formula option:
Book1
ABCDE
1Date TimeDate Time
207/01/2023 01:15a07/01/2023 01:15b
307/01/2023 01:15b07/01/2023 01:30e
407/01/2023 01:15c07/01/2023 01:45h
507/01/2023 01:30d07/01/2023 02:00j
607/01/2023 01:30e07/01/2023 02:15l
707/01/2023 01:30f
807/01/2023 01:45g
907/01/2023 01:45h
1007/01/2023 01:45i
1107/01/2023 02:00j
1207/01/2023 02:15k
1307/01/2023 02:15l
1407/01/2023 02:15m
Sheet1
Cell Formulas
RangeFormula
D2:E6D2=LET( rng,A2:B14, r,INDEX(rng,,1), s,SCAN(1,r,LAMBDA(a,b,IF(b<>OFFSET(b,-1,0),1,a+1))), k,IF(COUNTIF(r,r)=1,0,IF(s=2,0,"")), FILTER(rng,k=0))
Dynamic array formulas.
I couldn't get this to work. I got an error message saying that my function had too many arguments to function. Thank you though!
 
Upvote 0
Try this:
Asssuming you want the entire row deleted
VBA Code:
Sub Test_Me()
'Modified 9/4/2023 4:52 AM DST
Application.ScreenUpdating = False
Dim i As Long
Dim Lastrow As Long
Lastrow = Cells(Rows.Count, "B").End(xlUp).Row

For i = Lastrow To 1 Step -1
 If Cells(i, 2).Value = 1 Or Cells(i, 2).Value = 3 Then Rows(i).Delete
Next
Application.ScreenUpdating = True
End Sub
Thanks for the post! Nothing is happening when I run this macro, would I need to modify it to run on my active sheet? I'll keep trying to make this work and post an update if something happens.

Appreciate the help!
 
Upvote 0
Thanks for the post! Nothing is happening when I run this macro, would I need to modify it to run on my active sheet? I'll keep trying to make this work and post an update if something happens.

Appreciate the help!
Sure, this macro assumes you want to run it on the active sheet. No where in your original post did you mention the specific sheet name.
 
Upvote 0
I couldn't get this to work. I got an error message saying that my function had too many arguments to function. Thank you though!
Maybe if we could see a more representative example of your data?
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,240
Members
452,621
Latest member
Laura_PinksBTHFT

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