选择多个列并将格式设置为日期

时间:2015-06-08 18:41:55

标签: excel vba excel-vba format

我想选择 列I,K,Q,R并将第2行的整列格式化为日期(mm / dd / yyyy) 我知道这段代码会选择我不需要的所有列。 任何人都可以用VBA代码帮我解决这个问题吗? 谢谢! 我包含了我的一部分代码,询问是否可以在第一个代码块中包含日期格式。 wsMain是一张供您参考的表格

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 个答案:

答案 0 :(得分:0)

你尝试像这样简单的事情

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