아래 절차에 따라 ADO를 사용하여 닫힌 통합 문서에서 레코드 집합을 검색하고 데이터를 읽고 쓸 수 있습니다.

다음과 같이 프로 시저를 호출하십시오.

GetWorksheetData “C : FoldernameFilename.xls”, “SELECT * FROM [SheetName $];”, ThisWorkbook.Worksheets (1) .Range ( “A3”)

SheetName을 데이터를 검색 할 워크 시트 이름으로 바꿉니다.

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 “파일을 찾을 수 없습니다! “, vbExclamation, ThisWorkbook.Name Exit Sub End If ‘레코드 세트 열기 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 [필드 이름] LIKE ‘A % ‘ “, _ cn, adOpenStatic, adLockOptimistic, adCmdText’rs.Open”SELECT FROM [SheetName $] WHERE [필드 이름] LIKE ‘A %’ORDER BY [필드 이름] “, _ cn, adOpenStatic, adLockOptimistic, adCmdText ‘선택 사항 레코드 세트를 검색하는 방법 ‘Set rs = cn.Execute ( “[A1 : Z1000]”)’first workstation ‘Set rs = cn.Execute ( “[DefinedRangeName]”)’오류 발생시 모든 워크 시트 이동 0 If rs Is Nothing Then MsgBox “열 수 없음 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 매크로 예제에서는 VBA 프로젝트가 ADO 개체 라이브러리에 대한 참조를 추가했다고 가정합니다.

도구, 참조 메뉴를 선택하고 Microsoft ActiveX 데이터 개체 x.x 개체 라이브러리를 선택하여 VBE 내에서이 작업을 수행 할 수 있습니다.