So how can you import the contents of a csv or
text file into your Excel worksheet:
Find out:
'======================================================== 'DESIGNED & CODED BY LOEBLCOM SERVICES 2013 'ERIK LOEBL(713)409-7041 'EMAIL: erik@loeblcomservices.com 'WEB: http://loeblcomservices.com '======================================================== Sub query_text_file() 'PURPOSE: Query A Text File '************************************* 'clear 'Text File Contents' worksheet '************************************* Worksheets("Text File Contents").Select Range("A2:K6500").Clear '************************************* 'now populate copy the orders over '************************************* Dim strPath As String 'Need to reference the: ' Microsoft ActiveX Data Objects 2.5 Library Dim s_rst As ADODB.Recordset Dim s_cnn As ADODB.Connection 's for sub connection Dim intRow As Integer Const adOpenStatic = 3 Const adLockOptimistic = 3 Const adCmdText = &H1 Set s_cnn = New ADODB.Connection strToolWkbk = "read_file.txt" strPath = ThisWorkbook.Path & "\read_file" Debug.Print strPath 'For a text file, Data Source is the folder, not the file s_cnn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strPath & ";" _ & "Extended Properties=""text;HDR=Yes;FMT=Delimited"";" s_cnn.Open Set s_rst = New ADODB.Recordset strSQL = "SELECT * FROM " & strToolWkbk s_rst.Open strSQL, _ s_cnn, adOpenStatic, adLockOptimistic, adCmdText intRow = 2 s_rst.MoveFirst Do Until s_rst.EOF Range("A" & intRow) = s_rst(0) Range("B" & intRow) = s_rst(1) Range("C" & intRow) = s_rst(2) Range("D" & intRow) = s_rst(3) Range("E" & intRow) = s_rst(4) Range("F" & intRow) = s_rst(5) Range("G" & intRow) = s_rst(6) Range("H" & intRow) = s_rst(7) Range("I" & intRow) = s_rst(8) Range("J" & intRow) = s_rst(9) intRow = intRow + 1 s_rst.MoveNext Loop Range("A:C").Select Selection.NumberFormat = "General" Range("D:D").Select Selection.NumberFormat = "m/d/yyyy" Range("E:K").Select Selection.NumberFormat = "General" Range("A1").Select MsgBox "DONE." s_rst.Close s_cnn.Close Set s_rst = Nothing Set s_cnn = Nothing End Sub
Click here for the file:
VBA Read Excel File.zip
How To Parse A Flat File In Excel VBA
In another post I demonstrated how to access a file on your computer using the MS Office Library. Here it is if you don’t know what I’m talking about. In this post, I am going to show you how to access the file and load it into your spreadsheet. I will do the same thing […]
How to pick a file to load In VBA
Picking a file to load in your Microsoft App is a very important skill to know. In this blog post you will see how to do it. First you need to set a reference to the MS office object library From VBE editor –> select Tools > MS office object library (click check mark) Sub […]
How can I interact with other Office applications (Excel) using VBA in Access?
Need to write your Access data or query to an Excel file? Here is the how to do it: Most people are familiar with Excel and know how to use it well (enough), and when you start talking about Access, they get scared off, and don’t know what to do anymore. Well, here you are […]
How To Create A Parameter Query In Access
A parameter query changes your ordinary static access query to be more dynamic and interactive. It will ask you a question about what you want to search for, allowing you to do a search query multiple times instead of just once. You can do your parameter query straight from the QBE (Query By Example) Editor, […]