VBA code to paste based on date column

RW11700

New Member
Joined
Jul 7, 2022
Messages
8
Office Version
  1. 365
Platform
  1. Windows
I have a nightly report that is generated in excel and I am hoping to copy a cell range from sheet James and paste it to another sheet James2 based off the current date. In column A of James2 I have dates in mm/dd/yyyy format, I am hoping to take my copied cell range from James and paste them to James2 starting in column B based on TODAYS date in column A. I am able to copy the cells based on the below code but I am unable to figure out how to paste the data in sheet James2

Workbooks("Daily Availability TEST FILE.xlsm").Worksheets("James").Range("D8:X8").Copy
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Welcome to the Board!

Are both of these sheets in the same workbook?

I think it might be beneficial to show us a simple example.
Show us a small sample of the data in each sheet, and what you want the expected results to look like.

MrExcel has a tool called “XL2BB” that lets you post samples of your data that will allow us to copy/paste it to our Excel spreadsheets, so we can work with the same copy of data that you are. Instructions on using this tool can be found here: XL2BB Add-in

Note that there is also a "Test Here” forum on this board. This is a place where you can test using this tool (or any other posting techniques that you want to test) before trying to use those tools in your actual posts.
 
Upvote 0
Both sheets are in the same workbook.

I want to have the cell range saved on another sheet so I can reference previous days. I am having trouble automating it to paste based off the date in column A.
Screenshot 2022-07-07 165514 Example.png
Screenshot 2022-07-07 165755 Example 2.png
 
Upvote 0
Do you need to check/verify that the dates match, or can you just paste it to the next available row on your second sheet?
If you need to match it, is the value "System Date: 07-07-2022" all found in cell A3, or is it split up over A3 and B3?
If it is split up, is the value entered in B3 a valid date, or is it text?
If you are unsure, enter this formula in any blank cell and see what it returns:
=ISNUMBER(B3)

If it is a valid date, it will return TRUE.
 
Upvote 0
Thank you for taking the time to help, cell B3 came back as False. The report opens and runs automatically each night through task scheduler, I would need the cell range to be able to find "todays" date from column A on sheet James2, then paste the data in the same row as todays date starting in column B.
 
Upvote 0
OK, but you still haven't answered this question:
If you need to match it, is the value "System Date: 07-07-2022" all found in cell A3, or is it split up over A3 and B3?

We need to know if you have:
- "System Date: 07-07-2022" all found in cell A3
or
- "System Date: " in cell A3 and "07-07-2022" in cell B3

If you are unsure, what does this formula return:
=LEN(B3)
 
Upvote 0
=LEN(B3) - this returned 0

So this is a report that I am pulling from our property management system, the date shown in cell A3 is the system date in our property management system. The entire "System Date: 07-07-2022" is in cell A3.
 
Upvote 0
OK, your original question and your screen images don't quite match up (regarding which columns you are copying), so this code may need to be altered slightly.
But this should do what you need:
VBA Code:
Sub MyCopyMacro()

    Dim sh1 As Worksheet, sh2 As Worksheet
    Dim curDate As Date
    Dim rng As Range
    Dim r As Long
   
'   Set worksheet variables
    Set sh1 = Sheets("James")
    Set sh2 = Sheets("James2")
   
'   Get date out of Range A3 on first sheet
    curDate = Right(Trim(sh1.Range("A3")), 10)
   
'   Find date in column A on second sheet
    Set rng = sh2.Columns("A:A").Find(What:=curDate, After:=sh2.Range("A1"), LookIn:=xlFormulas2, _
        LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False)
   
'   Check to see if match found
    If rng Is Nothing Then
        MsgBox "Could not find date " & Format(curDate, "m/d/yyyy") & " on Sheet " & sh2.Name, vbOKOnly, "ERROR!"
        Exit Sub
    End If
   
'   Copy data over
    sh1.Range("C8:W8").Copy sh2.Cells(rng.Row, "B")

End Sub
 
Upvote 0
Solution
Daily Availability TEST FILE.xlsm
ABCDEFGHIJKLMNOPQRSTUVWX
1Carousel HotelPage: 1 of 2
2
3System Date: 07-07-2022Generated on: 07-07-2022 07:02 AM
4
5
6ThuFriSatSunMonTueWedThuFriSatSunMonTueWedThuFriSatSunMonTueWed
7789101112131415161718192021222324252627
8Available72538322211242149513631173118517357393642
9Pick Up11576212122-1-1-130111-1-2
10Total Rooms259259259259259259259259259259259259259259259259259259259259259
11Occ %979085889296919281808688938893807278858684
12OOO/DNS000000000000000000000
13Total Sold252234221227237248235238210208223228242228241208186202220223217
14Arrivals6576481165332305346679946411960444875662217
15Departures5794611104321435074698441273347777059481923
16Total People715671643657695721703681604614681692713679706590513556598609592
17Grps Not P/U000000000000000000000
18Restriction***********
James
Cells with Conditional Formatting
CellConditionCell FormatStop If True
C6:W18Expression=C$6="Sat"textNO
C6:W18Expression=C$6="Fri"textNO
 
Upvote 0
Daily Availability TEST FILE.xlsm
ABCDEFGHIJKLMNOPQRSTU
17/5/2022
27/6/2022
37/7/2022
47/8/2022
57/9/2022
67/10/2022
77/11/2022
87/12/2022
97/13/2022
107/14/2022
117/15/2022
127/16/2022
137/17/2022
147/18/2022
157/19/2022
167/20/2022
177/21/2022
187/22/2022
197/23/2022
207/24/2022
217/25/2022
227/26/2022
237/27/2022
247/28/2022
257/29/2022
267/30/2022
277/31/2022
288/1/2022
298/2/2022
308/3/2022
318/4/2022
328/5/2022
338/6/2022
348/7/2022
358/8/2022
368/9/2022
378/10/2022
388/11/2022
398/12/2022
408/13/2022
418/14/2022
428/15/2022
438/16/2022
448/17/2022
458/18/2022
468/19/2022
478/20/2022
488/21/2022
498/22/2022
508/23/2022
518/24/2022
528/25/2022
538/26/2022
548/27/2022
558/28/2022
568/29/2022
578/30/2022
588/31/2022
James2
 
Upvote 0

Forum statistics

Threads
1,215,507
Messages
6,125,212
Members
449,214
Latest member
mr_ordinaryboy

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