【问题标题】:Excel VBA - Date SubstituteExcel VBA - 日期替换
【发布时间】:2021-04-24 03:54:21
【问题描述】:

我即将使用 MS Excel 中的 VBA 从 YY-MM-DD 的文本格式到 DD-MM-YY 替换整行中的数据格式。我试图从一开始就删除撇号,但日期 21-04-02 被识别为 21.04.2002 而不是 02.04.2021,因此需要替换整行中每个 ell 中的字符。

在下面的代码中,我需要为每个单元格添加替换部分

Sub DateSub()
   Dim strInput As String, strOutput As String
   Dim LastRowcheck As Long, n1 As Long, rowschecktodelete As Long

  LastRowcheck = Sheets("T1").Range("B" & Rows.Count).End(xlUp).Row

  For n1 = 2 To LastRowcheck
    With Worksheets("Sheet1").Cells(n1, 2)
     'HERE SHOULD BE REPLACING PART
        Sheets("T1").Cells(n1, 2).Select
        Selection.NumberFormat = "yy-mm-dd"
   End With
  Next n1
    Application.DisplayAlerts = False

End Sub 

【问题讨论】:

  • 一个公式可以解决这个问题。假设您的日期为A1 = '21-04-02。将公式=DATE(2000+LEFT(A1,2),MID(A1,4,2),RIGHT(A1,2)) 放入单元格B1。如果您仍然想要 vba,那么在此处使用相同的逻辑
  • 我相信这会起作用,但我是 VBA 新手,我想念一些使它正常工作的东西,我想用这种方式替换 B 列中的每个单元格。 Sheets("T1" ).Cells(n1, 2) = DATE(2000+LEFT(nl,2),MID(A1,4,2),RIGHT(nl,2))

标签: excel vba


【解决方案1】:

如果您想要 VBA,那么您可以一次性转换整个范围而无需循环,如下所示

Option Explicit

Sub Sample()
    Dim ws As Worksheet
    Dim rng As Range
    Dim sAddr As String
    Dim lRow As Long
    
    Set ws = Worksheets("Sheet1")
    
    With ws
        lRow = .Range("B" & .Rows.Count).End(xlUp).Row
        
        Set rng = Range("T1:T" & lRow)
        sAddr = rng.Address

        rng = Evaluate("index(DATE(2000+LEFT(" & sAddr & _
                              ",2),MID(" & sAddr & _
                              ",4,2),RIGHT(" & sAddr & _
                              ",2)),)")
                              
        rng.NumberFormat = "DD-MM-YY"
    End With
End Sub

要了解其工作原理,请参阅Convert an entire range to uppercase without looping through all the cells

行动中

【讨论】:

    【解决方案2】:
       Dim strInput As String, strOutput As String
       Dim LastRowcheck As Long, n1 As Long, rowschecktodelete As Long
    
      LastRowcheck = Sheets("T1").Range("B" & Rows.Count).End(xlUp).Row
    
      For n1 = 2 To LastRowcheck
        With Worksheets("T1").Cells(n1, 2)
         'HERE SHOULD BE REPLACING PART
            d1 = Sheets("T1").Cells(n1, 2).Value
            Sheets("T1").Cells(n1, 2).Value = Mid(d1, 7, 2) + "-" + Mid(d1, 4, 2) + "-" + Mid(d1, 1, 2)
            
            Sheets("T1").Cells(n1, 2).Select
            'Selection.NumberFormat = "yy-mm-dd"
            Selection.NumberFormat = "dd-mm-yy"
            
       End With
      Next n1
        Application.DisplayAlerts = False
    

    【讨论】:

      【解决方案3】:

      您可以使用Text to Columns 方法。

      例如:

      Option Explicit
      Sub convDates()
          Const convCol As Long = 1
          Dim WS As Worksheet, Rng As Range
          
      'change sheetName, convCol and destination to your specifications if this works for you.
          
      Set WS = Worksheets("sheet5")
      With WS
          Set Rng = Range(.Cells(1, convCol), .Cells(.Rows.Count, convCol).End(xlUp))
          Rng.TextToColumns _
              Destination:=.Cells(1, convCol + 1), _
              DataType:=xlDelimited, _
              textqualifier:=xlTextQualifierDoubleQuote, _
              Consecutivedelimiter:=True, _
              Tab:=False, _
              semicolon:=False, _
              Space:=False, _
              other:=False, _
              fieldinfo:=Array(1, xlYMDFormat)
      End With
      End Sub
      

      在上面的代码中,convCol 是您要转换的列(本例中的列 A) 另请注意,我们将结果写入左侧一列(请参阅Destination:= 参数。

      如果您对它的工作方式感到满意,您可以将convCol 更改为您感兴趣的列(可能是B 或2 列),并通过从Destination:= 参数中删除+1,覆盖原始数据.

      【讨论】:

        猜你喜欢
        • 2018-10-27
        • 2016-06-25
        • 2013-04-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-02
        • 2019-04-16
        • 1970-01-01
        相关资源
        最近更新 更多