vba to format several columns with the date format

dpaton05

Well-known Member
Joined
Aug 14, 2018
Messages
2,375
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
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
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
 
Upvote 0
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"
 
Upvote 0
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

:banghead:
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,909
Messages
6,175,313
Members
452,634
Latest member
cpostell

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