2013-05-17 89 views
6

在JAVA或C++中,我们可以沿着myString.insert(position, word)的方向执行一些操作。有没有办法在Excel VBA的字符串中做同样的事情?在我的工作表中,我有一个如下所示的字符串:01/01/995,我想在年份中插入1,因此请将其设为01/01/1995插入到Excel VBA字符串中

Dim test_date As String 
test_date = "01/25/995" 
test_date = Mid(test_date, 1, 10) & "1" & Mid(test_date, 11, 4) 

有没有另一种更简单/更优雅的方式来做到这一点?

+2

使用'中秋节$''不Mid',前者是一个字符串函数,后来的一个变体功能。字符串函数更快 – brettdj

回答

9

我不认为有一个更干净的方式做到这一点,所以你可以把它包装在一个函数。另一种做法是使用replace,但它不是更清洁。

Function Insert(source As String, str As String, i As Integer) As String 
    Insert = Replace(source, tmp, str & Right(source, Len(source)-i)) 
End Function 

或者只是修改你有什么

Function Insert(source As String, str As String, i As Integer) As String 
    Insert = Mid(source, 1, i) & str & Mid(source, i+1, Len(source)-i) 
End Function