|
楼主 |
发表于 2016-12-14 17:53
|
显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
Sub DataImport()
On Error Resume Next
'Open the original data
Dim tmpBook As Workbook, tmpSheet As Worksheet
'Set tmpBook = Excel.ActiveWorkbook
'Set tmpSheet = tmpBook.Sheets(1)
Dim openFilePN As String
openFilePN = Excel.Application.GetOpenFilename("All files (*.*),*.*", , "Select the workbook!")
Set tmpBook = Excel.Workbooks.Open(openFilePN)
'Set tmpSheet = tmpBook.Sheets(1)
'Set tmpBook = Excel.ActiveWorkbook
Set tmpSheet = tmpBook.Sheets(1)
Dim i, j, k, n, m As Integer
Dim cnn As ADODB.Connection
Dim rs As ADODB.Recordset
Set cnn = New ADODB.Connection
Set rs = New ADODB.Recordset
Dim SQL As String
Dim myPath As String
Dim myTable As String
myPath = "C:\Users\0000135401\Desktop\TEGE yoursony" & "GE DB.accdb"
myTable = "原紙データ"
SQL = "select * from" & myTable
cnn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & myPath & ";Jet OLEDB:Database Password=ge" 'Open DB
rs.Open SQL, cnn, adOpenKeyset, adLockOptimistic
m = rs.RecordCount
For i = 2 To tmpSheet.Range("B65536").End(xlUp).Row
rs.AddNew
rs.Fields(0).Value = tmpSheet.Cells(i, 9).Value '大連処理開始日
rs.Fields(1).Value = tmpSheet.Cells(i, 11).Value '会社名
rs.Fields(2).Value = tmpSheet.Cells(i, 12).Value '事前申請(伝票区分)
rs.Fields(3).Value = tmpSheet.Cells(i, 8).Value 'レポートID
rs.Fields(4).Value = TD '入力担当
rs.Fields(6).Value = "未審査" '一次審査結果
rs.Fields(9).Value = "未審査" '二次審査結果
rs.Fields(14).Value = tmpSheet.Cells(i, 13).Value '領収書が必須
rs.Fields(15).Value = tmpSheet.Cells(i, 51).Value '入力数
rs.Fields(16).Value = tmpSheet.Cells(i, 2).Value '申請者提出日
rs.Fields(17).Value = tmpSheet.Cells(i, 56).Value '従業員 ID
rs.Fields(18).Value = tmpSheet.Cells(i, 7).Value 'レポート総計
rs.Fields(19).Value = tmpSheet.Cells(i, 10).Value '担当経理
rs.Update
Next
tmpSheet.Range("AE1:AG65536").ClearContents
tmpBook.Save
tmpBook.Close
m = rs.RecordCount - m
MsgBox m & "件 import to Acess sucessfully"
rs.Close
cnn.Close
Set rs = Nothing
Set cnn = Nothing
End Sub |
|