透视全文而不是计数-Excel

时间:2019-01-16 06:03:58

标签: excel vba pivot

我正在尝试确定最有效的方式来将sheet1上的中央表中的特定行显示为sheet2上的文本。

我已经设置了一个表,其中包含许多事件,这些事件一直在被多个人使用。该工作表充当中央数据库,并与多个实时使用该工作的人共享。

我想在sheet2上提供一个表,该表允许其他用户查看中央数据库中的特定事件。因此,我需要将特定的值从sheet1导出到sheet2。我知道可以使用过滤器轻松完成此操作,但是sheet1上的表一直在使用,并且不会被破坏。

我不能仅对sheet1表进行排序或过滤,因为它需要一直被其他方使用

我真的只需要查看sheet1中上个月的特定值。我已经编写了根据输入到Sheet1上指定列中的特定值导出所有行的代码。但是由于文件大小,Excel经常崩溃。

然后,我认为数据透视表可能会更容易,并且我不必使用VBA。是否可以将特定的行作为文本显示出来,可以按日期将其分组,例如一个月?

例如,如果我想查看上个月['A's']中所有['X's']Column B的全文,它将类似于以下内容:

中央数据库表Sheet1

   A   B  C   D
0 11/1 A Big Dog
1 10/1 X  1   2 
2 11/1 Y  Y   Y
3 1/2  A Big Cat
4 1/2  X  3   4 
5 1/2  Y  Y   Y

输出表Sheet2

   A  B  C   D
1 1/2 A Big Cat
2 1/2 X  3   4

2 个答案:

答案 0 :(得分:3)

就像其他人在评论中提到的那样,将SQL与ADODB一起使用可能比使用数据透视表更好。我还建议您将数据(Sheet1)与表示层(Excel)分开。例如。将您的数据存储在实际数据库中,例如Access,SQL Server等。

但是,当您正在寻找止损时,我想我可以给您一种可以暂时满足需求的方法。该代码已注释,但随时可以提出问题。您需要添加对Microsoft Active X Data Object 2.8 or greater的引用才能使此工作正常。 How to add a reference?


早期绑定方法

Option Explicit
Public Sub DisplayView(StartDate As Date, EndDate As Date)
    'Add a reference to Microsoft Active X Data Object 2.8 or greater
    Dim dbConnection  As ADODB.Connection
    Dim dbRecordset   As ADODB.Recordset
    Dim dbCommand     As ADODB.Command
    Dim OutputSheet   As Excel.Worksheet
    Dim dbField       As Variant
    Dim fieldCounter  As Long

    Set dbConnection = New ADODB.Connection
    Set dbRecordset = New ADODB.Recordset
    Set dbCommand = New ADODB.Command
    Set OutputSheet = ThisWorkbook.Worksheets("Sheet2")

    'Do a quick check to determine the correct connection string
    'if one of these don't work, have a look here --> https://www.connectionstrings.com/excel/
    If Left$(ThisWorkbook.FullName, 4) = "xlsm" Then
        dbConnection.connectionstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & _
        ThisWorkbook.FullName & ";Extended Properties='Excel 12.0 Macro;HDR=YES';"
    Else
        dbConnection.connectionstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & _
        ThisWorkbook.FullName & ";Extended Properties='Excel 12.0;HDR=YES';"
    End If

    'Open the connection and parameterize the query
    dbConnection.Open
    With dbCommand
        .ActiveConnection = dbConnection
        .CommandType = adCmdText
        'A in B in the text below are the field names in your Sheet 1
        'I wasn't sure what the names of the fields are so I named them as they appeared
        'That being Column A is called A, Column B is called B etc
        .CommandText = "Select * from [Sheet1$] where B in ('A','X') and A >= @StartDate and A < @EndDate"
        .Parameters.Append .CreateParameter("@StartDate", adDate, adParamInput, , StartDate)
        .Parameters.Append .CreateParameter("@EndDate", adDate, adParamInput, , EndDate)
        Set dbRecordset = .Execute
    End With

    'Clear the Output Sheet
    OutputSheet.Cells.Clear

    'Add Headers to output
    For Each dbField In dbRecordset.Fields
        fieldCounter = fieldCounter + 1
        OutputSheet.Cells(1, fieldCounter).Value2 = dbField.Name
    Next

    'Dump the found records
    OutputSheet.Range("A2").CopyFromRecordset dbRecordset
    If dbConnection.State = adStateOpen Then dbConnection.Close
