Changing part of the Date String

keith05281967

Board Regular
Joined
May 6, 2011
Messages
68
Greetings,

I have a string var that contains a file name "FILENAME_06052011"

If the file name meets a certain criteria, I want to change the "day part" , MMDDYYYY in the date to the 1st day of the month.

So "FILENAME_06052011" would become "FILENAME_06012011"

This is "VBA CODE" i'm talking about. Not a worksheet function

Is there a function in VBA that will help me? Like Right stmt? Can this be used inside a Replace?

Excel 07
thanks,
Keith
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Try like this

Code:
Sub cccc()
Dim s As String
s = "FILENAME_06052011"
s = Left(s, InStr(s, "_") + 2) & "01" & Right(s, 4)
MsgBox s
End Sub
 
Upvote 0
Using VoG's setup as a basis, here is another way to do what you want...
Code:
Sub cccc()
    Dim s As String
    s = "FILENAME_06052011"
    Mid(s, Len(s) - 5, 2) = "01"
    MsgBox s
End Sub
Using Mid in statement form (as above) is extremely fast in VB although if you are not executing this in a very large loop, you won't see its speed advantage over the originally proposed concatenation method.
 
Upvote 0
If you're using all of the string on the right, you need not specify the length to the function:

Code:
Mid(s, Len(s) - 5) = "01"
 
Upvote 0
If you're using all of the string on the right, you need not specify the length to the function:

Code:
Mid(s, Len(s) - 5) = "01"
That's true.... good point (although some may not consider it as clear a statement without the length argument).
 
Upvote 0

Forum statistics

Threads
1,224,591
Messages
6,179,768
Members
452,940
Latest member
rootytrip

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top