Create sheetnames based on imported filenames without extension

Berenloper

Board Regular
Joined
May 28, 2009
Messages
73
Office Version
  1. 365
Platform
  1. Windows
Hi everyone,

As part of a bigger macro I want to create sheetnames based on imported filenames.
I use this code:

Code:
Dim fs  As New FileSystemObject
Dim fo As Folder
Dim fi As File
Dim WB As Workbook
Dim ws As Worksheet
Dim sname As String
Sub loadall()
    Set WB = ThisWorkbook
    Set fo = fs.GetFolder("C:\Users\berenloper\Downloads\CSV\")
    For Each fi In fo.Files
        If UCase(Right(fi.Name, 4)) = ".CSV" Then
            sname = Replace(Replace(fi.Name, ":", "_"), "\", "-")
            Set ws = WB.Sheets.Add
            ws.Name = sname
            Call yourRecordedLoaderModified(fi.Path, ws)
        End If
    Next
End Sub

... etc

The code itself works fine but the sheetnames also contain the filename extensions. I tried to eliminate them in the line 'ws.Name = sname' but no luck. Whatever I made, everytime some error occurs.

Any help on this would be appreciated! :)

Regards,

Berenloper
 
Last edited:

Some videos you may like

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.

mrmmickle1

Well-known Member
Joined
May 11, 2012
Messages
2,456
Try this...it splits the file name at the period....

Code:
sname = Split(Fi.Name,".")(0)
ws.Name = sname
 
Last edited:

Berenloper

Board Regular
Joined
May 28, 2009
Messages
73
Office Version
  1. 365
Platform
  1. Windows
Hello Matt,

Yes! That's perfect. Thanks a lot!! :)

Regards,

Berenloper
 

Watch MrExcel Video

Forum statistics

Threads
1,122,297
Messages
5,595,301
Members
413,985
Latest member
daniel1969

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
Top