Changing format to last working date

andysh

Board Regular
Joined
Nov 8, 2019
Messages
105
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

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.

GWteB

Well-known Member
Joined
Sep 10, 2010
Messages
1,502
Office Version
  1. 2013
Platform
  1. Windows
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
 

andysh

Board Regular
Joined
Nov 8, 2019
Messages
105
Sorry, should have specified, it's copying multiple columns and I just need to apply the formatting to column A as it pastes.
 

GWteB

Well-known Member
Joined
Sep 10, 2010
Messages
1,502
Office Version
  1. 2013
Platform
  1. Windows
I understand what your code is doing, but I'm not sure about what you're trying to achieve.
 

GWteB

Well-known Member
Joined
Sep 10, 2010
Messages
1,502
Office Version
  1. 2013
Platform
  1. Windows

ADVERTISEMENT

Perhaps ...
VBA Code:
wb2.Range("A" & lr2).PasteSpecial xlPasteFormats
 

andysh

Board Regular
Joined
Nov 8, 2019
Messages
105
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)
 

GWteB

Well-known Member
Joined
Sep 10, 2010
Messages
1,502
Office Version
  1. 2013
Platform
  1. Windows
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
 

Forum statistics

Threads
1,136,803
Messages
5,677,818
Members
419,722
Latest member
Rizzol

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
Top