我有一些报告,我每天必须以特定方式将其导入数据库MS Access。问题是,报告的布局方式使得这样做很困难。
Program: Name A
Date | Description | Other | Stuff
Date | Description | Other | Stuff
Program: Name B
Date | Description | Other | Stuff
Date | Description | Other | Stuff
Program: Name C
Date | Description | Other | Stuff
Date | Description | Other | Stuff
基本上,我希望它能做的就是这样。
Program: Name A | Date | Description | Other | Stuff
Program: Name A | Date | Description | Other | Stuff
Program: Name B | Date | Description | Other | Stuff
Program: Name B | Date | Description | Other | Stuff
Program: Name C | Date | Description | Other | Stuff
Program: Name C | Date | Description | Other | Stuff
我玩过几种不同的解决方案,但到目前为止还没有完成任务。
感谢您的帮助!
答案 0 :(得分:2)
类似的东西:
Dim xl As New Excel.Application
Dim ws As Excel.Worksheet
Dim rng As Excel.Range
Dim rs As DAO.Recordset
Dim db As Database
Set db = CurrentDb
''Run once
''s = "create table reporttable (id counter primary key, program text, " _
'' & "pdate text, [description] text, other text, stuff text)"
''db.Execute s, dbFailOnError
Set rs = db.OpenRecordset("ReportTable")
xl.Visible = True ''dev
Set ws = xl.Workbooks.Open("z:\docs\report.xlsx").Sheets("Sheet1")
Set rng = ws.UsedRange
For i = 1 To rng.Rows.Count
If rng.Cells(i, 1) Like "Program*" Then
progdat = rng.Cells(i, 1)
Else
rs.AddNew
rs!program = progdat
rs!pdate = rng.Cells(i, 1) ''Text, because you cannot trust reports
rs!Description = rng.Cells(i, 2)
rs!Other = rng.Cells(i, 3)
rs!Stuff = rng.Cells(i, 4)
rs.Update
End If
Next
Set rs = Nothing
Set rng = Nothing
ws.Parent.Close
Set ws = Nothing
xl.Quit
Set xl = Nothing