VBA查找&更换更改日期为我们格式

发布于 2025-02-10 02:13:03 字数 879 浏览 3 评论 0原文

当我通过VBA查找并替换时,它将我的日期更改为我们的格式。因此,我有一列日期,但是它们都以我想删除的文本为前缀(例如发票日期:dd/mm/yyyy)。当我使用CTRL + F并手动替换时,一切都很棒。删除文本,日期保留为原始格式dd/mm/yyyy。但是,如果使用VBA进行此操作时,如果一天小于12(即一年的月份),则将日期更改为mm/dd/yyyy。我已经尝试了多种不同的方法来转换它,但是它们似乎都有相同的问题。这是我的最新失败...

Sub DateConvert()

    Sheets("Sheet1").Select

    Dim strValue As String
    Dim RowCount As Integer
    Dim x As Integer
    Dim DateValue As Date

    RowCount = WorksheetFunction.CountA(Range("C1", Range("C1").End(xlDown)))

    For x = 2 To RowCount
        'changes cell value to a string
        strValue = Cells(x, 3).Value
        'removes unwanted text
        Cells(x, 3).Replace _
        What:="Invoice Date:", Replacement:=""
        'changes to string to desired date format
        DateValue = Cells(x, 3).NumberFormat = "dd/mm/yyyy"
        
    Next x
       

End Sub

请有人在笔记本电脑或我离开窗户之前避免我的这种痛苦。 提前致谢

when I run find and replace through vba it changes my dates to us format. So I have a column of dates, but they are all prefixed with text that I want to remove (like so Invoice Date:dd/mm/yyyy). When I use Ctrl + F and replace manually, it's all great. Removes the text, the date remains in it's original format dd/mm/yyyy. However, when using vba to do this it changes the dates to mm/dd/yyyy if the the day is less than 12 (ie months in a year). I've tried a number of different methods to convert it but they all seem to have the same problem. Here is my latest failure...

Sub DateConvert()

    Sheets("Sheet1").Select

    Dim strValue As String
    Dim RowCount As Integer
    Dim x As Integer
    Dim DateValue As Date

    RowCount = WorksheetFunction.CountA(Range("C1", Range("C1").End(xlDown)))

    For x = 2 To RowCount
        'changes cell value to a string
        strValue = Cells(x, 3).Value
        'removes unwanted text
        Cells(x, 3).Replace _
        What:="Invoice Date:", Replacement:=""
        'changes to string to desired date format
        DateValue = Cells(x, 3).NumberFormat = "dd/mm/yyyy"
        
    Next x
       

End Sub

Please, someone spare me this misery before either the laptop or me go out the window.
Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

墨小墨 2025-02-17 02:13:03

日期非常烦人。确保您使用正确日期的最佳选择是使用日期函数,然后根据需要格式化输出:

Sub DateConvert()
    
    Dim ws As Worksheet:    Set ws = ActiveWorkbook.Worksheets("Sheet1")
    Dim rData As Range:     Set rData = ws.Range("C2", ws.Cells(ws.Rows.Count, "C").End(xlUp))
    If rData.Row < 2 Then Exit Sub  'No data
    
    'Load range data into an array
    Dim aData() As Variant
    If rData.Cells.Count = 1 Then
        ReDim aData(1 To 1, 1 To 1)
        aData(1, 1) = rData.Value
    Else
        aData = rData.Value
    End If
    
    'Loop over array and perform conversion
    Dim aDateValues As Variant
    Dim i As Long
    For i = 1 To UBound(aData, 1)
        aDateValues = Split(Replace(aData(i, 1), "Invoice Date:", vbNullString), "/")   'Remove the extra text and pull the date values
        aData(i, 1) = DateSerial(aDateValues(2), aDateValues(1), aDateValues(0))        'Use DateSerial to guarantee correct date
    Next i
    
    'Output results to sheet with desired date format
    With rData
        .Value = aData
        .NumberFormat = "dd/mm/yyyy"
    End With
    
End Sub

Dates are extremely annoying to work with. Your best bet for making sure you're working with the correct date is to use the DateSerial function, and then format the output as desired:

Sub DateConvert()
    
    Dim ws As Worksheet:    Set ws = ActiveWorkbook.Worksheets("Sheet1")
    Dim rData As Range:     Set rData = ws.Range("C2", ws.Cells(ws.Rows.Count, "C").End(xlUp))
    If rData.Row < 2 Then Exit Sub  'No data
    
    'Load range data into an array
    Dim aData() As Variant
    If rData.Cells.Count = 1 Then
        ReDim aData(1 To 1, 1 To 1)
        aData(1, 1) = rData.Value
    Else
        aData = rData.Value
    End If
    
    'Loop over array and perform conversion
    Dim aDateValues As Variant
    Dim i As Long
    For i = 1 To UBound(aData, 1)
        aDateValues = Split(Replace(aData(i, 1), "Invoice Date:", vbNullString), "/")   'Remove the extra text and pull the date values
        aData(i, 1) = DateSerial(aDateValues(2), aDateValues(1), aDateValues(0))        'Use DateSerial to guarantee correct date
    Next i
    
    'Output results to sheet with desired date format
    With rData
        .Value = aData
        .NumberFormat = "dd/mm/yyyy"
    End With
    
End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文