ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Copy CSV to variables (https://www.excelbanter.com/excel-programming/373572-copy-csv-variables.html)

Striker

Copy CSV to variables
 
I have been using Excel VBA to open a CSV file, find the last row, and copy
data to an excel file. Once in the excel file using the .offset, I assign
the cell values to a variable. Now that I have populated variables, I do an
insert query against a SQL table. This works fine, but obviously I need to
streamline the process. I would like to simply copy the values from the CSV
directly into my variables, but not sure how to do it?

Say the CSV file has 3 values separated by commas JIM,SMITH,1012223333 and I
want to copy them to Variables with the names FNAME LNAME PHONE.

Obviously I need to find the last row with data in it, and do a loop.



Thanks for any suggestions.



Tom Ogilvy

Copy CSV to variables
 
If you values are truly on the last row:

Sub ReadStraightTextFile()
Dim fName As String, lName As String
Dim Phone As String

Open "E:\Data\Testfile.CSV" For Input As #1
Do While Not EOF(1)
Input #1, fName, lName, Phone
Loop
'Close the file
Close #1
MsgBox "fname: " & fName & vbNewLine _
& "Lname: " & lName & vbNewLine _
& "Phone: " & Phone
End Sub

--
Regards,
Tom Ogilvy


"Striker" wrote in message
...
I have been using Excel VBA to open a CSV file, find the last row, and copy
data to an excel file. Once in the excel file using the .offset, I assign
the cell values to a variable. Now that I have populated variables, I do
an insert query against a SQL table. This works fine, but obviously I need
to streamline the process. I would like to simply copy the values from the
CSV directly into my variables, but not sure how to do it?

Say the CSV file has 3 values separated by commas JIM,SMITH,1012223333 and
I want to copy them to Variables with the names FNAME LNAME PHONE.

Obviously I need to find the last row with data in it, and do a loop.



Thanks for any suggestions.






All times are GMT +1. The time now is 05:49 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com