Insert last day of previous month, copy and paste to next empty row

maratonomak1

New Member
Joined
Jun 13, 2016
Messages
7
Hi,

I have a code to copy and paste to next empty row from workbook X to Y column B(+3). I'm trying to insert the last day of previous month format (MMDDYY) but in column A . So if the next empty row pasted in cell B20 to insert the last day of previous month only in A20. My code so far.


Code:
Sub Copy()

ThisWorkbook.RefreshAll
Application.CalculateUntilAsyncQueriesDone
Application.CalculateFullRebuild
Application.CalculateUntilAsyncQueriesDone

Dim SourceBk As Workbook, DestBk As Workbook
Dim Sh As Worksheet
Dim pasterow As Long
Dim lo As Excel.ListObject

Set SourceBk = ThisWorkbook 'X"
Set DestBk = Workbooks("Y.xlsm")

Application.ScreenUpdating = False

For Each Sh In ThisWorkbook.Worksheets
pasterow = DestBk.Sheets(Sh.Name).Cells(Rows.Count, "B").End(xlUp).Row + 3
With SourceBk.Sheets(Sh.Name)
If .Range("A2") <> "" Then
Set lo = .ListObjects(1)
With lo
.Range.Copy
DestBk.Sheets(Sh.Name).Range("B" & pasterow).PasteSpecial xlPasteFormats
DestBk.Sheets(Sh.Name).Range("B" & pasterow).PasteSpecial xlPasteValuesAndNumberFormats
End With
Else
DestBk.Sheets(Sh.Name).Range("B" & pasterow).Value = "N/A"
End If
End With
Next Sh

Application.CutCopyMode = False

Set SourceBk = Nothing
Set DestBk = Nothing

Application.ScreenUpdating = True

End Sub
Code:


Any help will be greatly appreciated.

Thanks!
 
If all you want to do is add the date then modify your Else statement:
Code:
       Else
           DestBk.Sheets(Sh.Name).Range("B" & pasterow).Value = "N/A"
           [COLOR="#0000CD"]DestBk.Sheets(Sh.Name).Range("A" & pasterow) = Format(Date - Day(Date), "mmddyy")  [/COLOR]
       End If
And delete the If...End If Statement for the UsedRange.

That worked perfectly !!!

Thank you so much
 
Upvote 0

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.

Forum statistics

Threads
1,223,911
Messages
6,175,327
Members
452,635
Latest member
laura12345

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