VBA Read Excel File

Here is a question I received

“Thanks for the video, I have a question.How to read from a group of closed excel files searching for a certain value in say column F then if that value is found then copy that row to a workbook and continue search for then next value ”

So I thought about it and answered in word and a video example:

“You could query the closed Excel file with SQL,
or you could do what I demonstrate in this video…”

'========================================================
'DESIGNED & CODED BY LOEBLCOM SERVICES 
'ERIK LOEBL(713)409-7041
'EMAIL: erik@loeblcomservices.com
'WEB:   http://loeblcomservices.com
'========================================================


Public Function query_excel_file() As Boolean
    
    Dim strResults As String
    Dim strCarrier As String
    
    'PURPOSE: Query An Excel File
    
    '*************************************
    'clear 'Excel File Contents' worksheet
    '*************************************
    Worksheets("Excel File Contents").Select
    Range("A2:E65000").Clear
    
    '*************************************
    'now populate copy the orders over
    '*************************************
    Dim strSQL As String
    Dim strReadWkbk As String
    Dim intRow As Integer
    
    '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
    
    Const adOpenStatic = 3
    Const adLockOptimistic = 3
    
    strCarrier = Range("G9") 'get the carrier's name
    
    Set s_cnn = New ADODB.Connection

    strReadWkbk = "read_this_excel_file.xls"


    s_cnn.Open "Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};" & _
        "DBQ=" & ThisWorkbook.Path & "\" & strReadWkbk & "; ReadOnly=True;"
    

    Set s_rst = New ADODB.Recordset
    

    strSQL = "SELECT Employee,Customer,[Order Date],[Shipped Date],[Ship Via] FROM [Sheet1$] WHERE  [Ship Via] = '" & strCarrier & "'"
        
    s_rst.Open strSQL, s_cnn, adOpenStatic, adLockOptimistic
    
    intRow = 2 'start inserting values at this row
    
    If Not s_rst.EOF Then
        s_rst.MoveFirst
        
        Do Until s_rst.EOF
            
            Range("A" & intRow) = s_rst.Fields(0)
            Range("B" & intRow) = s_rst.Fields(1)
            Range("C" & intRow) = s_rst.Fields(2)
            Range("D" & intRow) = s_rst.Fields(3)
            Range("E" & intRow) = s_rst.Fields(4)
                    
            intRow = intRow + 1
            s_rst.MoveNext
        Loop
        
    Else
        MsgBox "No records"
    End If
    
    s_rst.Close
    Set s_rst = Nothing
    

    s_cnn.Close
    Set s_cnn = Nothing
        
    query_excel_file = True
    
    Exit Function

End Function

Click here for the file:
VBA Read Excel File.zip

For more Excel related posts, take a look at our sister site, http://www.vbastring.com




By the way, if you got or are getting value from the VBA information, please give me a tip, thanks!


These posts may help answer your question too...

How To Escape Apostrophe In SQL Update Query

If you are looping a table with thousands of records, you’ll probably run into at least one that has an apostrophe in the field name. Like “Mike’s” or “M’cormick”, or something else. Anyway, here is one way to escape the string when you are doing your update query. Option Compare Database Sub YDriveLoop() ‘4/23/24 erik@loeblcomservices.com […]

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 […]

Previous Post

Excel VBA Read Text File

Next Post

MS Access Form Calculation On Checked Items