2011年3月3日 星期四

如何用VBA 將Excel A資料匯到Excel B

'此處需要1個CommandButton

Option Explicit
Dim cn As New ADODB.Connection
Dim cn2 As New ADODB.Connection
Dim xlsFileName As String
Dim xlsFileName2 As String

Private Sub Command1_Click()
Dim SheetName As String
SheetName = "Sheet1"
cn.Execute "Select * Into [Excel 8.0;DATABASE=" & xlsFileName2 & "].[Sheet1] From (Select * from [" & SheetName & "$] " '將資料匯到 xlsFileName2
End Sub

Private Sub Form_Load()
xlsFileName = "C:\temp\123.xls"
xlsFileName2 = "C:\temp\456.xls"
On Error Resume Next
Kill xlsFileName2
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & xlsFileName & ";" & _
"Extended Properties=""Excel 8.0;IMEX=1;"";" & _
"Persist Security Info=False"

End Sub

沒有留言:

張貼留言