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
 

Some videos you may like

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"

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

Watch MrExcel Video

Forum statistics

Threads
1,122,670
Messages
5,597,483
Members
414,146
Latest member
marginmakerb

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