Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel VBA-日期替换_Excel_Vba - Fatal编程技术网

Excel VBA-日期替换

Excel VBA-日期替换,excel,vba,Excel,Vba,我将使用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, row

我将使用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 

如果需要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
要了解其工作原理,请参阅

在行动中


您可以使用
文本到列的方法

例如:

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列),并通过从
目标:=
参数中删除
+1
,覆盖原始数据


一个公式可以解决这个问题。让我们假设你的日期在
A1
=
'21-04-02
中。把公式
=日期(2000+左(A1,2),中(A1,4,2),右(A1,2))
在单元格
B1
中。如果您仍然需要vba,则使用相同的逻辑。我确信这会起作用,但我是vba新手,我错过了一些使其正常工作的方法,我希望以这种方式替换B列中的每个单元格。表格(“T1”)。单元格(n1,2)=日期(2000+左(nl,2),中(A1,4,2),右(nl,2))
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