如何控制从MS Access到Excel的输出

有没有人知道如何控制从MS Access中的表输出的布局到Excel。

例如:

我的MS Access表看起来像这样

-------------------------------------------------- |ID | Field 2 | Field 3| Field 4 | ETC -------------------------------------------------- |A | 1 | 2 | 3 | |A | 4 | 5 | 6 | |B | 7 | 8 | 9 | |C | 10 | 11 | 12 | |A | 13 | 14 | 15 | 

虽然我想将数据输出到excel中,例如:

 -------------------------------------------------- |ID | Field 2 | Field 3| Field 4 | ETC -------------------------------------------------- |A | 1 | 2 | 3 | | | 4 | 5 | 6 | | | 13 | 14 | 15 | |B | 7 | 8 | 9 | |C | 10 | 11 | 12 | 

所以我可以通过“ID”字段对所有logging进行分组。

有任何想法吗?

Re:行顺序

当然,您可以在Excel中对行进行sorting,但是如果您想在Access中执行此操作,则可以创build一个Select查询来进行sorting,保存,然后导出查询而不是表格。

Re:抑制重复的值

您可能不想完全忽略它们,但可以使用“条件格式”function将它们隐藏在Excel中。 在你的情况下,你会的

  • selectA列中的ID值(从第2行开始)
  • 调用条件格式
  • select“使用公式确定要格式化的单元格”
  • 将公式设置为= A2 = A1
  • 将字体颜色设置为与背景颜色相同

(参考: 这里 )

编辑:作为对以下评论的回应,示例应用格式的Excel VBA代码(由Excel中的“loggingmacros”捕获):

 Range("A2:A6").Select Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=A2=A1" Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority With Selection.FormatConditions(1).Font .ThemeColor = xlThemeColorDark1 .TintAndShade = 0 End With Selection.FormatConditions(1).StopIfTrue = False 

尝试这样的事情。

首先几个有用的function:

1)基于SELECT语句(通过parameter passing)从Access获取Recordset。

 Option Explicit Public Function Rst_From_Access(sSQL_Select As String) As ADODB.Recordset Dim oConn As ADODB.Connection Dim oRst As ADODB.Recordset Dim sPath_DB As String Dim sFile_DB As String Dim sConn As String 'Instantiate the ADO-objects. Set oConn = New ADODB.Connection Set oRst = New ADODB.Recordset 'Set Path and File sPath_DB = ThisWorkbook.Names("PARAM_PATH_DB").RefersToRange.value sFile_DB = ThisWorkbook.Names("PARAM_FILE_DB").RefersToRange.value 'Create the connectionstring. sConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & sPath_DB & sFile_DB & ";" With oConn .Open (sConn) 'Open the connection. .CursorLocation = adUseClient 'Necessary to disconnect the recordset. End With With oRst .Open sSQL_Select, oConn 'Create the recordset. Set .ActiveConnection = Nothing 'Disconnect the recordset. End With Set Rst_From_Access = oRst End Function 

2)ADO连接:

 Public Sub open_ADODB_Connection() Dim oConn As ADODB.Connection Dim sPath_DB As String Dim sFile_DB As String Dim sConn As String 'Instantiate the ADO-objects. Set oConn = New ADODB.Connection 'Set Path and File sPath_DB = ThisWorkbook.Names("PARAM_PATH_DB").RefersToRange.value sFile_DB = ThisWorkbook.Names("PARAM_FILE_DB").RefersToRange.value 'Create the connectionstring. sConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & sPath_DB & sFile_DB & ";" With oConn .Open (sConn) 'Open the connection. .CursorLocation = adUseClient 'Necessary to disconnect the recordset. End With End Sub 

3)调用logging集:

选项显式

 Sub Connect_To_DB() Dim oSheet As Excel.Worksheet Dim sSQL_Select As String Dim oRst As ADODB.Recordset Dim iMax_Col As Integer Dim lMax_Row as long Set oSheet = ThisWorkbook.Sheets("Main") 'Get recordset sSQL_Select = "SELECT * FROM T_TABLE ORDER BY ID;" Set oRst = Rst_From_Access(sSQL_Select) iMax_Col = oRst.Fields.Count oRst.MoveLast iMax_Row = oRst.RecordCount With oSheet .Range(.Cells(1, 1), .Cells(iMax_Row, _ lMax_Col)).CopyFromRecordset oRst End With End Sub 

当然,你会有一个重复的ID,会导致一个问题? 一般来说,我认为最好保留每一行的所有数据。 如果是打印目的,那么我会立即从Access打印它。