希望能够查看标题列并重新格式化VBA中这些列中的单元格

时间:2016-06-01 14:07:07

标签: excel vba excel-vba

我希望查看Excel工作表的标题并找到包含“DATE”或“DT”字样的标题,并将该列中的所有数据重新格式化为“mm / dd / yyyy hh:mm”任何帮助,将不胜感激。感谢。

现有代码(取消评论)

Sub Macro() 
    Dim iLastRow As Integer 
    Dim i As Integer 
    iLastRow = ActiveSheet.Range("a10000").End(xlUp).Row 
    For i = 3 To iLastRow 
        ActiveSheet.Range("A" & i).Select 
        Selection.NumberFormat = "m/d/yyyy h:mm" 
    Next i 
End Sub

1 个答案:

答案 0 :(得分:0)

这应该做你想要的:

Sub Macro()
  Dim x As Variant, y As Variant
  For Each y In ThisWorkbook.Worksheets
    With y
      For Each x In .Range(.Cells(1, 1), .Cells(1, .Columns.Count).End(xlToLeft)).Cells
        If InStr(1, Replace(x.Value, "DATE", "DT", , , vbTextCompare), "DT", vbTextCompare) Then
          Intersect(.UsedRange, .UsedRange.Offset(2), x.EntireColumn).NumberFormat = "m/d/yyyy h:mm"
        End If
      Next
    End With
  Next
End Sub

只需更改For Each行以满足您的需求;)