How to use ADO and VBA to Read from Worksheets

Поделиться
HTML-код
  • Опубликовано: 15 сен 2024
  • How to use ADO and VBA to read from worksheets
    The one big advantage of ADO is that is allows to easily read data from closed workbooks.
    So what is ADO. ADO is ActiveX Data Objects which help use to read from databases and other data sources. It is a programming library that does all the hard work for us as it provides a common interface for us to talk to all our data sources.
    In this video I am going to show you the different ways you can harness the power of ADO with lots of examples.
    DOWNLOAD THE SOURCE CODE FOR THIS VIDEO HERE: bit.ly/3GWkgfK
    FREE CHEAT SHEET: Get the free cheat sheet on VBA arrays here: (bit.ly/2MXsnz9)
    Excel VBA Training
    The Excel VBA Handbook Course(TheExcelVBAHan...)
    Webinar Archives - 60+ Hours of VBA training(excelmacromast...)
    Free Excel VBA Resources
    Excel VBA Articles (excelmacromast...)
    Related Links
    Connection Strings (www.connection...)
    VBA Class Modules - The Ultimate Guide (excelmacromast...)
    Using ADO and SQL (bit.ly/2Sk9Qjs)
    Useful VBA Shortcut Keys:
    Shift + F2: Get the definition of the item under the cursor.
    Ctrl + Shift + F2: Go to the last cursor position.
    Ctrl + Space: AutoComplete Word.
    Alt + F11: Switch between Excel and the VBA Editor.
    Ctrl + R: View the Project Explorer Window.
    F4: View the Properties Window.
    F5: Run the code from the current sub.
    F9(or click left margin): Add a breakpoint to pause the code.
    Tab: To move lines of code to the right(Indent).
    Shift + Tab: To move lines of code to the left(Outdent).

