Naming worksheet as part of file name

kaffal

Board Regular
Joined
Mar 7, 2009
Messages
68
Hi all, I have a macro to loop through the files in a folder. The macro will then open up the files and copy over to a new workbook.

As I want to rename the worksheets created in the new workbook as part of the file name. Any idea how to extract the file name out and stored in a string?

Currently it will be named after the Cell A1 in the opened file. However I would like to rename as below.

Example, the file name : 030309_Mary Sales.xls, Worksheet Name to Create : Mary Sales.
File name : 030309_John King, Worksheet Name : John King


(in the code, 030309 taken from my menu sheet,D4 is the keyword , to search in files containing D4, where D3 is the directory to look in)

Code:
Sub RunCodeOnAllXLSFiles()

Dim lCount As Long
Dim wbResults As Workbook
Dim wbCodeBook As Workbook

Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.EnableEvents = False

On Error Resume Next

Set wbCodeBook = ThisWorkbook

fpath = Sheets("menu").Range("D3")
fcriteria = Sheets("menu").Range("D4")

i = 2
    With Application.FileSearch
        .NewSearch
         'Change path to suit
        .LookIn = fpath
        .FileType = msoFileTypeExcelWorkbooks
        .FileName = fcriteria & "*.xls"
    

            If .Execute > 0 Then 'Workbooks in folder

              For lCount = 1 To .FoundFiles.Count 'Loop through all.
                 'Open Workbook x and Set a Workbook variable to it
                 Set wbResults = Workbooks.Open(FileName:=.FoundFiles(lCount), UpdateLinks:=0)


                 'DO YOUR CODE HERE
                  TabName = Range("A1").Value
                  EmptyCell = Range("C4").Value
                  If EmptyCell <> "" Then
                     ActiveSheet.Name = TabName
                                  
                     Sheets(TabName).Copy After:=wbCodeBook.Sheets(1)
                                        
                     ActiveSheet.Columns("B:AZ").AutoFit
                     
                     i = i + 1
                                                        
                     wbCodeBook.Sheets("Menu").Cells(i, 2).Value = "Completed"
                     wbCodeBook.Sheets("Menu").Cells(i + 18, 4).Value = TabName
                  
                  
                  End If
                  wbResults.Close savechanges:=False
                  
                  
                 Next lCount
                 
            End If
            
            Sheets("Menu").Select

    End With

    On Error GoTo 0
                               
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
    Application.EnableEvents = True
 
End Sub
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Did you want something like this?

<font face=Courier New><SPAN style="color:#00007F">Dim</SPAN> s <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN><br>s = Mid(WbResults.Name, InStr(WbResults.Name, "_") + 1)<br>TabName = Left(s, Len(s) - 4)</FONT>
 
Upvote 0
Hi it works!
But what happen if the file name is something like 030309_Mary Sales_today.xls, 030309_John King.xls

I only want Mary Sales, and John King to be captured.

Sorry I did not clarify my condition earlier on.
 
Upvote 0
try
Rich (BB code):
Dim x As String
x = Mid(wbResults.Name, InStrRev(wbResults.Name, "_") + 1)
 
Upvote 0
<font face=Courier New><SPAN style="color:#00007F">Dim</SPAN> s <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN><br>s = Mid(WbResults.Name, InStr(WbResults.Name, "_") + 1)<br><SPAN style="color:#00007F">If</SPAN> InStr(1, s, "_") = 0 <SPAN style="color:#00007F">Then</SPAN><br>   TabName = Left(s, Len(s) - 4)<br><SPAN style="color:#00007F">Else</SPAN><br>   TabName = Left(s, InStr(1, s, "_") - 1)<br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN></FONT>
 
Upvote 0
Code:
Dim x As String
x = Replace(Mid(wbResults.Name, InStrRev("_" & wbResults.Name, "_") + 1),"xls","")
 
Upvote 0
try
Rich (BB code):
Dim x As String
x = Mid(wbResults.Name, InStrRev(wbResults.Name, "_") + 1)

it captured the last word as the file name after "_".
For example, it captured today for file name 030309_Mary Sales_today.xls
while I wan to captured Mary Sales instead of today.

Maybe I can define x = string after the first"_" and end before the next "_" or "."
Any idea how to code this condition ?
 
Upvote 0
"Maybe I can define x = string after the first"_" and end before the next "_" or "."
Any idea how to code this condition ? "

See my previous post.
 
Upvote 0
what happen if the file name is something like 030309_Mary Sales_today.xls, 030309_John King.xls

I only want Mary Sales, and John King to be captured.
What other possible conditions do you have ?
 
Upvote 0
Does this good enough or do you have other pattern ?
Code:
Dim x As String
x = Replace(Replace(wbResults.Name,"_today.xls",""),".xls","")
x = Mid(wbResults.Name, InStrRev("_" & wbResults.Name, "_") + 1)
 
Upvote 0

Forum statistics

Threads
1,214,818
Messages
6,121,725
Members
449,049
Latest member
MiguekHeka

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