I am trying to create a report in Word that drags data from an Excel spreadsheet. Being on a works PC I am limited as to what I can do (cant open word from excel macro's) so my work-around is to copy all the Information I need from one excel worksheet into another so that is properly formatted / arranged as a data source for a word mail merge.
The problem I have is that I want to copy the records that run between 07:00 on one day to 07:00 the next. It went a bit wrong when I added a nested IF for the times.
Any help is much appreciated, Rgds Iain
Sub CopyFromLog()
Dim LastRow As Long
Dim i As Long, j As Long, ns As Date, nf As Date, o As Date, f As String, s As String, t As Date
With Worksheets("Log")
LastRow = .Cells(.Rows.Count, "B").End(xlUp).Row
End With
With Worksheets("Data")
Worksheets("Data").Rows("3:" & LastRow).Clear
j = .Cells(.Rows.Count, "B").End(xlUp).Row + 2
End With
With Worksheets("Navigation")
ns = Worksheets("Navigation").Cells(3, "C").Value ' the report start date
nf = Worksheets("Navigation").Cells(4, "C").Value ' the report end date
End With
For i = 2 To LastRow
With Worksheets("Log")
o = Worksheets("Log").Cells(i, "B").Value 'start date
t = Worksheets("Log").Cells(i, "V").Value 'end date
s = Worksheets("Log").Cells(i, "R").Value 'start time
f = Worksheets("Log").Cells(i, "W").Value 'finish time
If o <= ns And s >= "07:00" Then
If t >= nf And f <= "07:00" Or t >= nf And f <= "R" Then
.Rows(i).Copy Destination:=Worksheets("Data").Range("A" & j)
j = j + 1
End If
End If
End With
Next i
End Sub`