nguyendangkhoi
Thành viên chính thức
- Tham gia
- 18/2/09
- Bài viết
- 59
- Được thích
- 5
PHP:
Dim NewSh
'
Dim SourceFile As String
Const SourceSheet As String = "Sheet1"
Const SourceRange As String = "a2:b22"
Dim WBpath As String
Public sFileName As String
Dim SourceWB As Workbook, TgtWb As Workbook
Private Sub GetDataFromClosedWorkbook(SourceFile As String, SourceRange As String)
Dim dbConnection As Object, rs As Object
Dim dbConnectionString As String
Set dbConnection = CreateObject("ADODB.Connection")
Set rst = CreateObject("ADODB.recordset")
Dim TargetCell As Range, i As Integer
dbConnectionString = "DRIVER={Microsoft Excel Driver (*.xls)};" & _
"ReadOnly=1;DBQ=" & SourceFile
On Error GoTo InvalidInput
dbConnection.Open dbConnectionString
Set rs = dbConnection.Execute("[" & SourceSheet & "$" & SourceRange & "]")
Set TargetCell = NewSh.Cells(1, 1)
TargetCell.CopyFromRecordset rs
rs.Close
dbConnection.Close
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
End Sub
PHP:
Private Sub CommandButton1_Click() 'ADO
Dim MyArr
Dim Start As Double, Finnish As Double
'WBpath = ThisWorkbook.Path
WBpath = ThisWorkbook.Path
SourceFile = WBpath & "\" & "SourceWbName.xls"
'SourceFile = fg
Start = Timer
If SheetExists("DataSheet") = True Then
Application.DisplayAlerts = False
Sheets("DataSheet").Delete
Application.DisplayAlerts = True
End If
Set NewSh = Sheets.Add(After:=Sheets(Sheets.Count))
NewSh.Name = "DataSheet"
Call GetDataFromClosedWorkbook(SourceFile, SourceRange)
MyArr = Sheets("DataSheet").Range("a2:b22").Value
Application.DisplayAlerts = False
Application.DisplayAlerts = True
Finnish = Timer
End Sub