How To Use A Closed Workbook As A Database (ADO) Using VBA In Excel?
Tags: touseaclosedworkbook, vbamacros
Published: 2021-10-01 05:26:23

---
title: How To Use A Closed Workbook As A Database (ADO) Using VBA In Excel?
published: true
date: 2021-09-30 07:25:25 UTC
tags: ToUseAClosedWorkbook,VBAMacros
canonical_url: https://geekexcel.com/how-to-use-a-closed-workbook-as-a-database-ado-using-vba-in-excel/
---

In this tutorial, we will guide you to learn how to **use a closed workbook as a database (ADO) using VBA **in Excel. Let’s get them below!! Get an official version of MS Excel from the following link: **[https://www.microsoft.com/en-in/microsoft-365/excel](https://www.microsoft.com/en-in/microsoft-365/excel)**

## Example

- Firstly, in the Excel Worksheet, you have to go to the  **Developer Tab.**
- Then, you need to ** ** select the  **Visual Basic**  option under the  **Code**  section.

![Select Visual Basic](https://geekexcel.com/wp-content/uploads/2021/09/Select-Visual-Basic-9-1024x180.png)<figcaption>Select Visual Basic</figcaption>

- Now, you have to  **copy and paste the code**  given below.

```
Sub GetWorksheetData(strSourceFile As String, strSQL As String, TargetCell As Range)
Dim cn As ADODB.Connection, rs As ADODB.Recordset, f As Integer, r As Long
If TargetCell Is Nothing Then Exit Sub
Set cn = New ADODB.Connection
On Error Resume Next
cn.Open "DRIVER={Microsoft Excel Driver (*.xls)};DriverId=790;ReadOnly=True;" & _
"DBQ=" & strSourceFile & ";"
' DriverId=790: Excel 97/2000
' DriverId=22: Excel 5/95
' DriverId=278: Excel 4
' DriverId=534: Excel 3
On Error GoTo 0
If cn Is Nothing Then
MsgBox "Can't find the file!", vbExclamation, ThisWorkbook.Name
Exit Sub
End If

' open a recordset
Set rs = New ADODB.Recordset
On Error Resume Next
rs.Open strSQL, cn, adOpenForwardOnly, adLockReadOnly, adCmdText
' rs.Open "SELECT * FROM [SheetName$]", _
cn, adOpenForwardOnly, adLockReadOnly, adCmdText
' rs.Open "SELECT * FROM [SheetName$]", _
cn, adOpenStatic, adLockOptimistic, adCmdText
' rs.Open "SELECT * FROM [SheetName$] WHERE [Field Name] LIKE 'A%'", _
cn, adOpenStatic, adLockOptimistic, adCmdText
' rs.Open "SELECT * FROM [SheetName$] WHERE [Field Name] LIKE 'A%' ORDER BY [Field Name]", _
cn, adOpenStatic, adLockOptimistic, adCmdText

' optional ways of retrieving a recordset
' Set rs = cn.Execute("[A1:Z1000]") ' first worksheet
' Set rs = cn.Execute("[DefinedRangeName]") ' any worksheet

On Error GoTo 0
If rs Is Nothing Then
MsgBox "Can't open the file!", vbExclamation, ThisWorkbook.Name
cn.Close
Set cn = Nothing
Exit Sub
End If

RS2WS rs, TargetCell
' TargetCell.CopyFromRecordset rs ' optional approach for Excel 2000 or later

If rs.State = adStateOpen Then
rs.Close
End If
Set rs = Nothing
cn.Close
Set cn = Nothing
End Sub

```

- You need to  **save the code**  by selecting it and then  **close the window**.

![](https://geekexcel.com/wp-content/uploads/2021/09/Save-the-Code-97-1024x857.png)<figcaption>Save the Code</figcaption>

- Again, you have to go to the  **Excel Spreadsheet** , and click on the  **Developer Tab**.
- You need to choose the  **Macros option**  in the Code section.

![Choose Macro option](https://geekexcel.com/wp-content/uploads/2021/09/Choose-Macro-option-11-1024x184.png)<figcaption>Choose Macro option</figcaption>

- Now, you have to make sure that your  **macro name is selected**  and click the  **Run ** button.
- Finally, you will  **replace SheetName with the worksheet name**  you want to  **retrieve data**  from.

## Wind-Up

We hope that this tutorial gives you guidelines on how to **use a closed workbook as a database (ADO) using VBA **in Excel. Please leave a comment in case of any  **queries,**  and don’t forget to mention your valuable  **suggestions**  as well. Thank you so much for Visiting Our Site!! Continue learning on **[Geek Excel](https://geekexcel.com/)!! **Read more on [**Excel Formulas**](https://geekexcel.com/excel-formula/) **!!**

**Read Next:**

- **[How to Connect Excel to Access Database using Excel VBA?](https://geekexcel.com/how-to-connect-excel-to-access-database-using-excel-vba/)**
- **[How To Pull data from a closed workbook in Excel VBA?](https://geekexcel.com/how-to-pull-data-from-a-closed-workbook-in-excel-vba/)**
- **[Create a New Workbook in Excel 365 Using Shortcut](https://geekexcel.com/simple-shortcut-to-create-a-new-workbook-in-excel-365/)**
- **[Save Workbook in Microsoft Excel 365 – Easy Shortcut](https://geekexcel.com/shortcut-to-save-workbook-in-excel-365/)**
- **[Close Current Workbook in Microsoft Excel 365 – Useful Shortcut](https://geekexcel.com/shortcut-to-close-current-workbook-in-microsoft-excel-365/)**