End Sub

'Run from here
Public Sub ExampleRunner()
    'Supply the dates you want to filter for
    DisplayView #1/1/2019#, #1/20/2019#
End Sub

根据要求,这是后期绑定方法,不需要明确引用Microsoft Active X Data Object

Option Explicit
Private Const adCmdText As Long = 1
Private Const adDate As Long = 7
Private Const adParamInput As Long = 1

Public Sub DisplayView(StartDate As Date, EndDate As Date)
    'Add a reference to Microsoft Active X Data Object 2.8 or greater
    Dim dbField       As Variant
    Dim fieldCounter  As Long
    Dim dbConnection  As Object
    Dim dbRecordset   As Object
    Dim dbCommand     As Object
    Dim OutputSheet   As Excel.Worksheet

    Set dbConnection = CreateObject("ADODB.Connection")
    Set dbRecordset = CreateObject("ADODB.Recordset")
    Set dbCommand = CreateObject("ADODB.Command")

    Set OutputSheet = ThisWorkbook.Worksheets("Sheet2")

    'Do a quick check to determine the correct connection string
    'if one of these don't work, have a look here --> https://www.connectionstrings.com/excel/
    If Left$(ThisWorkbook.FullName, 4) = "xlsm" Then
        dbConnection.connectionstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & _
        ThisWorkbook.FullName & ";Extended Properties='Excel 12.0 Macro;HDR=YES';"
    Else
        dbConnection.connectionstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & _
        ThisWorkbook.FullName & ";Extended Properties='Excel 12.0;HDR=YES';"
    End If

    'Open the connection and parameterize the query
    dbConnection.Open
    With dbCommand
        .ActiveConnection = dbConnection
        .CommandType = adCmdText
        'A in B in the text below are the field names in your Sheet 1
        'I wasn't sure what the names of the fields are so I named them as they appeared
        'That being Column A is called A, Column B is called B etc
        .CommandText = "Select * from [Sheet1$] where B in ('A','X') and A >= @StartDate and A < @EndDate"
        .Parameters.Append .CreateParameter("@StartDate", adDate, adParamInput, , StartDate)
        .Parameters.Append .CreateParameter("@EndDate", adDate, adParamInput, , EndDate)
        Set dbRecordset = .Execute
    End With

    'Clear the Output Sheet
    OutputSheet.Cells.Clear

    'Add Headers to output
    For Each dbField In dbRecordset.Fields
        fieldCounter = fieldCounter + 1
        OutputSheet.Cells(1, fieldCounter).Value2 = dbField.Name
    Next

    'Dump the found records
    OutputSheet.Range("A2").CopyFromRecordset dbRecordset
    If dbConnection.State = adStateOpen Then dbConnection.Close
End Sub

'Run from here
Public Sub ExampleRunner()
    'Supply the dates you want to filter for
    DisplayView #1/1/2019#, #1/20/2019#
End Sub

答案 1 :(得分:0)

这是下面提到的Power Query的结果的一些屏幕截图。我选择了(在Excel 2003中)数据->导入外部数据->新建数据库查询 然后,我选择“ Excel文件”并添加了我想要的数据。确保在查询选项中选择“不为空”。然后,我添加了自动排序功能。

ScreenShot1

ScreenShot2

您可以在Sheet2 Excel工作簿中使用Power QueryAnother link here。这样,您可以在需要时更新数据。然后,使用SQL查询您需要的内容。

使用非常简单,不需要任何编码(除非您想使用SQL)。

可以完成此操作,然后在其他工作簿中进行过滤和排序。

@ ryan-wildry的帖子非常棒(SQL的命令文本也是如此),但是如果您不想使用vba或数据库,则可以使用它(以及他的SQL文本)

一个例子是:   SELECT * FROM [Sheet1 $] WHERE Column2 ='X';

唯一的问题是,如果同一列中混合了数据类型。从第1行和第4行可以看出(由于值的类型不同,它们无法出现在查询中。(这是使用Excel 2003,因此如果您使用的是更新版本,则可以检查)。

我一直在做一些研究,发现Excel的Power Query可以处理混合数据类型,因此,如果您使用Power Query,则应进行设置。