remove file extension from sheet name import

jsalazar82

New Member
Joined
Sep 8, 2017
Messages
9
Hello, I am trying to modify this VBA to exclude the ".txt" from the sheetname after import - but not sure how to that, is it easier to just add a line to remove the last 4 characters from the sheet name after import is done? thanks

HTML:
Dim xWb As WorkbookDim xToBook As Workbook
Dim xStrPath As String
Dim xFileDialog As FileDialog
Dim xFile As String
Dim xFiles As New Collection
Dim i As Long
Set xFileDialog = Application.FileDialog(msoFileDialogFolderPicker)
xFileDialog.AllowMultiSelect = False
xFileDialog.Title = "Select a folder of TXT files"
If xFileDialog.Show = -1 Then
    xStrPath = xFileDialog.SelectedItems(1)
End If
If xStrPath = "" Then Exit Sub
If Right(xStrPath, 1) <> "\" Then xStrPath = xStrPath & "\"
xFile = Dir(xStrPath & "*.txt")
If xFile = "" Then
    MsgBox "No files found", vbInformation, "TXT to tabs"
    Exit Sub
End If
Do While xFile <> ""
    xFiles.Add xFile, xFile
    xFile = Dir()
Loop
Set xToBook = ThisWorkbook
If xFiles.Count > 0 Then
    For i = 1 To xFiles.Count
        Set xWb = Workbooks.Open(xStrPath & xFiles.Item(i))
        xWb.Worksheets(1).Copy after:=xToBook.Sheets(xToBook.Sheets.Count)
        On Error Resume Next
        ActiveSheet.Name = xWb.Name
        On Error GoTo 0
        xWb.Close False
    Next End If
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
how to remove the last 4 characters from a sheet name

is there a way to do this, i cannot find anything on the forum of via google. any help appreciated
thanks!
 
Upvote 0
Upvote 0
Re: how to remove the last 4 characters from a sheet name

How about
Code:
Sub ShtName()
'jsalazar82
    With ActiveSheet
        .Name = Left(.Name, Len(.Name) - 4)
    End With

End Sub
 
Upvote 0
Re: how to remove the last 4 characters from a sheet name

Since you got a reply to this thread, I am merging the two together, so we do not have two active threads on the same issue.
 
Upvote 0
Re: how to remove the last 4 characters from a sheet name

Seeing as you've got code on this thread, which you didn't on the other one.
Ignore post#5 & try this
Code:
ActiveSheet.Name = Left(xWb.Name,Len(xWb.Name)-4)
 
Upvote 0
Re: how to remove the last 4 characters from a sheet name

Since you got a reply to this thread, I am merging the two together, so we do not have two active threads on the same issue.

Cheers for that Joe4
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,231
Messages
6,170,884
Members
452,364
Latest member
springate

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