I cannot seems to access a VBA defined range ("DateRange") within Application.Intersect.
Using Application.Intersect(DateRange,Cellref) gives the error "Object required"
The range is able to be copied to another sheet and the address can also be retrieved via MsgBox DateRange.Address
.
Below is the code.
This one has really got me scratching my head.
Sub FindCells2()
Dim LR As Long, i As Long
Dim Txt As String
Dim j, k As Integer
Dim StartDate, FinishDate As String
Dim Sh As Worksheet: Set Sh = Sheets("Full chart and primary cals")
Dim Cellref, DateRange As Range
'Find Date Range
'Search location and values
LookupColumn = "B"
'Enter date as YYYY.MM.DD HH:00
StartDate = "2013.01.02 20:00"
FinishDate = "2013.01.09 20:00"
With Sh
'Search for cells containing date strings
For j = 1 To 30000
If Sh.Range(LookupColumn & j).Value = FinishDate Then FinishDateRow = j
Next j
For k = FinishDateRow To 1 Step -1
If Sh.Range(LookupColumn & k).Value = StartDate Then StartDateRow = k
Next k
'Set DateRange as area between rows with Date strings
Set DateRange = Sh.Range("A" & StartDateRow & ":" & "Q" & FinishDateRow)
MsgBox DateRange.Address
'Find Cells
'Loop through sheet looking for cells
LR = .Range("B" & Rows.Count).End(xlUp).Row
For i = 1 To LR
'Find cells in "M" and store thier reference in Cellref
If .Range("M" & i).Value <= 25 Then Set Cellref = .Range("M" & i)
'Find if Cell ref is contained within DateRange and store result as bool
If Application.Intersect(DateRange, Cellref) Is Nothing Then MsgBox Intersect is Negative
'Output cell ranges to the appropriate sheets
If iSect And Cellref.Cells(i, "M").Offset(0, -5) <= Cellref.Cells(i, "M").Offset(-10, -5) Then Cellref.Offset(-3, 0).Resize(10, 1).EntireRow.Copy Destination:=Sheets("DownT").Range("A" & Rows.Count).End(xlUp).Offset(2)
If iSect And Cellref.Cells(i, "M").Offset(0, -5) > Cellref.Cells(i, "M").Offset(-10, -5) Then Cellref.Offset(-3, 0).Resize(10, 1).EntireRow.Copy Destination:=Sheets("UpT").Range("A" & Rows.Count).End(xlUp).Offset(2)
'End With
Next i
End With
End Sub