Create sheetnames based on imported filenames without extension

Berenloper

Board Regular
Joined
May 28, 2009
Messages
83
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:

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Try this...it splits the file name at the period....

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

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

Regards,

Berenloper
 
Upvote 0

Forum statistics

Threads
1,216,095
Messages
6,128,795
Members
449,468
Latest member
AGreen17

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