Expected Function or variable" error on the substring .OpenText

abberyfarm

Well-known Member
Joined
Aug 14, 2011
Messages
733
Hi there,

I am trying to use the macro below to loop through text files in a folder. It is supposed to import each file one by one and save it as an excel file.

I'm getting a compiler error message "Expected Function or variable", with highlight on the substring .OpenText from above.

Would anybody know what is wrong?

Thanks


Code:
Sub exampleLoop() 
    Dim fName As String 
    Dim fPath As String 
    Dim newBook As Workbook 
    fPath = "E:\folder\" 
     
     
    If Right(fPath, 1) <> "\" Then 
        fPath = fPath & "\" 
    End If 
     
     
    fName = Dir(fPath & "*.txt") 
    Application.ScreenUpdating = False 
    Do Until fName = "" 
        Set newBook = Workbooks.OpenText(Filename:=fPath & fName, Origin:= _ 
        xlMSDOS, startRow:=1, DataType:=xlDelimited, TextQualifier:=xlDoubleQuote _ 
        , ConsecutiveDelimiter:=True, Tab:=False, Semicolon:=False, Comma:=True _ 
        , Space:=True, Other:=True, OtherChar:="^", FieldInfo:=Array(Array(1, 1) _ 
        , Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 1), Array(6, 1), Array(7, 1), Array(8, 1), _ 
        Array(9, 1)), TrailingMinusNumbers:=True) 
         
        newBook.SaveAs Filename:=fPath & Left(fName, Len(fName) - 4) & ".xlsx", _ 
        FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False 
        newBook.Close savechanges:=False 
         
         'Get next file
        fName = Dir() 
    Loop 
    Application.ScreenUpdating = True 
End Sub
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Try this ...

Code:
Sub exampleLoop()


    Dim fName As String
    Dim fPath As String   
   
    fPath = "C:\#data\Funds"
    
     If Right(fPath, 1) <> "\" Then
    fPath = fPath & "\"
    End If
     
    fName = Dir(fPath & "*.txt")
    Application.ScreenUpdating = False
    
    Do Until fName = ""
        Workbooks.OpenText Filename:=fPath & fName, Origin:= _
         xlMSDOS, StartRow:=1, DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, _
           ConsecutiveDelimiter:=True, Tab:=False, Semicolon:=False, Comma:=True, _
        Space:=True, Other:=True, OtherChar:="^", FieldInfo:=Array(Array(1, 1), _
        Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 1), Array(6, 1), Array(7, 1), Array(8, 1), _
          Array(9, 1)), TrailingMinusNumbers:=True
              '   fpa.SaveAs Filename:=fPath & Left(fName, Len(fName) - 4) & ".xlsx", _
        FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
        
        ActiveWorkbook.SaveAs Filename:=fPath & Left(fName, Len(fName) - 4) & ".xlsx", _
                                         FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
                                         
        ActiveWorkbook.Close savechanges:=False                   'Get next file
        
        fName = Dir()
     Loop
     
 Application.ScreenUpdating = True
 
 End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,213,490
Messages
6,113,956
Members
448,535
Latest member
alrossman

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