닫힌 통합 문서에서 많은 데이터를 가져 오려면 ADO와 아래 매크로를 사용하여이 작업을 수행 할 수 있습니다.

닫힌 통합 문서의 첫 번째 워크 시트가 아닌 다른 워크 시트에서 데이터를 검색하려면 사용자 정의 명명 된 범위를 참조해야합니다. 아래 매크로는 다음과 같이 사용할 수 있습니다 (Excel 2000 이상에서) :

GetDataFromClosedWorkbook "C:\FolderName\WorkbookName.xls", "A1:B21", ActiveCell, False

GetDataFromClosedWorkbook "C:\FolderName\WorkbookName.xls", "MyDataRange", Range ("B3"), True

Sub GetDataFromClosedWorkbook(SourceFile As String, SourceRange As String, _

TargetRange As Range, IncludeFieldNames As Boolean)

' requires a reference to the Microsoft ActiveX Data Objects library

' if SourceRange is a range reference:

'   this will return data from the first worksheet in SourceFile

' if SourceRange is a defined name reference:

'   this will return data from any worksheet in SourceFile

' SourceRange must include the range headers

'

Dim dbConnection As ADODB.Connection, rs As ADODB.Recordset

Dim dbConnectionString As String

Dim TargetCell As Range, i As Integer

dbConnectionString = "DRIVER={Microsoft Excel Driver (*.xls)};" & _

"ReadOnly=1;DBQ=" & SourceFile

Set dbConnection = New ADODB.Connection

On Error GoTo InvalidInput

dbConnection.Open dbConnectionString ' open the database connection

Set rs = dbConnection.Execute("[" & SourceRange & "]")

Set TargetCell = TargetRange.Cells(1, 1)

If IncludeFieldNames Then

For i = 0 To rs.Fields.Count - 1

TargetCell.Offset(0, i).Formula = rs.Fields(i).Name

Next i

Set TargetCell = TargetCell.Offset(1, 0)

End If

TargetCell.CopyFromRecordset rs

rs.Close

dbConnection.Close ' close the database connection

Set TargetCell = Nothing

Set rs = Nothing

Set dbConnection = Nothing

On Error GoTo 0

Exit Sub

InvalidInput:

MsgBox "The source file or source range is invalid!", _

vbExclamation, "Get data from closed workbook"

End Sub

CopyFromRecordSet-method를 사용하지 않는 또 다른 방법 아래 매크로를 사용하면 가져 오기를 수행하고 RecordSet에서 반환 된 결과를 더 잘 제어 할 수 있습니다.

Sub TestReadDataFromWorkbook()

' fills data from a closed workbook in at the active cell

Dim tArray As Variant, r As Long, c As Long

tArray = ReadDataFromWorkbook("C:\FolderName\SourceWbName.xls", "A1:B21")

' without transposing

'    For r = LBound(tArray, 2) To UBound(tArray, 2)

'        For c = LBound(tArray, 1) To UBound(tArray, 1)

'            ActiveCell.Offset(r, c).Formula = tArray(c, r)

'        Next c

'    Next r

' with transposing

tArray = Application.WorksheetFunction.Transpose(tArray)

For r = LBound(tArray, 1) To UBound(tArray, 1)

For c = LBound(tArray, 2) To UBound(tArray, 2)

ActiveCell.Offset(r - 1, c - 1).Formula = tArray(r, c)

Next c

Next r

End Sub

Private Function ReadDataFromWorkbook(SourceFile As String, SourceRange As String) As Variant

' requires a reference to the Microsoft ActiveX Data Objects library

' if SourceRange is a range reference:

'   this function can only return data from the first worksheet in SourceFile

' if SourceRange is a defined name reference:

'   this function can return data from any worksheet in SourceFile

' SourceRange must include the range headers

' examples:

' varRecordSetData = ReadDataFromWorkbook("C:\FolderName\SourceWbName.xls", "A1:A21")

' varRecordSetData = ReadDataFromWorkbook("C:\FolderName\SourceWbName.xls", "A1:B21")

' varRecordSetData = ReadDataFromWorkbook("C:\FolderName\SourceWbName.xls", "DefinedRangeName")

Dim dbConnection As ADODB.Connection, rs As ADODB.Recordset

Dim dbConnectionString As String

dbConnectionString = "DRIVER={Microsoft Excel Driver (*.xls)};ReadOnly=1;DBQ=" & SourceFile

Set dbConnection = New ADODB.Connection

On Error GoTo InvalidInput

dbConnection.Open dbConnectionString ' open the database connection

Set rs = dbConnection.Execute("[" & SourceRange & "]")

On Error GoTo 0

ReadDataFromWorkbook = rs.GetRows ' returns a two dim array with all records in rs

rs.Close

dbConnection.Close ' close the database connection

Set rs = Nothing

Set dbConnection = Nothing

On Error GoTo 0

Exit Function

InvalidInput:

MsgBox "The source file or source range is invalid!", vbExclamation, "Get data from closed workbook"

Set rs = Nothing

Set dbConnection = Nothing

End Function

매크로 예제에서는 VBA 프로젝트가 ADO 개체 라이브러리에 대한 참조를 추가했다고 가정합니다.

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

데이터 가져 오기 또는 내보내기를 위해 ADO와 DAO 중에서 선택할 수있는 경우 ADO를 사용하십시오.