VBA copy and paste rows based on data (Date)

Rogue909

New Member
Joined
Oct 23, 2017
Messages
11
I need to copy calibration data from a master calibration file into a report file (to display the daily calibration in the report).

My goal is to put a button on the report excel sheet that will search the daily calibration for all the data that has been entered that day. (The date will be contained in a cell for referencing purposes). Then copy the row and insert it into the report file.

I can "think out" the code in my head. I was thinking of using a for loop to begin searching the dates (In my case the first date is located in cell A26.) Then have it add 1 to each as it searches the rows (A27, A28, A29, etc.) If it finds the day matches the current day then it copies the row (I can also just use offset/range to copy just columns A-P) and paste it into the report file. Then continue to find the rest of the dates that match.

The problem is twofold; I don't know how to stop the For loop (once it finishes sorting dates I don't want it to just continue searching empty cells) and I'm not very good at starting from scratch in VBA (I've only used VBA for 2 weeks maybe? And I'm rather new at coding to begin with...)

Does anyone have an example of a code that runs a similiar task that could help me?
Or know if an easy solution to this?
Any advice in general?
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
You can try:

Code:
Sub VCAPRBODD()Dim lRow As Long
Dim iCntr As Long


Application.ScreenUpdating = False


lRow = Sheets("Master").Cells.Find(What:="*", SearchOrder:=xlRows, SearchDirection:=xlPrevious, LookIn:=xlValues).Row
    
  For iCntr = 26 To lRow Step 1
    If Cells(iCntr, "A") = Sheets("Master").Range("A23") Then
     Rows(iCntr).Copy Sheets("Other").Rows(Sheets("Other").Cells.Find(What:="*", SearchOrder:=xlRows, SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1)
    End If
  Next
  
Application.ScreenUpdating = True


End Sub


Excel 2012
ABCDEFGHIJKLMNOP
22Reference Date
231-Jan-17
24
25DateOther DataOther DataOther DataOther DataOther DataOther DataOther DataOther DataOther DataOther DataOther DataOther DataOther DataOther DataOther Data
261/1/2017Data Col B 1Data Col C 1Data Col D 1Data Col B 1Data Col C 1Data Col D 1Data Col B 1Data Col C 1Data Col D 1Data Col B 1Data Col C 1Data Col D 1Data Col B 1Data Col C 1Data Col D 1
271/2/2017Data Col B 2Data Col C 2Data Col D 2Data Col B 2Data Col C 2Data Col D 2Data Col B 2Data Col C 2Data Col D 2Data Col B 2Data Col C 2Data Col D 2Data Col B 2Data Col C 2Data Col D 2
281/1/2017Data Col B 3Data Col C 3Data Col D 3Data Col B 3Data Col C 3Data Col D 3Data Col B 3Data Col C 3Data Col D 3Data Col B 3Data Col C 3Data Col D 3Data Col B 3Data Col C 3Data Col D 3
291/2/2017Data Col B 4Data Col C 4Data Col D 4Data Col B 4Data Col C 4Data Col D 4Data Col B 4Data Col C 4Data Col D 4Data Col B 4Data Col C 4Data Col D 4Data Col B 4Data Col C 4Data Col D 4
301/1/2017Data Col B 5Data Col C 5Data Col D 5Data Col B 5Data Col C 5Data Col D 5Data Col B 5Data Col C 5Data Col D 5Data Col B 5Data Col C 5Data Col D 5Data Col B 5Data Col C 5Data Col D 5
311/2/2017Data Col B 6Data Col C 6Data Col D 6Data Col B 6Data Col C 6Data Col D 6Data Col B 6Data Col C 6Data Col D 6Data Col B 6Data Col C 6Data Col D 6Data Col B 6Data Col C 6Data Col D 6
321/3/2017Data Col B 7Data Col C 7Data Col D 7Data Col B 7Data Col C 7Data Col D 7Data Col B 7Data Col C 7Data Col D 7Data Col B 7Data Col C 7Data Col D 7Data Col B 7Data Col C 7Data Col D 7
331/1/2017Data Col B 8Data Col C 8Data Col D 8Data Col B 8Data Col C 8Data Col D 8Data Col B 8Data Col C 8Data Col D 8Data Col B 8Data Col C 8Data Col D 8Data Col B 8Data Col C 8Data Col D 8
341/1/2017Data Col B 9Data Col C 9Data Col D 9Data Col B 9Data Col C 9Data Col D 9Data Col B 9Data Col C 9Data Col D 9Data Col B 9Data Col C 9Data Col D 9Data Col B 9Data Col C 9Data Col D 9
351/3/2017Data Col B 10Data Col C 10Data Col D 10Data Col B 10Data Col C 10Data Col D 10Data Col B 10Data Col C 10Data Col D 10Data Col B 10Data Col C 10Data Col D 10Data Col B 10Data Col C 10Data Col D 10
361/1/2017Data Col B 11Data Col C 11Data Col D 11Data Col B 11Data Col C 11Data Col D 11Data Col B 11Data Col C 11Data Col D 11Data Col B 11Data Col C 11Data Col D 11Data Col B 11Data Col C 11Data Col D 11
371/3/2017Data Col B 12Data Col C 12Data Col D 12Data Col B 12Data Col C 12Data Col D 12Data Col B 12Data Col C 12Data Col D 12Data Col B 12Data Col C 12Data Col D 12Data Col B 12Data Col C 12Data Col D 12
381/2/2017Data Col B 13Data Col C 13Data Col D 13Data Col B 13Data Col C 13Data Col D 13Data Col B 13Data Col C 13Data Col D 13Data Col B 13Data Col C 13Data Col D 13Data Col B 13Data Col C 13Data Col D 13
391/1/2017Data Col B 14Data Col C 14Data Col D 14Data Col B 14Data Col C 14Data Col D 14Data Col B 14Data Col C 14Data Col D 14Data Col B 14Data Col C 14Data Col D 14Data Col B 14Data Col C 14Data Col D 14
401/3/2017Data Col B 15Data Col C 15Data Col D 15Data Col B 15Data Col C 15Data Col D 15Data Col B 15Data Col C 15Data Col D 15Data Col B 15Data Col C 15Data Col D 15Data Col B 15Data Col C 15Data Col D 15
411/1/2017Data Col B 16Data Col C 16Data Col D 16Data Col B 16Data Col C 16Data Col D 16Data Col B 16Data Col C 16Data Col D 16Data Col B 16Data Col C 16Data Col D 16Data Col B 16Data Col C 16Data Col D 16
421/3/2017Data Col B 17Data Col C 17Data Col D 17Data Col B 17Data Col C 17Data Col D 17Data Col B 17Data Col C 17Data Col D 17Data Col B 17Data Col C 17Data Col D 17Data Col B 17Data Col C 17Data Col D 17
431/1/2017Data Col B 18Data Col C 18Data Col D 18Data Col B 18Data Col C 18Data Col D 18Data Col B 18Data Col C 18Data Col D 18Data Col B 18Data Col C 18Data Col D 18Data Col B 18Data Col C 18Data Col D 18
Master


Will result in:


Excel 2012
ABCDEFGHIJKLMNOP
1DateOther DataOther DataOther DataOther DataOther DataOther DataOther DataOther DataOther DataOther DataOther DataOther DataOther DataOther DataOther Data
21/1/2017Data Col B 1Data Col C 1Data Col D 1Data Col B 1Data Col C 1Data Col D 1Data Col B 1Data Col C 1Data Col D 1Data Col B 1Data Col C 1Data Col D 1Data Col B 1Data Col C 1Data Col D 1
31/1/2017Data Col B 3Data Col C 3Data Col D 3Data Col B 3Data Col C 3Data Col D 3Data Col B 3Data Col C 3Data Col D 3Data Col B 3Data Col C 3Data Col D 3Data Col B 3Data Col C 3Data Col D 3
41/1/2017Data Col B 5Data Col C 5Data Col D 5Data Col B 5Data Col C 5Data Col D 5Data Col B 5Data Col C 5Data Col D 5Data Col B 5Data Col C 5Data Col D 5Data Col B 5Data Col C 5Data Col D 5
51/1/2017Data Col B 8Data Col C 8Data Col D 8Data Col B 8Data Col C 8Data Col D 8Data Col B 8Data Col C 8Data Col D 8Data Col B 8Data Col C 8Data Col D 8Data Col B 8Data Col C 8Data Col D 8
61/1/2017Data Col B 9Data Col C 9Data Col D 9Data Col B 9Data Col C 9Data Col D 9Data Col B 9Data Col C 9Data Col D 9Data Col B 9Data Col C 9Data Col D 9Data Col B 9Data Col C 9Data Col D 9
71/1/2017Data Col B 11Data Col C 11Data Col D 11Data Col B 11Data Col C 11Data Col D 11Data Col B 11Data Col C 11Data Col D 11Data Col B 11Data Col C 11Data Col D 11Data Col B 11Data Col C 11Data Col D 11
81/1/2017Data Col B 14Data Col C 14Data Col D 14Data Col B 14Data Col C 14Data Col D 14Data Col B 14Data Col C 14Data Col D 14Data Col B 14Data Col C 14Data Col D 14Data Col B 14Data Col C 14Data Col D 14
91/1/2017Data Col B 16Data Col C 16Data Col D 16Data Col B 16Data Col C 16Data Col D 16Data Col B 16Data Col C 16Data Col D 16Data Col B 16Data Col C 16Data Col D 16Data Col B 16Data Col C 16Data Col D 16
101/1/2017Data Col B 18Data Col C 18Data Col D 18Data Col B 18Data Col C 18Data Col D 18Data Col B 18Data Col C 18Data Col D 18Data Col B 18Data Col C 18Data Col D 18Data Col B 18Data Col C 18Data Col D 18
Other
 
Upvote 0
An alternative option is
Code:
Sub CopyOnDate()

    With Sheets("[COLOR=#ff0000]Master[/COLOR]")
        With .Range("A26", .Range("A" & Rows.Count).End(xlUp))
            .Replace Sheets("[COLOR=#ff0000]Master[/COLOR]").Range("A23").Value, True
            On Error GoTo Xit
            .SpecialCells(xlConstants, xlLogical).EntireRow.Copy Sheets("[COLOR=#ff0000]List[/COLOR]").Range("A" & Rows.Count).End(xlUp).Offset(1)
            On Error GoTo 0
            .Replace True, Sheets("[COLOR=#ff0000]Master[/COLOR]").Range("A23").Value
        End With
        Sheets("[COLOR=#ff0000]List[/COLOR]").Columns(1).Replace True, .Range("A23").Value
    End With
Exit Sub

Xit:
    MsgBox "No dates found"
    
End Sub
Changing sheet names to suit
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,186
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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