Комментарии • 192

  • @darkfelony
    @darkfelony 5 лет назад +25

    I am learning VBA alongside your videos and you are an enormous help.

  • @houstonvanhoy7767
    @houstonvanhoy7767 3 года назад +9

    After watching this video and a few others, I wonder whether Sir Paul is the only person creating technical content and teaching courses who defines ADO and other alphabet soup without just assuming that everybody knows what the heck it is. Thank you, Sir Paul.

  • @homejonny9326
    @homejonny9326 5 лет назад +1

    Why is this fantastic Excel resource soo overlooked??
    OMG, we can SQL data Worksheets!!! Even from a closed workbook!!

  • @michaelmaguire6709
    @michaelmaguire6709 4 года назад +1

    Paul, your RUclips videos and blog articles are excellent. The best out there IMHO!

  • @dejanhense9455
    @dejanhense9455 4 года назад +1

    Awesome video. ADO explained very simply.
    Greetings from Germany

  • @MrLawlorterry
    @MrLawlorterry 3 года назад

    Fantastic. This is great. I will be able to some a data migration headache with this method.

  • @bouimizarmohamed3240
    @bouimizarmohamed3240 2 года назад

    For all the VIDEOS, and continued efforts that you have made in my education, I will always be thankful.

  • @Mandelbrot567
    @Mandelbrot567 5 лет назад +2

    Excellent video, and thanks for providing the Excel file to download, this really helps .

    • @homejonny9326
      @homejonny9326 5 лет назад +1

      Yes, type yourself the code helps learning in theory, but, in practice, I do prefer copy paste the original code, understand its fundamentals and then modify it as to my own needs. :)

  • @lastdance903
    @lastdance903 10 месяцев назад

    I like the difference sources handling at 8:54. New technique learnt, thanks.

  • @benjaminhinojo9998
    @benjaminhinojo9998 5 лет назад

    Most helpful VBA content on internet so far!

  • @mtotowamungu8259
    @mtotowamungu8259 5 лет назад +1

    Thank you for this video and your social engagement. This is the best ADO introduction I've seen so far. I'm really looking forward to your next video.

  • @maciejdolinski1036
    @maciejdolinski1036 5 лет назад +1

    Great video (as all ones from you). You expand my VBA knowledge every time!

  • @karnabudhathoki5311
    @karnabudhathoki5311 3 года назад

    Thank you Paul... I have successfully used my first ADO from your tuts....Thanks ...a lot from Nepal......

  • @sasavienne
    @sasavienne 5 лет назад +1

    You are one of the best VBA teachers who show how to usefully and effectively use VBA using practical examples which are stunning. I know how to create connections using power query, but learning such a VBA code is excellent. Thanks for sharing.. 🌟 🌟 🌟 🌟

  • @MrVicmanr90
    @MrVicmanr90 5 лет назад

    Your videos are awesome for learning VBA. Thanks for sharing your knowledge.

  • @lopher70
    @lopher70 5 лет назад +1

    Great! I loved this solution, I will implement it is several crucial process in my job
    Regards from Mexico 🇲🇽

    • @homejonny9326
      @homejonny9326 5 лет назад +1

      Me too, I will do the same, in Brazil!!

  • @maciejgrabowski3393
    @maciejgrabowski3393 5 лет назад +3

    You are the best, thanks!!

  • @boomer5015
    @boomer5015 5 лет назад +2

    Interested to see how to interact with Excel Tables with ADO, hopefully that gets covered.

  • @nax9225
    @nax9225 Год назад

    Amazing sir 👌. Very easy to learn 👍

  • @TheUnicleo
    @TheUnicleo 4 года назад +2

    quite interesting! never used ADO on Excel data before! what kind of SQL can ADO use for queries? Can we make more complicated queries such as joining two or more tables / sheets? Can we use Excel tables as SQL table? I appreciate telling us more and giving a reference to study!🙏

  • @stefanogattoCH
    @stefanogattoCH 3 года назад

    Thank you very much! This is clearly explained, simple and powerful!

  • @albertoghiglia9566
    @albertoghiglia9566 4 года назад

    Good job! Very professional video. Pls continue in this direction!

  • @bondniko
    @bondniko 3 года назад

    Another jewel in my collection

  • @kamalam29
    @kamalam29 3 года назад

    I didn't know that we have to use the connection strings for both workbooks when we work with multiple workbooks. that's why I was keep getting not supported table format error. thanks, paul again.

  • @ouzytheoriginal
    @ouzytheoriginal 3 года назад

    thank you sir, its very helpful

  • @antonioravenna
    @antonioravenna 4 года назад

    thank you very much for the information.

  • @SquatFull
    @SquatFull 4 года назад

    I have used ADO and VBA to connect Acess Databases and Excel worksheets for years. Once I set up the Microsoft ActiveX Data Object on the data user's computer, the Excel workbook VBA code retrieved data from Access. Data was also sent to Access by VBA code.

  • @samivirta1250
    @samivirta1250 5 лет назад +1

    Thanks a lot for sharing, very useful!
    I might replace Power Query solution with this , in one specific case - needs more testing.

  • @jimfitch
    @jimfitch 4 года назад +10

    Hi Paul,
    Great tutorial on ADO. Best I’ve seen. I did quite a bit of it in Excel & Access some years back (Excel & Access 2007 & 2010), then shifted to other things. Getting ready to replace those old programs with modern code (including Power Query, etc.) & migration of all content from file server to SharePoint. Also, need to incorporate your advice to use arrays. (Man, the code flies when I do that!) Question: Is there a general rule about speed of arrays v. recordset? ADO v. Power Query? Or, does it depend on each situation? Are there good rules of thumb for when it is best to use each technique? Thanks for your great tutorials & willingness time share your expertise!

    • @KaiWeissmann
      @KaiWeissmann 3 года назад

      I would love to hear that as well :)

  • @Deepak_Singh1008
    @Deepak_Singh1008 3 года назад

    Superb.... It's really helpful

  • @claychan6389
    @claychan6389 5 лет назад +1

    much-awaited,thanks!

  • @ajinkyaankush3648
    @ajinkyaankush3648 2 года назад

    Thanks a lot for your valuable information on ADO. Just a quick question, how can we write data back to closed workbook using ADO??

  • @DataNovelties
    @DataNovelties 4 года назад +1

    Brilliant; very lovely.

  • @duongsamvanna1830
    @duongsamvanna1830 3 года назад +1

    Hi Master
    Can you make a video show us how we can download OLE object from access using ADODB object ?

  • @ibrahimezz4317
    @ibrahimezz4317 5 лет назад

    You are amazing just hopping for free complete serious that would help us all with amazing work you do

  • @johannesvanderzwan7122
    @johannesvanderzwan7122 5 лет назад

    Thank you very much Paul for the EXCEL Videos. It would be nice if you could add the files you are using as an example.

  • @dallas9101
    @dallas9101 3 года назад

    Thanks a lot for your video, you are a master!!!

  • @directiva108
    @directiva108 5 лет назад

    omg this was so simple and they never told me! now everything is clear and simple 8l

  • @tsaodon1786
    @tsaodon1786 4 года назад

    Great video, one problem for using ADO is that you have to select the relevant VBA Projects under Tools/Reference menu.

  • @charlesdawnbeltran9603
    @charlesdawnbeltran9603 3 года назад +1

    Amazing video. Is it possible to populate the mutiple columnn recordset into userform listbox instead of worksheet?

    • @KaiWeissmann
      @KaiWeissmann 3 года назад

      You could read the recordset into an array with
      arr = rs.GetRows
      and then load this array into the listbox.
      Hope that helps :)

  • @aleksandrshvechkov4453
    @aleksandrshvechkov4453 5 лет назад

    Very nice lesson, thank you!

  • @GabrielCagna
    @GabrielCagna 4 года назад

    amazing video! thanks so much for sharing it!

  • @jeffwright6616
    @jeffwright6616 5 лет назад

    Currently managing the build of a VSTO Excel add-in that is using ADO to interact with SQL Server data (and creating a dynamically generated Task Pane for user criteria entry). Interested in seeing your techniques. Have been thoroughly enjoying your uploads.

  • @sasavienne
    @sasavienne 5 лет назад

    Thanks in advance Paul.
    Waiting impatiently :-)

  • @prasitkeng001
    @prasitkeng001 4 года назад

    I have read the code of my colleague and the connection string that connect to the Excel worksheet is
    "Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};DBQ=" Thisworkbook.FullName
    Is this the same connection string as what you show in this video?
    Anyway, thanks for making this video. The explanation about the ADODB connection is so clear.

  • @JStruthers89
    @JStruthers89 2 года назад

    Thanks for this! Can you should how to retrieve specific named range cell values from different worksheets in a closed workbook?

  • @Djblois1
    @Djblois1 5 лет назад +3

    I wonder do you know if anyone has compared these two for speed:
    1) Vlookup (or now xlookup)
    2) An ADO connection that reads the lookup values into an array, use an ADO connection to pull in the return values and then paste it back to the sheet.

  • @thearchibaldtuttle
    @thearchibaldtuttle 5 лет назад +1

    Exciting!

  • @nadeemsalim807
    @nadeemsalim807 2 года назад

    Very good tutorial, something new to learn. Can you upload the correct source code file please? The one I have downloaded is different! Thanks

  • @reeteshism
    @reeteshism 3 года назад

    Hello there. Its a really nice video which you have made, crisp and clearly.
    I would like to ask, can ADO be used to extract or copy data from multiple workbooks in a folder into a master file, without our opening all those workbooks?

  • @trinhthanhlinh150
    @trinhthanhlinh150 5 лет назад +1

    I am waiting for your released video

  • @adamploof3528
    @adamploof3528 5 лет назад

    Fantastic, thanks for posting!

  • @1234zztechman
    @1234zztechman 3 года назад

    Nice video. Thanks for Sharing. Could you please let me know, for excel as database, queries follows MS Access SQL dialect or we can use SQL server queries as well? ,

  • @homejonny9326
    @homejonny9326 5 лет назад

    You, Sir, are amazing!!

  • @sammir6859
    @sammir6859 5 лет назад +3

    How can I get ADO to work when the workbook I am trying to connect to is already in use? Also, can it return named Excel tables (not just a named range) within a worksheet?

  • @thanhdoquang7830
    @thanhdoquang7830 4 года назад +2

    This stunning when combine insert and select statement.
    I wonder if we can combine SQL update and select statement?

    • @thanhdoquang7830
      @thanhdoquang7830 4 года назад

      I have tried this combine Insert and Select but it error the syntax.
      The difference here is I openning the souce file and try to insert into the database file.
      Pls help this case.

  • @jrcryo
    @jrcryo Год назад

    Hello paul,
    i tried based on your tutorial a query and no luck of course, on very simple sheet your tutorial works just fine.
    what i need to query is a structured table (listobject) lost in the many useless data to me.
    How can i query this data?

  • @zoranstojanovski8407
    @zoranstojanovski8407 7 месяцев назад

    In this example we can read from excel sheet, I tried same to read from excel table, but till now didn't make to work. It would be great to put excel table in ADO.recordset, manipulate data and then get back into excel.

  • @dainluso
    @dainluso 2 года назад

    Great video again, thank you Paul. Is there a way to assign the recordset directly to an array without copying it first to a worksheet?

  • @KhalilYasser
    @KhalilYasser 5 лет назад

    You're awesome. Thanks a lot for awesome videos

  • @mikhailk5690
    @mikhailk5690 5 лет назад

    Hello Paul, your videos are fantastic! In one of my projects SharePoint lists are used as a database. Although I think it is very easy to set up such "databases" there is so little information on the Web about ADO connections to SharePoint lists. I think it will be interesting for those who have a need in a database but don't want to struggle with IT to request and manage one.

  • @krn14242
    @krn14242 5 лет назад

    Thanks Paul.

  • @sudhirpatyal8407
    @sudhirpatyal8407 4 года назад

    Hi Paul
    Your lectures are very easily understandable Very nicely explain.
    I have one query that is what happen if excel file has a password and we try to open that file for edit and add data in closed excel file.

    • @Excelmacromastery
      @Excelmacromastery  3 года назад

      You can supply the password in the connection string.

  • @rogerh2694
    @rogerh2694 2 года назад +1

    When you're getting data from a closed workbook, is it actually retrieving the data from SQL?

  • @alvarojanioravichaguavitor408
    @alvarojanioravichaguavitor408 4 года назад

    Thanks a lot!

  • @TheDinga1977
    @TheDinga1977 2 года назад

    Can you use this technique to create a reccordset from an excel workbook stored on an external website?

  • @dildokafir5058
    @dildokafir5058 5 лет назад

    Hi paul,
    Thanks a ton for this short & beautiful content.
    Is there a way to include multiple excel files in zip folder using vba.

  • @guitngood7944
    @guitngood7944 4 года назад +1

    Hi - I love this solution! However a common issue that I run into, for example, is that Excel users will leave rows 1-4 blank and the headers(field names) start on row 5. So once I connect to the recordset the array Ubound = 4 when in fact there are thousands of rows in the file that are not being counted.
    Is there a workaround to skip the first four empty/null rows and the get the actual row count? rst.getrows seems to return the same count.

    • @guitngood7944
      @guitngood7944 4 года назад +1

      Posting this in case it helps others. I found this is a reliable method to get an accurate count of row data and then store it in an object i.e rowCount:
      Dim rowCount as Variant
      With rs
      .MoveLast
      .MoveFirst
      rowCount = .GetRows(.RecordCount)
      End With

  • @robertmarzec2855
    @robertmarzec2855 5 лет назад

    Super video! How do you reset the out without using the Delete rows command for Insert Into mode query? Solved: ClearFormats :) But why ClearFormats I do not know.

  • @bestscenes1469
    @bestscenes1469 5 лет назад

    Great work.

  • @tommyfilano3970
    @tommyfilano3970 5 лет назад

    Great video! Is it possible to join several sources to one table by using the sql syntax?

  • @jithinrg100
    @jithinrg100 5 лет назад +1

    Is it possible to create a userform which reads excel/txt file and show as grid view preview. And I have a standard list of columns names which should display in that user form and drag and drop those standard fields to each respective columns in the grid view?

  • @Laxmanmane007
    @Laxmanmane007 5 лет назад

    Nice video please make video on web scrapping

  • @ProtegeBlackMamba
    @ProtegeBlackMamba 5 лет назад +3

    Does ADO help with concurrency where multiple users will access the same data source at the same time?

    • @AS-ym2bp
      @AS-ym2bp 5 лет назад

      I have this exact question as well. Imagine the possibilities IF ADO helped with concurrency.

    • @sammir6859
      @sammir6859 5 лет назад

      If it's read only data source (so you don't want people writing back to it) and no one actually accesses/opens the data source directly then yes. I don't know the answer otherwise.

    • @Excelmacromastery
      @Excelmacromastery  4 года назад

      No. Concurrency is not available in Excel. You need to use a database if this is a requirement.

  • @ilyastrojnov7627
    @ilyastrojnov7627 5 лет назад +1

    Sad, what Microsoft drivers for ado to old and dont support function like full join. I have much problem with excel/VBA/ADO/ Access with big data, and now i happy use Excel/VBA(vbs)/ADO/oracle.

  • @TheNeilsolaris
    @TheNeilsolaris Год назад

    Is it possible to change data on a closed Excel workbook, using VBA? Thanks!

  • @TheHellis
    @TheHellis 5 лет назад +1

    Thank you!
    I have managed as far as your video shows but how do I write to a closed workbook?
    At work there is a group that all use the same workbook locally but they share the data from a worksheet on the network.
    This is really slow. It takes about 20-25 seconds to open the "database" worksheet write some simple values and close it. (It's a xlsb and is about 650 kb currently).
    Is it possible to write to a file with ADO and have formulas inserted in the database worksheet?

  • @paulucheoma9266
    @paulucheoma9266 2 года назад

    Paul, this is once again top notch content. I have a quick question for you. Can I use a wildcard in the file name? For instance, let's say that I have an Excel file named "Paul's Budget (2020)". The file name will always start with "Paul's Budget", but the year in the parentheses will change from (2021) to (2022) etc. The less technical approach would be to change the file name in the code every year, but can I use a wildcard like this in my file name: Paul's Budget* ? Thanks in advance for your reply.

  • @Djblois1
    @Djblois1 2 года назад

    Is it Quicker to read from a workbook using ADO (since it can be closed) or is it quicker to open the workbook and read using other methods?

  • @frikduplessis3869
    @frikduplessis3869 5 лет назад

    I can't wait,

  • @abhishekbhatt8901
    @abhishekbhatt8901 2 года назад

    Sir, Can i add rows ,columns and format cell to a closed excel file with ADO?

  • @imranbhatti8580
    @imranbhatti8580 5 лет назад

    Hi Paul! Thanks for this great video. Can we retrieve data from a closed csv file likewise?

  • @andywawa7227
    @andywawa7227 2 года назад

    Paul, very informative. How do I display only one field from a recordset in my range ("B2"). I've got a function getSQLData(query) and use Range("B2").copyfromRecordset getSqlData(query).Fields(1) but it doesn't work...

  • @Shampand
    @Shampand 5 лет назад

    Love video and this is very helpful. I have a question regarding the copyfromrecordset. Instead of putting the results into a worksheet. If I know my query will only return one result, is there a way to store this as a variable instead?

    • @Shampand
      @Shampand 5 лет назад +1

      I’ve got this I think. I’ve added in -
      rs.movefirst
      Str = rs.fields(0).value

  • @amarokmail
    @amarokmail 5 лет назад

    Also top👍🏻 But also one question😉 For the last example with the query FROM a closed workbook in my workbook I get always the Runtime error: "Cannot update. Database or object is read-only." in the row connection.execute query 🙁

    • @sapisoftware1942
      @sapisoftware1942 4 года назад

      I had the same problem, the worbook was in OneDrive folder and moved to another folder on my disk C and it works well.

  • @Rice0987
    @Rice0987 2 года назад

    And what is modUsingClass in project panel?😊

  • @dgjanes917
    @dgjanes917 4 года назад

    Is it more efficient to use ADO to copy from a closed workbook or to open it in vba and use advancedfilter?

  • @givanildosilvadeamorim326
    @givanildosilvadeamorim326 Год назад

    Hello, it is possible to connect to an excel spreadsheet in onedrive personal using ADO.

  • @kingpin11111
    @kingpin11111 3 года назад

    Hello Paul! Thanks for a great tutorial. I have a question related to what is being reffered to as "memory leaks" in excel. I read some time ago that certain actions can cause excel to not properly free objects and variables from memory, causing it to slow down over time. Those "memory leaks" persisted even if those objects were explicitly erased from the memory by setting them to nothing. It was stated that the only solution was to quit excel application a then open it again. ADO objects were mentioned as one of possible causes for such memory leaks. My question is: 1) "Is it true that some objects may not be properly cleared from the memory even if they are manually set to nothing?" 2) "If so, then Is ADO object known to be a possible cause for such problem?" Thank you very much!

  • @awadhbinamro948
    @awadhbinamro948 5 лет назад

    thanks a lot wished you included this work file

  • @srpvneto
    @srpvneto 2 года назад

    Hi! Only the Delete statement doesn't work when the Recordset is a Worksheet.

  • @atiqsarwar4222
    @atiqsarwar4222 4 года назад

    I add the data grid view from additional options, but I am unable to program it, how I can preview excel worksheet data in that data grid view

  • @RowanB86
    @RowanB86 3 года назад

    Great video. Thanks for this. If you could help with a problem we’ve been having with this kind of query at work, it would be massively appreciated. Since we started working on a new terminal server , we have discovered that SQL queries of this type will sometimes query a cached version of a worksheet table rather than the latest version. Once the workbook is saved, the query recognises the latest version of the table, but we never used to have this problem. The problem occurs sporadically. If you can provide any insight into why this might occur, it would be massively appreciated. Thank you.

  • @atiqsarwar4222
    @atiqsarwar4222 4 года назад

    Hello sir, hope you doing well, can you tell me how to data grid view programming

  • @kamil4177
    @kamil4177 2 года назад

    In your vba code you have hardcoded name of the table. How I can obtain the table name first using ado? I only now the worksheet name from other workbook where the table is.

  • @jbinmd
    @jbinmd 5 лет назад +2

    When you added WHERE > 60 and SUM...GROUP BY, it looks like you were getting numeric values back, not strings. What happened?

    • @Excelmacromastery
      @Excelmacromastery  5 лет назад

      It's an issue with ado. It puts an apostrophe in. The way around is to have a dummy record or remove apostrophe after the records are copied.

    • @jbinmd
      @jbinmd 5 лет назад

      @@Excelmacromastery You had a dummy record in there but didn't show it in the video?

    • @Excelmacromastery
      @Excelmacromastery  5 лет назад

      ​@@jbinmd I didn't use a dummy record that's why the numeric values had apostrophes.

  • @TravisFX
    @TravisFX 3 года назад

    Thanks for doing these great videos. Just getting into ADO. Am consolidating data from 12 sales files. 1 per month.
    And like to do it without opening each file. I followed the logic referring to other workbooks.
    A little confused with Connection.Open in your examples. You are connecting to "ThisWorkbook.Fullname". It seems like you are connecting to... yourself? Since the code is in the same file? I did the sourcefile added connection line and it works but we still need the Connection.Open to this workbook? Doesn't work without it. I thought we would've had to "Connection.Open" to the source file.

  • @marianraab2384
    @marianraab2384 3 года назад

    Many thanks for your tutorial it helps a lot.
    To find out the right search query string is for a non SQL user very tricky, but if you got it done then it is awesome fast.
    10x faster compared to my "old" method to opens the worksheet in background and read the data and close it again.
    And all without any trouble if the "other" worksheet is open in parallel by somebody or myself.
    Can I search in a specific range and get as return the same row from a other range,
    without run a loop through the first one and then a loop through the second with the row count?
    (Similar like a vlookup or xlookup)
    Worse enough I don't have headers like "Fruit" from your example.

  • @richardrossouw
    @richardrossouw 4 года назад +1

    Hi. I want to do the same but Select from an predefined Table "MyTable" in a sheet. Whatever I do I get an Run Time error that it cant find object MyTable.
    Select * from MyTable ; OR [MyTable] OR [MyTable$] or [Sheet1$MyTable] . So can you do this from an pre-defined Range or table and how do you reference it.

    • @lav1093
      @lav1093 3 года назад +1

      hey, could you solve it?

  • @kennethzagone2638
    @kennethzagone2638 6 месяцев назад

    I tried to use a query: Delete * From table where field=data, and received an error saying: Deleting data in a linked table is not supported by this ISAM.