【问题标题】:Format string to date with double digit day and month using Excel VBA使用 Excel VBA 将字符串格式化为具有两位数日期和月份的日期
【发布时间】:2022-01-16 11:06:04
【问题描述】:

我的工作表有这些公式。 T 列中的一些值是日期,一些是字符串或一般值:

及其价值观:

我有这个VBA 代码循环遍历列中的每个单元格并将字符串值转换为日期并格式化日期。但是,对于两位数的月份和日期,它工作得非常好,但不是个位数。问题的原因是什么,我怎样才能让它总是导致两位数,即“01 - 02 -2021”,不是“1/2/2021”?

Sub Date_format()

    Dim Cel As Range
    Dim i As Integer
    i = ActiveWorkbook.Sheets(1).Cells(Rows.Count, 1).End(xlUp).Row

    ' First I applied mm - dd as the same as the work sheet
    ActiveSheet.Range("T2:T" & i).NumberFormat = "mm-dd-yyyy"

    For Each Cel In ActiveSheet.Range("T2:T" & i)
        If Not Cel.Value = "n/a" Then

            ' Then I applied dd - mm to swap the day and month
            Cel.Value = Format(DateValue(Cel.Value), "dd - mm - yyyy")
        End If
    Next

End Sub

一旦我将.NumberFormat = "dd-mm-yyyy" 应用于该范围,我的另一个计算天数的公式=DAYS(TODAY(),T2) 将不再适用于大多数单元格,如下图所示:

【问题讨论】:

  • 在 FOR 循环前添加 .Range("T2:T" & i).NumberFormat = "dd - mm - yyyy"
  • once I applied .NumberFormat = "mm-dd-yyyy" to the range my other formula =DAYS(TODAY(),T2) that calculate days are not working anymore on some cells 哪些单元格?
  • 您提到您已申请.NumberFormat = "mm-dd-yyyy" 但T5 显示“dd-mm-yyyy”?您还可以使用您正在使用的当前代码更新问题吗?
  • @SiddharthRout 首先我申请了mm - dd 与工作表相同。然后我重新申请dd - mm 来交换日期和月份。我认为问题出在日期格式上?
  • Cel.Value = Format(DateValue(Cel.Value), "dd - mm - yyyy") 更改为Cel.Value =DateValue(Cel.Value)。您已经应用了格式。无需在循环中应用它。

标签: excel vba date


【解决方案1】:

请尝试转换这部分:

If Not Cel.Value = "n/a" Then
    Cel.Value = Format(DateValue(Cel.Value), "dd - mm - yyyy")
End If

作为:

If Not Cel.Value = "n/a" Then
    Cel.NumberFormat = "dd - mm - yyyy"
    Cel.Value = Format(DateValue(Cel.Value), "dd - mm - yyyy")
End If

【讨论】:

  • @Siddharth Rout 甚至,更好... :)
  • 你能解释一下吗!为什么它不能在单行中工作?
  • @Ibn e Ashiq 它不是在一行中工作吗? “不能单行工作”是什么意思?它应该可以工作......我只尝试在循环内调整您现有的代码。但是在循环之前放置这样一行(以格式化所有范围),它将完成这项工作(以更简单的方式)代码。而且快一点,不是每个单元格的格式...
  • 感谢详细回答,因为我应用了.NumberFormat = "dd - mm - yyyy" 我的其他公式不起作用,即=DAYS(TODAY(),T2)
  • @Ibn e Ashiq 这意味着在以这种方式运行代码后,您在“T:T”列中的未格式化为日期。我首先建议删除所有代码,只保留ActiveSheet.Range("T2:T" & i).NumberFormat = "mm-dd-yyyy"。看来您所说的不是真的:该列的格式为Date,而不是文本...在应用您的代码后变为文本。上面的行根据您的需要对其进行格式化(作为日期),而您的代码将创建字符串...
【解决方案2】:

正如我在上面的 cmets 中提到的,添加

.Range("T2:T" & i).NumberFormat = "dd - mm - yyyy" 

FOR 循环之前。

这是另一种不使用循环实现您想要的方法。

代码:

Option Explicit

Sub Date_format()
    Dim ws As Worksheet
    Dim lRow As Long
    
    '~~> Change this to the relevant sheet
    Set ws = Sheet1
    
    With ws
        '~~> Last row in Col T
        lRow = .Range("T" & .Rows.Count).End(xlUp).Row
        
        With .Range("T2:T" & lRow)
            .NumberFormat = "dd - mm - yyyy"
            
            .Value = ws.Evaluate("index(IF(" & .Address & _
                     "<>""n/a"",DATEVALUE(" & .Address & "),""n/a""),)")
        End With
    End With
End Sub

截图:

说明:

这种方法使用EVALUATEINDEXIFDATEVALUE 进行转换,而不使用任何循环。有关其工作原理的说明,请参阅Convert an entire range to uppercase without looping through all the cells

【讨论】:

  • 请看我试图用图片解释的问题
猜你喜欢
  • 2020-10-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多