Vba 查找列并将其格式设置为日期
我的excel工作表中有几个列,它们都被命名为“日期”。我想编写一个代码,在所有工作表中查找所有日期列,并将该列中的值格式化为“dd/mm/yyyy;@” 在下面的代码中,我尝试编写列(“日期”)。选择而不是列(“E:E”)。选择,但这不起作用。有谁能建议我如何解决这个问题吗 谢谢你的时间和建议Vba 查找列并将其格式设置为日期,vba,excel,excel-2007,Vba,Excel,Excel 2007,我的excel工作表中有几个列,它们都被命名为“日期”。我想编写一个代码,在所有工作表中查找所有日期列,并将该列中的值格式化为“dd/mm/yyyy;@” 在下面的代码中,我尝试编写列(“日期”)。选择而不是列(“E:E”)。选择,但这不起作用。有谁能建议我如何解决这个问题吗 谢谢你的时间和建议 Sub dateformat() Dim ws As Worksheet For Each ws In Worksheets Columns("E:E").Sel
Sub dateformat()
Dim ws As Worksheet
For Each ws In Worksheets
Columns("E:E").Select
Selection.NumberFormat = "dd/mm/yyyy;@"
Next ws
End Sub
您能否合并一些代码(修改以适应您的需求)
努布尔,这就是你想要的吗
Option Explicit
Sub Sample()
Dim aCell As Range, bCell As Range
Dim ws As Worksheet
Dim ExitLoop As Boolean
For Each ws In ThisWorkbook.Sheets
Set aCell = ws.Rows(1).Find(what:="Date", LookIn:=xlValues, _
lookat:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
ExitLoop = False
If Not aCell Is Nothing Then
Set bCell = aCell
ws.Columns(aCell.Column).NumberFormat = "dd/mm/yyyy;@"
Do While ExitLoop = False
Set aCell = ws.Rows(1).FindNext(After:=aCell)
If Not aCell Is Nothing Then
If aCell.Address = bCell.Address Then Exit Do
ws.Columns(aCell.Column).NumberFormat = "dd/mm/yyyy;@"
Else
ExitLoop = True
End If
Loop
End If
Next
End Sub
跟进
出现这些错误的原因是因为列的格式为文本。试试这个。这是有效的:)
嗯
Sid所有工作表日期中是否都有“E”列?或者“日期”可以在任何列中?如果在所有表格中,列E是日期,那么您在
列(“E:E”)
:)@Sid:谢谢您的回复。日期可以在任何列中。另外,我在一张工作表中有几个日期栏。最后一个问题。所有工作表中第1行的标题是否都是“日期”?@Sid:是的,例如:一张工作表中名为“日期”的列在(E:E)、(F:F)等范围内,而另一张工作表中名为(Az:Az)、(B:B)等范围内。您能够测试以下代码吗?@Andrew:我尝试了您的代码,它给我的编译错误是“参数数目错误或属性分配无效”。
Option Explicit
Sub Sample()
Dim aCell As Range, bCell As Range
Dim ws As Worksheet
Dim ExitLoop As Boolean
For Each ws In ThisWorkbook.Sheets
Set aCell = ws.Rows(1).Find(what:="Date", LookIn:=xlValues, _
lookat:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
ExitLoop = False
If Not aCell Is Nothing Then
Set bCell = aCell
ws.Columns(aCell.Column).NumberFormat = "dd/mm/yyyy;@"
Do While ExitLoop = False
Set aCell = ws.Rows(1).FindNext(After:=aCell)
If Not aCell Is Nothing Then
If aCell.Address = bCell.Address Then Exit Do
ws.Columns(aCell.Column).NumberFormat = "dd/mm/yyyy;@"
Else
ExitLoop = True
End If
Loop
End If
Next
End Sub
Option Explicit
Sub Sample()
Dim aCell As Range, bCell As Range
Dim ws As Worksheet
Dim lastRow As Long, i As Long
Dim ExitLoop As Boolean
For Each ws In ThisWorkbook.Sheets
Set aCell = ws.Rows(1).Find(what:="Date", LookIn:=xlValues, _
lookat:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
ExitLoop = False
If Not aCell Is Nothing Then
Set bCell = aCell
ws.Columns(aCell.Column).NumberFormat = "dd/mm/yyyy;@"
lastRow = ws.Range(Split(ws.Cells(, aCell.Column).Address, "$")(1) & _
ws.Rows.Count).End(xlUp).Row
For i = 2 To lastRow
With ws.Range(Split(ws.Cells(, aCell.Column).Address, "$")(1) & i)
.FormulaR1C1 = .Value
End With
Next i
ws.Columns(aCell.Column).AutoFit
Do While ExitLoop = False
Set aCell = ws.Rows(1).FindNext(After:=aCell)
If Not aCell Is Nothing Then
If aCell.Address = bCell.Address Then Exit Do
ws.Columns(aCell.Column).NumberFormat = "dd/mm/yyyy;@"
lastRow = ws.Range(Split(ws.Cells(, aCell.Column).Address, "$")(1) & _
ws.Rows.Count).End(xlUp).Row
For i = 2 To lastRow
ws.Range(Split(ws.Cells(, aCell.Column).Address, "$")(1) & i).FormulaR1C1 = _
ws.Range(Split(ws.Cells(, aCell.Column).Address, "$")(1) & i).Value
Next i
Else
ExitLoop = True
End If
Loop
End If
Next
End Sub