PDA

View Full Version : Reading 1st row of a CSV file into Array without opening it



volabos
03-07-2017, 12:34 PM
Hi again,

Let say I have a CSV file saved in disk. Now I want to read the 1st row of that file without opening it and import that data in some fixed Array. And I want to do that programmatically using VBA.

Could you please provide some pointer on how to achieve that?

Thanks for your time.

apo
03-13-2017, 06:53 AM
Here's a start:



Private Sub CommandButton1_Click()
Dim z, YourFile As String
YourFile = "C:\yourchoice.csv"
With GetObject(YourFile).Sheets(1)
z = .Range(.Cells(1, 1), .Cells(1, .Cells(1, .Columns.Count).End(xlToLeft).Column))
.Parent.Close
End With
End Sub

Paul_Hossler
03-13-2017, 07:22 AM
Hi again,

Let say I have a CSV file saved in disk. Now I want to read the 1st row of that file without opening it and import that data in some fixed Array. And I want to do that programmatically using VBA.

Could you please provide some pointer on how to achieve that?

Thanks for your time.


You'll have to open it somehow

You can open it as file without importing it into Excel as a worksheet

mdmackillop
03-14-2017, 06:42 AM
Sub Test()
Dim fso, f, txt, t


Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.opentextfile("C:\VBAX\Data1.csv")
txt = f.readline
'To check
t = Split(txt, ",")
Cells(1, 1).Resize(, UBound(t) + 1) = t
End Sub

snb
03-15-2017, 01:32 AM
You can't drive a car without entering it.

webeA
07-30-2018, 11:01 PM
Your response made my day - just registered to reply to you snb

niteshprabhu
10-15-2020, 09:31 PM
Hi again,

Let say I have a CSV file saved in disk. Now I want to read the 1st row of that file without opening it and import that data in some fixed Array. And I want to do that programmatically using VBA.

Could you please provide some pointer on how to achieve that?

Thanks for your time.

I don't know whether this will help, but you can use start recording macro then go to Data tab and use get data from Text/CSV, this will generate a code which can be used to read from a csv file without opening. However it will paste the data in your worksheet rather than in an array.

Paul_Hossler
10-16-2020, 05:44 AM
Welcome to the forum and thanks for the reply

However, since the question was more than 2 years old, I don't think the information will be helpful to the original requester

It's always a good idea to check the date to make sure that your response is still relevant before you take the time and effort to respond