Changing format to last working date

andysh

Board Regular
Joined
Nov 8, 2019
Messages
111
Hi,

I have the below code copying data from one workbook to two others and it works fine.

However, when pasting to wb2 (penultimate line of the if statement) I would like to reformat col A to 'workday(date(), -1)' with format dd/mm/yyyy. How could I add that into the code?

VBA Code:
lrw = ws.Range("A" & Rows.Count).End(xlUp).Row
lrw2 = ws.Range("B" & Rows.Count).End(xlUp).Row

If lrw > 2 Then

lc = ws.Cells(1, Columns.Count).End(xlToLeft).Column
lr = wb.Range("A" & Rows.Count).End(xlUp).Row + 1
lr2 = wb2.Range("A" & Rows.Count).End(xlUp).Row + 1

ws.Rows("3:" & lrw).Copy
wb.Range("A" & lr).PasteSpecial xlPasteAll
wb2.Range("A" & lr2).PasteSpecial xlPasteAll
ws.Range(ws.Cells(3, 1), ws.Cells(lrw, lc)).EntireRow.ClearContents


End If
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Not sure but do you mean something like this:
VBA Code:
With wb2.Range("A" & lr2)
    .PasteSpecial xlPasteAll
    .Formula = "=Workday(Date(),-1)"
    .NumberFormat = "dd/mm/yyyy"
End With
 
Upvote 0
Sorry, should have specified, it's copying multiple columns and I just need to apply the formatting to column A as it pastes.
 
Upvote 0
I understand what your code is doing, but I'm not sure about what you're trying to achieve.
 
Upvote 0
Perhaps ...
VBA Code:
wb2.Range("A" & lr2).PasteSpecial xlPasteFormats
 
Upvote 0
Perhaps ...
VBA Code:
wb2.Range("A" & lr2).PasteSpecial xlPasteFormats
Just got timed out replying, got collared by colleague

The data in column A is a timestamp ( using a Now() in the code) which is required for wb1 and stays as it is. We process the sheets the day after they're filled in so for for wb2 I need the timestamp removed and replaced with just a date for the previous working day, simply formatting the cells in the sheet leaves the time in the background which is messing up the pivot tables I'm trying to create. Hence the need to replace the timestamp with just a date -1.

Looking at my original post I really didn't make it clear what I was after, apologies. Perhaps the algorithm should be:

Paste data to next row wb2
If col A is timestamp (dd/mm/yyyy hh:mm)
replace contents with workday-1 (dd/mm/yyyy)
 
Upvote 0
If by workday you mean the day your code is launched, then this piece of code is probably what you are looking for.
VBA Code:
Set rng = wb2.Range("A" & lr2)
rng.PasteSpecial xlPasteAll
For Each c In rng
    If c.NumberFormat = "dd/mm/yyyy hh:mm" Then
        c.Value = (Date - 1)
        c.NumberFormat = "dd/mm/yyyy"
    End If
Next c
 
Upvote 0

Forum statistics

Threads
1,213,546
Messages
6,114,251
Members
448,556
Latest member
peterhess2002

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