VBA code to user select file without opening convert the extension to .txt from .xls

SamCha

New Member
Joined
Nov 23, 2020
Messages
30
Office Version
  1. 2016
Platform
  1. Windows
Hi all,

Please can you help me on this:

Have few files which have the data in either xml, xhtml,txt format but the extension is xls. The files cannot with opened in secured environment.

We can open the file when we change the extension to .txt. Open the MS 365 excel file > Open browse the .txt file>delimited>save as .xls. Then delete the .txt file from the folder.

The above consumes lot of time. Please can you suggest using vba how this can be done quickly. Was trying to use the below to open the dialogue box to select the file. But couldn't really understand how to use Name As function in vba to convert the extension to .xls without opening the selected file.

VBA Code:
With Application.FileDialog(msoFileDialogFilePicker)
        .AllowMultiSelect = False 
        .Filters.Add "Excel Files", "*.xlsx; *.xlsm; *.xls; *.xlsb
        .Show
        fullpath = .SelectedItems.Item(1)
    End With
    If InStr(fullpath, ".xls") = 0 Then
        Exit Sub
End Sub

thanks
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Hello. First you need to click the Tools menu in your VBA editor, then References. Look for Microsoft.Scripting.Runtime and check it. Then use this modified code. Change NewName to whatever you want.

VBA Code:
Sub Rename()

    Dim fso As FileSystemObject
    Dim f As File
   
    Set fso = New FileSystemObject
   
    With Application.FileDialog(msoFileDialogFilePicker)
        .AllowMultiSelect = False
        .Filters.Add "Excel Files", "*.xlsx; *.xlsm; *.xls; *.xlsb"
        .Show
        fullPath = .SelectedItems.Item(1)
    End With
    If InStr(fullPath, ".xls") = 0 Then
        Exit Sub
    End If
   
    Set f = fso.GetFile(fullPath)
    f.Name = "NewName.xls"
   
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,907
Messages
6,122,181
Members
449,071
Latest member
cdnMech

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