【问题标题】:how to get the filter dropdown list in excel如何在excel中获取过滤器下拉列表
【发布时间】:2022-01-26 22:31:37
【问题描述】:

我正在编写一个程序,它可以读取数据的电子表格(零件目录),并将数据放在不同电子表格的可用列中。零件目录中有 750,000 行。我想在程序开始时使用每个唯一值的下拉列表。 在电子表格中,您可以在列上放置过滤器。如果您单击该过滤器的下拉列表,它会提供唯一值列表(见下图)。如何访问该列表?

过滤列表

我尝试通过查看每个值(检查它是否已经在列表中)并添加它来重新创建列表。但它现在已经运行了 4 个多小时,仍然没有完成。我需要更快的东西。

另一种选择是手动创建静态列表。我不介意这一点,但就像我从数据而不是静态的假设列表中获取信息一样高枕无忧。

你怎么看?

Public Class Catalog
    Public XlApp As Excel.Application
    Public Xlbook As Excel.Workbook
    Public XlSh As Excel.Worksheet
    Dim WrdApp As Word.Application
    Dim WrdDoc As Word.Document
    Public SpreadsheetHeaderRowIndex As Integer
    Public TypeSelectionList As List(Of String)


    Private Sub Catalog_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        OpenSpreadsheet()

        GetFilterList()

        Dim TypeForm As New Type_Selection
        TypeForm.ShowDialog()

        ApplyFiltertoSpreadsheet()

    End Sub

#Region " OpenSpreadsheet | Private Sub"
    Private Sub OpenSpreadsheet()

        Dim Directry As String = "file location - part catalog\"
        Dim FileName As String = "part catalog records with usage by plants v2.xlsx"

        Try
            ' If excel is already open, get the application.
            XlApp = SysInter.Marshal.GetActiveObject("Excel.Application")
            DBWrite.WriteLine("Catalog | OpenSpreadsheet | Found the excel application.")

            ' Look through the open workbooks for the one being opened and get it.
            For Each XlObject As Excel.Workbook In XlApp.Application.Workbooks : If XlObject.Name = FileName Then : Xlbook = XlObject : DBWrite.WriteLine("Catalog | OpenSpreadsheet | Found spreadsheet: " & FileName) : Exit For : End If : Next

            ' If the workbook being opened isn't already opened, then open it.
            If Xlbook Is Nothing Then : Xlbook = XlApp.Workbooks.Open(Directry & FileName) : DBWrite.WriteLine("Catalog | OpenSpreadsheet | Opening spreadsheet: " & FileName) : End If

            ' Bring the excel into focus.
            Xlbook.Activate()
            XlApp.Visible = True
            XlApp.WindowState = Excel.XlWindowState.xlMaximized
            XlSh = Xlbook.Sheets("Select part_catalog")
            XlSh.Activate()
            DBWrite.WriteLine("Catalog | OpenSpreadsheet | Spreadsheet is active and visible.")

        Catch ex As NullReferenceException
            ' If excel is not open.
            'create a new excel
            XlApp = CreateObject("Excel.Application")
            XlApp.Visible = True
            DBWrite.WriteLine("Catalog | OpenSpreadsheet | Starting a new excel application.")

            'open the existing report
            Xlbook = XlApp.Workbooks.Open(Directry & FileName)
            DBWrite.WriteLine("Catalog | OpenSpreadsheet | Opening spreadsheet: " & FileName)

            ' Wait
            Do Until Xlbook.ReadOnly = False : XlApp.Wait(TimeValue("00:00:01")) : Loop

            ' Bring the excel into focus.
            Xlbook.Activate()
            XlApp.Visible = True
            XlApp.WindowState = Excel.XlWindowState.xlMaximized
            XlSh = Xlbook.Sheets("Select part_catalog")
            XlSh.Activate()
            DBWrite.WriteLine("Catalog | OpenSpreadsheet | Spreadsheet is active and visible.")

        Catch ex As System.Runtime.InteropServices.COMException
            ' If excel is not open.
            'create a new excel
            XlApp = CreateObject("Excel.Application")
            XlApp.Visible = True
            DBWrite.WriteLine("Catalog | OpenSpreadsheet | Starting a new excel application.")

            'open the existing report
            Xlbook = XlApp.Workbooks.Open(Directry & FileName)
            DBWrite.WriteLine("Catalog | OpenSpreadsheet | Opening spreadsheet: " & FileName)

            ' Wait
            Do Until Xlbook.ReadOnly = False : XlApp.Wait(TimeValue("00:00:01")) : Loop

            ' Bring the excel into focus.
            Xlbook.Activate()
            XlApp.Visible = True
            XlApp.WindowState = Excel.XlWindowState.xlMaximized
            XlSh = Xlbook.Sheets("Select part_catalog")
            XlSh.Activate()
            DBWrite.WriteLine("Catalog | OpenSpreadsheet | Spreadsheet is active and visible.")

        End Try

    End Sub
