Paste in Excel VBA string

In JAVA or C ++, we can do something along the line myString.insert(position, word) . Is there a way that we can do the same in an Excel VBA line? On my sheet, my line looks like this: 01 / 01 / 995 , I want to insert 1 per year, so do it 01 / 01 / 1995 .

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

Is there an even simpler / more elegant way to do this?

+6
source share
1 answer

I don’t think there is a cleaner way to do this so that you can simply wrap it with a function. Another way to do this would be with replace , but it is not any cleaner.

 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 

or just change what you have

 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 
+9
source

Source: https://habr.com/ru/post/945295/


All Articles