1

有没有人知道如何控制 MS Access to 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”字段对所有记录进行分组。

有任何想法吗?

4

2 回答 2

2

回复:行顺序

当然,您可以在 Excel 中对行进行排序,但如果您想在 Access 中执行此操作,您将创建一个执行排序的 Select 查询,保存它,然后导出查询而不是表。

回复:抑制重复值

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

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

(参考:这里

编辑:响应下面的评论,示例 Excel VBA 代码以应用格式(由 Excel 中的“记录宏”捕获):

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
于 2013-03-12T10:42:31.907 回答
1

尝试这样的事情。

先介绍几个有用的功能:

1) 基于 SELECT 语句(通过参数传递)从 Access 获取记录集。

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)调用记录集:

选项显式

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 打印它。

于 2013-03-12T10:20:59.847 回答