Selecting cells greater than date in a1

mrtim2232

New Member
Joined
Aug 24, 2017
Messages
48
Hi all,

I have got a macro that should select any cells within column C with a date value greater than the value I have entered into A1 I have ran the program and it doesn't cause any faults however It isn't selecting anything I'm sure my mistake is obvious any help would be appreciated the code is below:

Sub SelectByCellValue()
Dim lastrow As Long
Dim xRg As Range, yRg As Range
With ThisWorkbook.Worksheets("Sheet2")
lastrow = .Cells(.Rows.Count, "C").End(xlUp).Row
Application.ScreenUpdating = False
For Each xRg In .Range("C1:C" & lastrow)
'a1 contains a date value
If Range("a1").FormulaR1C1 = "=IF(RC[-3]<RC[-1],1,0)" Then
If yRg Is Nothing Then
Set yRg = .Range("A" & xRg.Row).Resize(, 2)
Else
Set yRg = Union(yRg, .Range("A" & xRg.Row).Resize(, 2))
End If
End If
Next xRg
Application.ScreenUpdating = True
End With

If Not yRg Is Nothing Then yRg.Select
End Sub
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Not sure what this line is intended to do:

Code:
If Range("a1").FormulaR1C1 = "=IF(RC[-3]<RC[-1],1,0)" Then

Suspect it should look more like this:

Code:
If xRg.Value > Range("a1").Value Then

WBD
 
Upvote 0
Thankyou It works like a charm If possible I'd like to be able to copy and paste the value in A1 to the newly selected cells the way I'm doing it now you have to re-run the macro for each cell which can be time consuming is there any way to get it to loop through and do all of the cells in the selection in one go? The code is below:

Sub SelectByCellValue()
Sheets("sheet2").Range("A1").Copy
Dim lastrow As Long
Dim xRg As Range, yRg As Range
With ThisWorkbook.Worksheets("Sheet2")
lastrow = .Cells(.Rows.Count, "C").End(xlUp).Row
Application.ScreenUpdating = False
For Each xRg In .Range("C1:C" & lastrow)
'a1 contains a date value
If xRg.Value > Range("a1").Value Then
If yRg Is Nothing Then
Set yRg = .Range("C" & xRg.Row)
Else
Set yRg = Union(yRg, .Range("c" & xRg.Row))
End If
End If
Next xRg
Application.ScreenUpdating = True
End With

If Not yRg Is Nothing Then yRg.Select
ActiveCell.PasteSpecial xlValues
End Sub
 
Upvote 0
Please use code tags next time. I suspect you want this:

Code:
Sub SelectByCellValue()


Dim lastRow As Long
Dim xRg As Range
Dim yRg As Range


Application.ScreenUpdating = False


With ThisWorkbook.Worksheets("Sheet2")
    lastRow = .Cells(.Rows.Count, "C").End(xlUp).Row
    For Each xRg In .Range("C1:C" & lastRow)
        'a1 contains a date value
        If xRg.Value > Range("A1").Value Then
            If yRg Is Nothing Then
                Set yRg = .Range("C" & xRg.Row)
            Else
                Set yRg = Union(yRg, .Range("C" & xRg.Row))
            End If
        End If
    Next xRg
    If Not yRg Is Nothing Then yRg.Value = .Range("A1").Value
End With


Application.ScreenUpdating = True


End Sub

WBD
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,323
Members
452,635
Latest member
laura12345

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