Sub LoadDataFromWorkbook03()
On Error Resume Next
On Error GoTo 0
Dim tArray As Variant, r As Long, c As Long
n = Cells(Rows.Count, "A").End(xlUp).Row + 1
Cells(n, "A").Select
tArray = ReadDataFromWorkbook("D:\Operations\Performance\Data\Sales\MTD Sales PC03", "A2:P15000")
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
Application.Calculation = xlAutomatic
On Error Resume Next
On Error GoTo 0
End Sub
Private Function ReadDataFromWorkbook(SourceFile As String, SourceRange As String) As Variant
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
ASKER
Sub LoadDataFromWorkbook03()
On Error Resume Next
On Error GoTo 0
Dim myws As Worksheet
mysheet = "detail" 'You specify your worksheet here
Set myws = Application.ActiveWorkbook.Sheets(mysheet)
Dim tArray As Variant, r As Long, c As Long
n = Cells(Rows.Count, "A").End(xlUp).Row + 1
Cells(n, "A").Select
tArray = ReadDataFromWorkbook("D:\Operations\Performance\Data\Sales\MTD Sales PC03", "A2:P15000")
For r = LBound(tArray, 2) To UBound(tArray, 2)
For c = LBound(tArray, 1) To UBound(tArray, 1)
myws.Range("A1").Activate 'You specify where you want the active cell to be
ActiveCell.Offset(r, c).Formula = tArray(c, r)
Next c
Next r
Application.Calculation = xlAutomatic
On Error Resume Next
On Error GoTo 0
End Sub
ASKER
Sub LoadDataFromWorkbook03()
On Error Resume Next
On Error GoTo 0
Dim myws As Worksheet
mysheet = "DETAIL" 'You specify your worksheet name, need to insert exact name here
Set myws = Application.ActiveWorkbook.Sheets(mysheet)
Dim tArray As Variant, r As Long, c As Long
n = Cells(Rows.Count, "A").End(xlUp).Row + 1
Cells(n, "A").Select
myws.Select
myws.Activate
myws.Range("A2").Select
myws.Range("A2").Activate 'You specify where you want the active cell to be
tArray = ReadDataFromWorkbook("D:\Operations\Performance\Data\Sales\MTD Sales PC03", "A2:P15000")
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
Application.Calculation = xlAutomatic
On Error Resume Next
On Error GoTo 0
End Sub
ASKER
ASKER
Microsoft Excel topics include formulas, formatting, VBA macros and user-defined functions, and everything else related to the spreadsheet user interface, including error messages.
TRUSTED BY
Open in new window