使用VBA将选定列从Access表导入Excel
我正试图从访问表中导入选定的数据。 这个表有4列,我只想要列2和3.在Excel中,并希望他们列出的顺序:列3,列2(相反,他们是如何在Access中)。 此外,我想根据Excel Spread工作表中引用的date(我在代码中称为RpDate)select行(从Access表)。 在Access中,“Date”是第一列。 我需要一些帮助。 谢谢。
Sub ADOImportFromAccessTable() Dim DBFullName As String Dim TableName As String Dim TargetRange As Range Dim RpDate As Range DBFullName = "C:\Documents\Database.mdb" TableName = "DataTable" TargetRange = Range("C5") RpDate = Range("B2").Value Dim cn As ADODB.Connection, rs As ADODB.Recordset, intColIndex As Integer Set TargetRange = TargetRange.Cells(1, 1) ' open the database Set cn = New ADODB.Connection cn.Open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & _ "C:\Documents\Database.mdb" & ";" Set rs = New ADODB.Recordset With rs ' open the recordset .Open TableName, cn, adOpenStatic, adLockOptimistic, adCmdTable ' all records .Open "SELECT * FROM " & TableName & _ " WHERE [Date] = RpDate, cn, , , adCmdText" ' filter rows based on date rs.Open , TargetRange End With rs.Close Set rs = Nothing cn.Close Set cn = Nothing End Sub
尝试这个
Sub ADOImportFromAccessTable() Dim DBFullName As String Dim TableName As String Dim TargetRange As Range Dim RpDate As Range DBFullName = "C:\Documents\Database.mdb" TableName = "DataTable" Set TargetRange = Range("C5") RpDate = Range("B2").Value Dim cn As ADODB.Connection, rs As ADODB.Recordset, intColIndex As Integer Set TargetRange = TargetRange.Cells(1, 1) ' open the database Set cn = New ADODB.Connection cn.Open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & _ "C:\Documents\Database.mdb" & ";" Set rs = New ADODB.Recordset With rs ' open the recordset .Open TableName, cn, adOpenStatic, adLockOptimistic, adCmdTable ' all records .Open "SELECT Time, Tank FROM " & TableName & " WHERE [Date] = " & RpDate & " ORDER BY Tank, Time", cn, , , adCmdText ' filter rows based on date End With rs.Close Set rs = Nothing cn.Close Set cn = Nothing End Sub
这不是SQL注入的教训,而是一个开始
- 如何创buildExcel公式以显示在“ – ”分隔时间(“hh-hh”)之间有多less小时
- python openpyxl load_workbook错误:TypeError(NoneType not Iterable)和ValueError(Max。Value is 180)
- 从Access运行VBA以改变Excelfile
- 在Excel中交替行颜色/数字 – VBA
- 无法closuresexcel进程的Application.Quit和令人不安的进程更新xls文件
- 如何更改ACE TypeGuessRows Access 2007的registry值,以便在使用VBA从Excel文件导入时正确地确定字段数据types
- 在Excel中将一个字段从访问导出到特定的单元格
- 为什么我的ADO Access连接给出的结果与在Access应用程序中运行的查询不同?
- 在VBA中复制Excel工作簿variables
- 运行Access-VBA脚本时,新计算机永久冻结
- 从Access的Excel格式化每隔一段时间(对象variables/块未设置)