Select 多列并将格式设置为日期

Select multiple columns and Set the format as Date

我正在尝试 select I、K、Q、R 列并将第 2 行的整个列格式化为日期 (mm/dd/yyyy) 我知道此代码将 select 所有我不需要的列。 谁能用 VBA 代码帮我解决这个问题? 谢谢! 我包含了一部分代码来询问您是否可以在第一段代码中包含日期格式。 wsMain 是一个 sheet 供您参考

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

你试试像这样简单的东西

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

示例 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