vba to format several columns with the date format

dpaton05

Well-known Member
Joined
Aug 14, 2018
Messages
2,042
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I have some code, trying to format several columns as dates, what is wrong with it as it won't work?

Code:
    Set sht = Worksheets("Home")

    With sht

        Set tbl = .ListObjects("tblCosting")
        
        For Each tblrow In tbl.ListRows
            Combo = Format(tblrow.Range.Cells(1, 1), "mmmm yyyy")
            Set wsDst = Sheets(Combo)
            
            With wsDst
                'This copies the first 10 columns, i.e. A:J, of the current row of the table to column A in the destination sheet.
                tblrow.Range.Resize(, 10).copy
                With .Range("A" & Rows.Count).End(xlUp).Offset(1)
                    .PasteSpecial xlPasteValuesAndNumberFormats
[COLOR=#ff0000]                    .Columns("A", "AD", "AE").NumberFormat = "dd/mm/yyyy"[/COLOR]
                'This should go to the 15th column in the current row, i.e. column O, and copy that column and the next 2 columns, i.e. O:Q, to column K on the destination sheet.
                tblrow.Range.Offset(, 14).Resize(, 3).copy
                .Range("K" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteValuesAndNumberFormats
                'Similarly this should copy columns AD:AF from the table to column N on the destination sheet.
                tblrow.Range.Offset(, 29).Resize(, 3).copy
                .Range("N" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteValuesAndNumberFormats
            End With
            
        Next tblrow
 

Some videos you may like

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.

MARK858

MrExcel MVP
Joined
Nov 12, 2010
Messages
13,886
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
  2. Mobile
Maybe....

Code:
    With wsDst
        'This copies the first 10 columns, i.e. A:J, of the current row of the table to column A in the destination sheet.
        tblrow.Range.Resize(, 10).Copy
        .Range("A" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteValuesAndNumberFormats

        .Range("A:A", "AD:AD", "AE:AE").NumberFormat = "dd/mm/yyyy"
        'This should go to the 15th column in the current row, i.e. column O, and copy that column and the next 2 columns, i.e. O:Q, to column K on the destination sheet.
        tblrow.Range.Offset(, 14).Resize(, 3).Copy
        .Range("K" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteValuesAndNumberFormats
        'Similarly this should copy columns AD:AF from the table to column N on the destination sheet.
        tblrow.Range.Offset(, 29).Resize(, 3).Copy
        .Range("N" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteValuesAndNumberFormats
    End With
 

Michael M

Well-known Member
Joined
Oct 27, 2005
Messages
19,767
Office Version
  1. 2016
  2. 2013
  3. 2007
Platform
  1. Windows
firstly your columns line is in the wrong place as you're with statement is for column "A"
So, move move it up above that line and use
Code:
Range("A1,AD1,AE1").EntireColumn.NumberFormat = "dd/mm/yyyy"
 

MARK858

MrExcel MVP
Joined
Nov 12, 2010
Messages
13,886
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
  2. Mobile
Going mad and left in the quotes in my last post so again maybe...

Code:
With wsDst
        'This copies the first 10 columns, i.e. A:J, of the current row of the table to column A in the destination sheet.
        tblrow.Range.Resize(, 10).Copy
        .Range("A" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteValuesAndNumberFormats

        .Range("A:A, AD:AD, AE:AE").NumberFormat = "dd/mm/yyyy"
        'This should go to the 15th column in the current row, i.e. column O, and copy that column and the next 2 columns, i.e. O:Q, to column K on the destination sheet.
        tblrow.Range.Offset(, 14).Resize(, 3).Copy
        .Range("K" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteValuesAndNumberFormats
        'Similarly this should copy columns AD:AF from the table to column N on the destination sheet.
        tblrow.Range.Offset(, 29).Resize(, 3).Copy
        .Range("N" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteValuesAndNumberFormats
End With

:oops:
 
Last edited:

Watch MrExcel Video

Forum statistics

Threads
1,123,492
Messages
5,601,994
Members
414,490
Latest member
Rip181

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