问题描述
我有一个宏,该宏将日期作为文本粘贴到一列中,并且我在某处阅读过,将文本转换回日期的最佳解决方案是搜索并替换"/"
.
I have a macro which pastes dates as text in a column and I have read somewhere, the best solution to convert the text back to dates is to do a search and replace of "/"
.
如果我在Excel中手动执行此操作,它就像微风一样工作.但是,当我尝试使用宏执行此操作时,我看不到将单元格转换为日期.
If I do this manually in Excel, it works like a breeze. However, when I try to do this with a macro, I do not see the cells being converted to dates.
这是宏记录器给我的以及我要使用的内容:
This is what the macro recorder gave me and what I am trying to use:
Range("t_drill[Date]").Replace What:="/", Replacement:="/", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=True, SearchFormat:=False, ReplaceFormat:=False
推荐答案
使用TextToColumns转换日期:
Using TextToColumns to convert date:
这是针对DMY的日期格式,如果您使用其他日期格式,则必须进行调整.
This is for the date format DMY, if you use a different date format then you will have to make adjustments.
Range("t_drill[Date]").TextToColumns Destination:=Range("t_drill[Date]"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _
:=Array(1, 4), TrailingMinusNumbers:=True
要针对不同的日期格式进行调整,请根据需要进行更改:
To adjust for different date formats change as required:
DMY := Array(1,4)
YMD := Array(1,5)
MYD := Array(1,6)
DYM := Array(1,7)
YDM := Array(1,8)
这篇关于Excel VBA-替换表列中的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!