vba 更改工作簿所有工作表中列的日期格式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9106670/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 15:07:21  来源:igfitidea点击:

change the date format of a column in all sheets of a workbook

excelexcel-vbavba

提问by user582485

Im trying to change a custom date format to dd/mm/yyyy. The code below works for the current worksheet but wont work for all worksheets.

我试图将自定义日期格式更改为 dd/mm/yyyy。下面的代码适用于当前工作表,但不适用于所有工作表。

Sub dateformat()

    Dim ws As Worksheet

   For Each ws In Worksheets
        Columns("E:E").Select
        Selection.NumberFormat = "dd/mm/yyyy;@"
        'MsgBox ws.Name
    Next ws

End Sub

its hard to manually run the macro per column as the workbook has 100+ sheets. any thoughts on how i can get this to work?

由于工作簿有 100 多张工作表,因此很难每列手动运行宏。关于如何让它发挥作用的任何想法?

回答by Jesse

This works for me:

这对我有用:

Sub dateformat()

Dim ws As Worksheet
    For Each ws In ActiveWorkbook.Worksheets
        ws.Columns("E:E").NumberFormat = "dd/mm/yyyy;@"
    Next ws
End Sub

Avoid selecting, it's unreliable and unnecessary. Also "worksheets" should have a workbook.

避免选择,这是不可靠和不必要的。“工作表”也应该有一个工作簿。