简体   繁体   English

Excel VBA中的复选框搜索

[英]Checkbox search in excel VBA

this is my first time in this forum. 这是我第一次参加这个论坛。 Im a 'newbie' in excel VBA and would appreciate if anyone can help. 我是excel VBA中的“新手”,如果有人可以提供帮助,我们将不胜感激。

I am trying to develop a checkbox search page, where: 我正在尝试开发一个复选框搜索页面,其中:

  • the user is able to use checkbox select up to 4 items to search (in 'Search' worksheet) 用户可以使用复选框选择最多4个项目进行搜索(在“搜索”工作表中)
  • In the data sheet, the data is recorded (by columns) as follows: 在数据表中,数据记录如下(按列):

Code Book Item Material 1 Percentage 1 Material 2 Percentage 2 Material 3 Percentage 3 Material 4 Percentage 4 代码本项目物料1百分比1物料2百分比2物料3百分比3物料4百分比4

  • an item, eg "Polyester", will appear in columns 'material 1', 2 and so on, depending on the record 一个项目(例如“聚酯”)将出现在“材料1”,“ 2”等列中,具体取决于记录
  • the search should be based on the checkboxes that are checked in the search page 搜索应基于搜索页面中选中的复选框
  • if multiple keywords are checked, eg polyester, wool, cotton, then only rows that has all 3 keywords should be shown 如果选中了多个关键字,例如聚酯,羊毛,棉花,则应仅显示所有3个关键字的行

I have searched numerous posts online, and there's one where they delete rows if the keywords are found. 我在网上搜索了无数帖子,有一个帖子在找到关键字的情况下会删除行。 Is there any way i can twist the logic and apply to this particular situation? 我有什么办法可以改变逻辑并适用于这种特殊情况?

Been stuck for many days... grateful if anyone can help! 被困了很多天...如果有人可以帮助,不胜感激!

Update 1: I have managed to find something on the internet that deletes the rows if certain keywords are found - however - i would like to do the opposite (this would leave me the remaining correct ones instead...) 更新1:我已经设法在互联网上找到一些可以找到某些关键字的行,但这些行会删除行-但是,我想做相反的事情(这将使我剩下的正确关键字...)

Code as below - is it possible to change?: 如下代码-是否可以更改?

Sub Example1()

    Dim varList As Variant
    Dim lngarrCounter As Long
    Dim rngFound As Range, rngToDelete As Range
    Dim strFirstAddress As String

    Application.ScreenUpdating = False

    varList = VBA.Array("Here", "There", "Everywhere")

    For lngarrCounter = LBound(varList) To UBound(varList)
        With Sheet1.UsedRange
            Set rngFound = .Find( _
                                What:=varList(lngarrCounter), _
                                Lookat:=xlWhole, _
                                SearchOrder:=xlByRows, _
                                SearchDirection:=xlNext, _
                                MatchCase:=True)

            If Not rngFound Is Nothing Then
                strFirstAddress = rngFound.Address

                If rngToDelete Is Nothing Then
                    Set rngToDelete = rngFound
                Else
                    If Application.Intersect(rngToDelete, rngFound.EntireRow) Is Nothing Then
                        Set rngToDelete = Application.Union(rngToDelete, rngFound)
                    End If
                End If

                Set rngFound = .FindNext(After:=rngFound)

                Do Until rngFound.Address = strFirstAddress
                    If Application.Intersect(rngToDelete, rngFound.EntireRow) Is Nothing Then
                        Set rngToDelete = Application.Union(rngToDelete, rngFound)
                    End If
                    Set rngFound = .FindNext(After:=rngFound)
                Loop
            End If
        End With
    Next lngarrCounter

    If Not rngToDelete Is Nothing Then rngToDelete.EntireRow.Delete

    Application.ScreenUpdating = True

Perhaps you could try the Sheets("Search").Cells.Find([keyword]) command which is similiar to the ctrl+L keys. 也许您可以尝试Sheets("Search").Cells.Find([keyword]) ctrl + L键的Sheets("Search").Cells.Find([keyword])命令。

If you wish to search more than 1 keyword, you could use the following loop: 如果您希望搜索多个关键字,则可以使用以下循环:

dim i as Integer
dim vTemp as Variant
dim strFind as String
dim objSearch as Object

strFind = range("searchField").value
vTemp = split(strFind, ",")

for i =0 to Ubound(vTemp)
    Set objSearch  = Sheets("Search").Cells.Find(vTemp(i))
    'Do whatever you like to the object of search
next i 

I hope I could help. 希望我能帮上忙。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM