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.
 

Some videos you may like

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.

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,828
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



Excel File After opening with VBA



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



Excel File After opening with VBA



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?
 

Watch MrExcel Video

Forum statistics

Threads
1,108,507
Messages
5,523,309
Members
409,510
Latest member
HQ2401

This Week's Hot Topics

Top