VBA to open text files in EXCEL as text format

kdaniel86

New Member
Joined
Aug 1, 2014
Messages
35
Hi all,
I'd like to create an excel vba which can do the following:

An excel sheet contains filepath of the files to be opened.

1. Loops through the 2nd column and opens the file in Text format so that formulas are written as strings.
2. If the file is already open, it does not open the file and moves to the next line.

Code:
Sub OpenAsTextFormat()


Dim LastRow as Long
Dim afilepath as String


	LastRow = Cells(Rows.Count, "B").End(xlUp).Row
	For x = 1 to LastRow
		afilepath = Cells(x, 2)

I don't know how to continue from here.

Any help would be appreciated.
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
I got as far as

Code:
LastRow1 = Cells(Rows.Count, "A").End(xlUp).Row
    For y = 1 To LastRow1
        afilepath = Cells(y, 2)
        On Error Resume Next
        Set WB = Workbooks(afilepath)
        On Error GoTo 0
        If WB Is Nothing Then
            Set WB = Workbooks.Open(afilepath)
        End If
        
    Next y

This opens file if it isn't already open and works perfect. However, how can I make it so that excel opens the file in TEXT FORMAT?

​any help would be appreciated
 
Upvote 0
Hi,

Have you tried Workbook.OpenText method.
I'm not sure what minimum version of Excel is required for that though.
 
Upvote 0
Hi,
I've worked on the code and I've successfully opened the files using this code but I still have a problem.


Code:
Sub Openthem()
Dim LastRow1 As Long, y As Long
Dim WB As String


    WB = ActiveWorkbook.Name


    LastRow1 = Cells(Rows.Count, "B").End(xlUp).Row
    For y = 1 To LastRow1
        afilepath = Cells(y, 2)
        Workbooks.OpenText (afilepath)
        Workbooks(WB).Activate
        
    Next y


End Sub

I am still not able to open these files in string format as shown in screenshots below.

Orig Text File

20j5s7a.png


Excel File After opening with VBA

295bo6b.png


As you can see, texts that start with "-" is showing as #Name?

What piece of code/parameter do I need to add to prevent this from happening?

Any help would be appreciated.
 
Upvote 0
Hi,
I've worked on the code and I've successfully opened the files using this code but I still have a problem.


Code:
Sub Openthem()
Dim LastRow1 As Long, y As Long
Dim WB As String


    WB = ActiveWorkbook.Name


    LastRow1 = Cells(Rows.Count, "B").End(xlUp).Row
    For y = 1 To LastRow1
        afilepath = Cells(y, 2)
        Workbooks.OpenText (afilepath)
        Workbooks(WB).Activate
        
    Next y


End Sub

I am still not able to open these files in string format as shown in screenshots below.

Orig Text File

20j5s7a.png


Excel File After opening with VBA

295bo6b.png


As you can see, texts that start with "-" is showing as #Name?

What piece of code/parameter do I need to add to prevent this from happening?

Any help would be appreciated.


can anybody help me?
 
Upvote 0

Forum statistics

Threads
1,213,546
Messages
6,114,251
Members
448,556
Latest member
peterhess2002

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