#End Region

#Region " GetFilterList | Private Sub"
    Private Sub GetFilterList()
        Try

            ' Static list from reading the spreadsheet
            TypeSelectionList = New List(Of String) From {GET FILTER LIST}
            DBWrite.WriteLine("Catalog | GetFilterList | Established the filter list.")

        Catch ex As Exception
            MessageBox.Show("Catalog | GetFilterList" & vbNewLine & "Problems creating the filter list.", "Error!")
        End Try
    End Sub
#End Region

#Region " ApplyFiltertoSpreadsheet | Private Sub"
    Private Sub ApplyFiltertoSpreadsheet()
        'Try
        'If XlSh.FilterMode Then : DBWrite.WriteLine("Catalog | ApplyFiltertoSpreadsheet | Filtermode is ON.") : Else : DBWrite.WriteLine("Catalog | ApplyFiltertoSpreadsheet | Filtermode is OFF.") : End If

        ' Static list from reading the spreadsheet
        Dim LastRowIndex As Integer = XlSh.UsedRange.Rows.Count

        XlSh.Range("F13", "F" & LastRowIndex).AutoFilter(1, Type_Selection.FilterText, Excel.XlAutoFilterOperator.xlAnd,, True)

        Dim FcolFilter As Excel.Filter = XlSh.AutoFilter.Filters.Item(5)
        FcolFilter.Criteria1()
        Stop
        'Dim ListOfFilters As New List(Of Excel.Filter) : For Each Filtr As Excel.Filter In XlSh.AutoFilter.Filters : ListOfFilters.Add(Filtr) : Next
        'Stop
        'If XlSh.FilterMode Then : DBWrite.WriteLine("Catalog | ApplyFiltertoSpreadsheet | Filtermode is ON.") : Else : DBWrite.WriteLine("Catalog | ApplyFiltertoSpreadsheet | Filtermode is OFF.") : End If

        'Catch ex As Exception
        '    MessageBox.Show("Catalog | GetFilterList" & vbNewLine & "Problems applying the selected filter.", "Error!")
        'End Try
    End Sub
#End Region

End Class

**Popup dialog class
Public Class Type_Selection
    Public FilterText As String
    Dim g_FormCatSelect As Type_Selection = Me

    Private Sub Type_Selection_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        LoadFilterItems()

    End Sub

    Private Sub Btn_Select_Click(sender As Object, e As EventArgs) Handles Btn_Select.Click
        FilterText = Me.CmboBox_SelectAType.SelectedItem
        Me.Close()
    End Sub

#Region "Load combobox with filter items"
    Private Sub LoadFilterItems()
        ' Add the categories to the dropdown list.
        For Each Entity As String In Catalog.TypeSelectionList
            'g_FormCatSelect.Invoke(New UpdatePropertiesTabDel(AddressOf UpdatePropertiesTab))
            g_FormCatSelect.CmboBox_SelectAType.Items.Add(Entity)
        Next

        ' Make the first category in the list as the default.
        g_FormCatSelect.CmboBox_SelectAType.SelectedItem = g_FormCatSelect.CmboBox_SelectAType.Items.Item(0)
    End Sub
#End Region
End Class

【问题讨论】:

  • 可能需要看一些代码,你是如何尝试构建这个列表的,它的来源是什么。
  • @Hursey 该程序首先打开零件目录电子表格并激活正确的工作表。然后获取/创建过滤器列表(你正在帮助什么)。然后打开一个新表单,其中包含将由列表填充的下拉列表(组合框)。
  • 如果您编辑问题并显示打开此电子表格的代码以及您如何尝试构建该列表,仍然会有所帮助。任何答案实际上都将取决于您在做什么,否则人们只是在猜测
  • @Hursey 如果你想看一下,我已经添加了代码。
  • 我们说的是行 TypeSelectionList = New List(Of String) From {GET FILTER LIST}?为什么不向我们展示您的最佳尝试?您是否考虑过使用 OleDB 或其他 3rd 方 excel 处理工具之一,这些工具可能更适合数据量而不是办公自动化?

标签: excel vb.net list filter


【解决方案1】:

如果我理解你的意思,我想你可以简单地使用这个公式:

=UNIQUE(Table1[Column1]) 

希望对你有帮助!如果对您有帮助,请将此设置为正确答案! :)

【讨论】:

    猜你喜欢
    • 2021-01-24
    • 2021-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-02
    • 1970-01-01
    • 1970-01-01
    • 2020-09-27
    相关资源
    最近更新 更多