select rows that match current active cell vba issue

PTamiggi

New Member
Joined
May 2, 2021
Messages
9
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
Ok need some help because the error isn't making sense to me. I have 4 dates in column A with around 12 copies of each(12-Jan-24, 19-Jan-24, 26-Jan-24, 02-Feb-24). I want to select a cell in column A and run a macro that will select all rows that have a match. The code I have is working for the dates 12-Jan-24 and 19-Jan-24, but gives an error (see pic) when running the macro when I have either date 26-Jan-24 and 02-Feb-24 active. I plan to cut and paste selected rows as to another sheet within the workbook, eventually after figuring out this issue. Any help would be appreciated.

VBA Code:
Option Explicit

Sub Date_Match_Rows_Select()
Dim tableR As Range, cell As Range, d As Range
Dim s As String
Set tableR = Range("A:A")
Set d = Selection
For Each cell In tableR
  If cell = d Then
  s = s & cell.Row & ":" & cell.Row & ", "
  End If
Next cell
s = Left(s, Len(s) - 2)
Range(s).Select
End Sub

Error.PNG
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Strange.. Here, find a safer version of your code. Maybe it helps:
VBA Code:
Option Explicit
Sub Date_Match_Rows_Select()
  Dim tableR As Range, cell As Range, d As Range
  Dim s As Range, lRow As Long
 
  lRow = Cells(Rows.Count, "A").End(xlUp).Row
  Set tableR = Range("A1:A" & lRow)
  Set d = Selection
  Set s = d
  For Each cell In tableR
    If cell.Value2 = d.Value2 Then
      Set s = Union(s, cell)
    End If
  Next cell
  s.EntireRow.Select
End Sub
 
Upvote 0
Solution
I am not getting an error with data as you described.
However, I am wondering why you have the code check 1,048,576 cells when you only have about 50 cells with data? :eek:

Just wondering if you get different result if you make this change in the code
Rich (BB code):
Set tableR = Range("A:A")
Set tableR = Range("A1:A100")
 
Upvote 0
Strange.. Here, find a safer version of your code. Maybe it helps:
VBA Code:
Option Explicit
Sub Date_Match_Rows_Select()
  Dim tableR As Range, cell As Range, d As Range
  Dim s As Range, lRow As Long
 
  lRow = Cells(Rows.Count, "A").End(xlUp).Row
  Set tableR = Range("A1:A" & lRow)
  Set d = Selection
  Set s = d
  For Each cell In tableR
    If cell.Value2 = d.Value2 Then
      Set s = Union(s, cell)
    End If
  Next cell
  s.EntireRow.Select
End Sub
This is working perfectly. Thank you.
 
Upvote 0
I am not getting an error with data as you described.
However, I am wondering why you have the code check 1,048,576 cells when you only have about 50 cells with data? :eek:

Just wondering if you get different result if you make this change in the code
Rich (BB code):
Set tableR = Range("A:A")
Set tableR = Range("A1:A100")
I have tried that, and it didn't make a difference. Thank you for the idea though.
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,200
Members
453,022
Latest member
RobertV1609

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