InStr Function

Boris7265

Board Regular
Joined
Apr 6, 2011
Messages
68
Hello everybody .

I am adding the new sheet to existing workbook and the file that is being imported name like this 07-13-11.xls and I would like to name the new sheet as 071311. The below is code that I am able come up so far and it return the name 07-13-11.xls. I am not gof to use InStr Function , please really apreciate for help:
Sub AddWorksheet()
Dim FileName2 As String
Dim Filename1 As String
Dim FileName3 As String
Dim FileName4 As String
Dim FileName5 As String
Dim Year As String
Dim Filename As String
Dim FLName As String
FLName = Application.GetOpenFilename
Dim NewSheet As Worksheet
Filename1 = Right(FLName, 12)
FileName2 = Mid(Filename1, InStr(Filename1, "-") + 8, 12)
FileName3 = Mid(Filename1, InStr(Filename1, "-") + 1, 2)
FileName4 = Mid(Filename1, InStr(Filename1, "-") + 3, 2)
Year = "2011"
FileName5 = FileName3 + "/" + FileName4 + "/" + Year
Worksheets.Add().Name = Filename1
Set NewSheet = ActiveSheet

Thank you in advance,

Best Regards,

BorisGomel
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
What exactly do you want to name the worksheet?

Is it the name of the workbook the user select without the '-' and file extension?

If it is you want to strip out a specific character like '-' with this:
Code:
FLName = Replace(FLName, "-", "")
And to remove the extension:
Code:
FLName = Left(FLName, InStrRev(FLName, ".") - 1)

If used InStrRev but you could use InStr.
 
Upvote 0
Something like tHis might be less hassle:

<font face=Calibri><SPAN style="color:#00007F">Sub</SPAN> foo()<br>    <SPAN style="color:#00007F">Dim</SPAN> FLName <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN><br>        FLName = Range("A1")<br>        FLName = Left(FLName, Len(FLName) - 4)<br>        <br>        MsgBox Format(FLName, "mmddyy")<br>    <br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br></FONT>

HTH,
 
Upvote 0
Thank you all of you for your help.

I played with codes you have provided and it is working very well .

Again , thank you very much ,

The best Regards,

orisGomel
 
Upvote 0

Forum statistics

Threads
1,224,527
Messages
6,179,331
Members
452,907
Latest member
Roland Deschain

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