在列表框中显示执行结果时出现问题
我用excel 2013 vba的用户表单有最后一个问题。
我想在点击searchbutton(SearchButton)时,对我的用户表单中的search结果框(SearchResultsListBox)中显示的行数(结果)进行计数。
计算的行数(结果)应显示为结果find框(ResultsFoundTextBox)中的数字。
此刻,无论search结果框中的行数(结果)如何,总是返回96。 不知道为什么原始数据目前最多只有17-20行。
这是我的代码:
Private Sub SearchButton_Click() 'dim the variables Dim Crit As Range Dim FindMe As Range Dim SearchMe As Range Dim the_sheet1 As Worksheet Dim the_sheet3 As Worksheet 'error handler On Error GoTo errHandler: 'set object variables Set the_sheet1 = Sheets("Data Sheet") Set the_sheet3 = Sheets("Filter Data") 'hold in memory and stop screen flicker Application.ScreenUpdating = False 'if All Columns is selected With Me.SearchComboBox If .ListIndex <> -1 Then Select Case .Value Case "All" the_sheet3.Range("Y2") = "" the_sheet3.Range("Y3") = Me.EnterTextBox.Value 'if other categories are selected Case "Project Name", "Client", "Sector", "Status", "Contract Value", "Anticipated Final Account", "Revenue Traded Prior", "2015", "2016", "2017", "2018", "2019", "Discipline", "Board Director", "Associate Director", "Commercial Manager", "Project Manager", "Quantity Surveyor", "Pre-Con Start Date", "Actual Start Date", "Defect Period Start Date", "Defect Period End Date" the_sheet3.Range("Y2") = Me.SearchComboBox.Value the_sheet3.Range("Y3") = Me.EnterTextBox.Value End Select End If End With 'find the value in the column Set SearchMe = Sheets("Filter Data").Range("Y3") Set FindMe = Sheets("Data Sheet").Range("A2:V62").Find(What:=SearchMe, LookIn:=xlValues, LookAt _ :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _ False, SearchFormat:=False) Sheets("Filter Data").Range("Y2").Value = Cells(2, FindMe.Column) Sheets("Data Sheet").Range("A1:V62").AdvancedFilter Action:=xlFilterCopy, _ CriteriaRange:=Range("'Filter Data'!Criteria"), CopyToRange:=Range( _ "'Filter Data'!Extract"), Unique:=False 'variable for criteria header Set Crit = the_sheet3.Cells(2, FindMe.Column) 'if no criteria is added to the search If Me.EnterTextBox.Value = "" Then the_sheet3.Range("Y3") = "" Else 'add values from the search the_sheet3.Range("Y2") = Crit If Crit = "Project Name" Then the_sheet3.Range("Y3") = "*" & Me.EnterTextBox.Value & "*" If Crit = "Client" Then the_sheet3.Range("Y3") = "*" & Me.EnterTextBox.Value & "*" If Crit = "Sector" Then the_sheet3.Range("Y3") = "*" & Me.EnterTextBox.Value & "*" If Crit = "Status" Then the_sheet3.Range("Y3") = "*" & Me.EnterTextBox.Value & "*" If Crit = "Contract Value" Then the_sheet3.Range("Y3") = Me.EnterTextBox.Value If Crit = "Anticipated Final Account" Then the_sheet3.Range("Y3") = Me.EnterTextBox.Value If Crit = "Revenue Traded Prior" Then the_sheet3.Range("Y3") = Me.EnterTextBox.Value If Crit = "2015" Then the_sheet3.Range("Y3") = Me.EnterTextBox.Value If Crit = "2016" Then the_sheet3.Range("Y3") = Me.EnterTextBox.Value If Crit = "2017" Then the_sheet3.Range("Y3") = Me.EnterTextBox.Value If Crit = "2018" Then the_sheet3.Range("Y3") = Me.EnterTextBox.Value If Crit = "2019" Then the_sheet3.Range("Y3") = Me.EnterTextBox.Value If Crit = "Discipline" Then the_sheet3.Range("Y3") = "*" & Me.EnterTextBox.Value & "*" If Crit = "Board Director" Then the_sheet3.Range("Y3") = "*" & Me.EnterTextBox.Value & "*" If Crit = "Associate Director" Then the_sheet3.Range("Y3") = "*" & Me.EnterTextBox.Value & "*" If Crit = "Commercial Manager" Then the_sheet3.Range("Y3") = "*" & Me.EnterTextBox.Value & "*" If Crit = "Project Manager" Then the_sheet3.Range("Y3") = "*" & Me.EnterTextBox.Value & "*" If Crit = "Quantity Surveyor" Then the_sheet3.Range("Y3") = "*" & Me.EnterTextBox.Value & "*" If Crit = "Pre-Con Start Date" Then the_sheet3.Range("Y3") = Me.EnterTextBox.Value If Crit = "Actual Start Date" Then the_sheet3.Range("Y3") = Me.EnterTextBox.Value If Crit = "Defect Period Start Date" Then the_sheet3.Range("Y3") = Me.EnterTextBox.Value If Crit = "Defect Period End Date" Then the_sheet3.Range("Y3") = Me.EnterTextBox.Value End If End If End If End If End If End If End If End If End If End If End If End If End If End If End If End If End If End If End If End If End If End If End If 'unprotect all sheets 'Unprotect_All 'add the dynamic data to the listbox Me.SearchResultListBox.RowSource = Range("'Filter Data'!Extract").Address(external:=True) 'Number of results found Me.ResultsFoundTextBox.Value = Me.SearchResultListBox.ListCount 'protect all sheets 'Protect_All 'error handler On Error GoTo 0 Exit Sub errHandler: 'if error occurs then show me exactly where the error occurs MsgBox "No match found for " & EnterTextBox.Text Exit Sub End Sub
我意识到这可能是这个问题,但我已经尝试了这么多的select,似乎没有任何工作:
'Number of results found Me.ResultsFoundTextBox.Value = Me.SearchResultListBox.ListCount
谢谢大家。 最后一点,然后我完成了! 🙂
您的If语句分层不当。 通过放置所有If
第一个和End If
的后面,你嵌套所有的陈述。 因此,当序列中的一个语句评估为False时,其余的都不会到达。 鉴于您在每次检查时都检查相同的值,我build议您使用Select Case … Case区块(这会使代码更短且更容易阅读)重写本节。