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 Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
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.
 
Upvote 0
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
 
Upvote 0
Greg

Have you tried changing Comma:=False to Comma:=True to tell VBA that the file is comma delimited?
 
Upvote 0
Hi
Its a semi-colon separated value. If I rename the *.csv to *.txt it works perfectly!
Greg
 
Upvote 0

Forum statistics

Threads
1,213,538
Messages
6,114,218
Members
448,554
Latest member
Gleisner2

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