vba 在不同位置的字符串中插入字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28628466/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 09:23:50  来源:igfitidea点击:

Insert characters into string in different locations

stringexcelexcel-vbatextworksheet-functionvba

提问by user3882752

I have a string which looks like this 20141007023617. And I would like to make it look like this 2014-10-07T02:36:17Z. The Tand Zare very important for me. So I would like to insert them as well.

我有一个看起来像这样的字符串20141007023617。我想让它看起来像这样2014-10-07T02:36:17Z。该TZ对于我来说非常重要。所以我也想插入它们。

So a recap,

所以回顾一下,

A1holds this string 20141007023617

A1持有这个字符串 20141007023617

B1should hold this string 2014-10-07T02:36:17Z

B1应该持有这个字符串 2014-10-07T02:36:17Z

回答by BrakNicku

Solution with TEXTfunction:

具有TEXT功能的解决方案:

=TEXT(A1,"0000-00-00\T00\:00\:00\Z")

回答by pnuts

Please try, Record Macro and:

请尝试,录制宏并:

=LEFT(A1,4)&"-"&MID(A1,5,2)&"-"&MID(A1,7,2)&"T"&MID(A1,9,2)&":"&MID(A1,11,2)&":"&MID(A1,13,2)&"Z"

回答by Dubison

On top of that if you would like to do it with VBA you can use following code. Whenever you run this code it will write the result to the next column same row.

最重要的是,如果你想用 VBA 来做,你可以使用以下代码。每当您运行此代码时,它都会将结果写入下一列同一行。

Sub arranges()
Dim str As String


str = ActiveCell.Value

ActiveCell.Offset(0, 1).Value = Left(str, 4) & "-" & Mid(str, 5, 2) & "-" & Mid(str, 7, 2) _
                                & "T" & Mid(str, 9, 2) & ":" & Mid(str, 11, 2) & ":" & Mid(str, 13, 2) & "Z"

End Sub