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

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.

kdaniel86

New Member
Joined
Aug 1, 2014
Messages
35
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
 

daverunt

Well-known Member
Joined
Jul 9, 2009
Messages
1,908
Office Version
  1. 2013
Platform
  1. Windows
Hi,

Have you tried Workbook.OpenText method.
I'm not sure what minimum version of Excel is required for that though.
 

kdaniel86

New Member
Joined
Aug 1, 2014
Messages
35

ADVERTISEMENT

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.
 

kdaniel86

New Member
Joined
Aug 1, 2014
Messages
35
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?
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,163,699
Messages
5,833,201
Members
430,196
Latest member
rez5656

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