opening a csv file with the workbooks.opentext command.

doogis

New Member
Joined
Feb 7, 2016
Messages
27
Hello,
I recently found an example of a macro online to open a *.txt file. I was wondering if there is a workaround to open a *.csv file?

Thanks in advance.

Greg

Private Sub CommandButton4_Click()

' Ask the user for the file name to open.
myFile = Application.GetOpenFilename
ChDir "d:\test"
' Check for the Cancel button.
If myFile = False Then Exit Sub

' Open the Text file with the OpenText method.
Workbooks.OpenText Filename:=myFile, Origin:=xlWindows, _
StartRow:=1, DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, _
Tab:=False, Semicolon:=True, Comma:=False, Space:=False, _
Other:=False, FieldInfo:=Array(1, 1)
ActiveSheet.Name = "sheet1"


End Sub
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"

Tinbendr

Well-known Member
Joined
Jul 21, 2010
Messages
997
Your question is a little vague since knowing what your are doing with the data might determine the method suggested.

But here are a couple of choices.

File Scripting
or the built in VBA commands, Open File For input as #1. Then either Input or Line Input.
 

doogis

New Member
Joined
Feb 7, 2016
Messages
27
I simply want to open a delimited csv file in excel to massage it a bit. In order for the above script to work i have to change my files to extension *.txt. If it is in *.csv it doesn't open the file properly.
thanks,
Greg
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,344
Office Version
  1. 365
Platform
  1. Windows
Greg

Have you tried changing Comma:=False to Comma:=True to tell VBA that the file is comma delimited?
 

doogis

New Member
Joined
Feb 7, 2016
Messages
27
Hi
Its a semi-colon separated value. If I rename the *.csv to *.txt it works perfectly!
Greg
 
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,879
Messages
5,834,208
Members
430,263
Latest member
abz54

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