Vba 操纵日期文本
我的excel工作表中有以下文本详细信息,我需要借助VBA代码从以下文本中仅提取日期。我无法使用MID等公式,因为日期为一位数或两位数。请帮助我 借方保险范围报告2016年1月2日11月8日34Vba 操纵日期文本,vba,excel,Vba,Excel,我的excel工作表中有以下文本详细信息,我需要借助VBA代码从以下文本中仅提取日期。我无法使用MID等公式,因为日期为一位数或两位数。请帮助我 借方保险范围报告2016年1月2日11月8日34 借记卡覆盖率报告2015 14 19 33最简单的方法是将文本转换为列 在电子表格中,选择列,转到“数据”选项卡,选择“文本到列” 从那里,使用分隔符u而不是默认选项卡选择其他并完成 您将以单独的列结束 Debit Coverage Report 1 2 2016 11 8
借记卡覆盖率报告2015 14 19 33最简单的方法是将文本转换为列 在电子表格中,选择列,转到“数据”选项卡,选择“文本到列” 从那里,使用分隔符u而不是默认选项卡选择其他并完成 您将以单独的列结束
Debit Coverage Report 1 2 2016 11 8 34
Debit Coverage Report 10 11 2015 14 19 33
从数据的第三方到您的心。假设您的借记覆盖率报告前缀保留在所有字符串中,并且2016年的日期顺序为2016年2月1日至2016年2月1日,请尝试以下VBA解决方案代码:
Option Explicit
Sub ExtractDate()
Dim LastRow As Long, i As Long, ind As Long
Dim MyArr As Variant
Dim DateArr() As Date
' init Date array to a lrage size on init >> will optimize it's size later in the code
ReDim DateArr(0 To 1000)
ind = 0 ' <-- init DateArr array index
' modify "Sheet1" to your sheet's name
With Sheets("Sheet1")
' in my test all data is in column B, so looking for last row with value in column B
LastRow = .Cells(.Rows.Count, "B").End(xlUp).Row
' loop through all cells in columns B
For i = 2 To LastRow
If .Range("B" & i).Value <> "" Then ' <-- check if the cell is not empty
MyArr = Split(.Range("B" & i).Value, "_") ' <-- split the string to array elements
DateArr(ind) = DateSerial(MyArr(5), MyArr(4), MyArr(3)) ' <-- inserting the array to DateArr array
' the line below is just to show you the results on column C (next to your original string)
.Range("C" & i).Value = DateArr(ind)
ind = ind + 1
End If
Next i
ReDim Preserve DateArr(0 To ind - 1) ' <-- resize array to number of array elements found in worksheet
End With
End Sub
下面的屏幕截图显示了运行此代码后C列中的结果
前缀是allways借记卡、保险单、报告单吗?日期和其他数字在哪里?小时/分/秒?借方保险范围报告月日月日月日?那么报告1是2016年2月1日吗?@sagar您测试过以下任何答案吗?他们成功了吗?有什么反馈吗?很抱歉,先生,是的,它的工作绝对正确,很好。非常感谢您的帮助…亲爱的先生还想了解有关代码-->ReDim PREVE DateArr0到ind-1的信息