Select multiple columns and Set the format as Date

5.2k Views Asked by At

I am trying to select Column I,K, Q,R and format the entire columns from row 2 as Date (mm/dd/yyyy) I know this code will select all of the columns which I don't need. Can anyone help me with this with VBA code? Thank you! I included a part of my code to ask you if it is possible to include the date formatting within the first chunk of code. wsMain is a sheet for your information

With wsMain
       .Columns("A:AO").AutoFit
       .Cells.ClearFormats
       .Rows(1).Font.Bold = True
       .Cells.Font.Name = "Georgia"
       .Cells.Font.Color = RGB(0, 0, 225)
       .Cells.Resize(.Rows.Count - 1).Offset(1).Interior.Color = RGB(216, 228, 188)


 Sub SelectColumn()

    Dim xColIndex As Integer
    Dim xRowIndex As Integer
    xIndex = Application.ActiveCell.Column
    xRowIndex = Application.ActiveSheet.Cells(Rows.Count, xIndex).End(xlUp).Row
    Range(Cells(2, xIndex), Cells(xRowIndex, xIndex)).Select
1

There are 1 best solutions below

3
On BEST ANSWER

You try something simple like this

Option Explicit
Sub DateFormat()
    Dim rng As Range
    Dim rngArea As Range
    '// set your range
    Set rng = Range("I1:I10, K1:K10, Q1:Q10, R1:R10")

    For Each rngArea In rng.Areas
        With rngArea
            .NumberFormat = "MM/DD/YYYY"
        End With
    Next rngArea
End Sub

Example 2

Sub DateFormat()
    '// (9) = I 
    Columns(9).NumberFormat = "MM/DD/YYYY"
    Columns(11).NumberFormat = "MM/DD/YYYY"
    Columns(17).NumberFormat = "MM/DD/YYYY"
    Columns(18).NumberFormat = "MM/DD/YYYY"
End Sub