mynguyen129
New Member
- Joined
- Sep 5, 2021
- Messages
- 1
- Office Version
- 365
- Platform
- MacOS
Hi All,
I'm new to VBA. So I have no idea how to work my button. I followed instructions from How To Create A Filtering Search Box For Your Excel Data — TheSpreadsheetGuru. I also changed the cell reference. I am currently getting an error msg. Not sure what to do with this.
This is what I copied:
Sub SearchBox()
'PURPOSE: Filter Data on User-Determined Column & Text/Numerical value
'SOURCE: www.TheSpreadsheetGuru.com
Dim myButton As OptionButton
Dim SearchString As String
Dim ButtonName As String
Dim sht As Worksheet
Dim myField As Long
Dim DataRange As Range
Dim mySearch As Variant
'Load Sheet into A Variable
Set sht = ActiveSheet
'Unfilter Data (if necessary)
On Error Resume Next
sht.ShowAllData
On Error GoTo 0
'Filtered Data Range (include column heading cells)
Set DataRange = sht.Range("B15:R89") 'Cell Range
'Set DataRange = sht.ListObjects("Table1").Range 'Table
'Retrieve User's Search Input
mySearch = sht.Shapes("UserSearch").TextFrame.Characters.Text 'Control Form
'mySearch = sht.OLEObjects("UserSearch").Object.Text 'ActiveX Control
'mySearch = sht.Range("A1").Value 'Cell Input
'Determine if user is searching for number or text
If IsNumeric(mySearch) = True Then
SearchString = "=" & mySearch
Else
SearchString = "=*" & mySearch & "*"
End If
'Loop Through Option Buttons
For Each myButton In sht.OptionButtons
If myButton.Value = 1 Then
ButtonName = myButton.Text
Exit For
End If
Next myButton
'Determine Filter Field
On Error GoTo HeadingNotFound
myField = Application.WorksheetFunction.Match(ButtonName, DataRange.Rows(1), 0)
On Error GoTo 0
'Filter Data
DataRange.AutoFilter _
Field:=myField, _
Criteria1:=SearchString, _
Operator:=xlAnd
'Clear Search Field
sht.Shapes("UserSearch").TextFrame.Characters.Text = "" 'Control Form
'sht.OLEObjects("UserSearch").Object.Text = "" 'ActiveX Control
'sht.Range("A1").Value = "" 'Cell Input
Exit Sub
'ERROR HANDLERS
HeadingNotFound:
MsgBox "The column heading [" & ButtonName & "] was not found in cells " & DataRange.Rows(1).Address & ". " & _
vbNewLine & "Please check for possible typos.", vbCritical, "Header Name Not Found!"
End Sub
I'm new to VBA. So I have no idea how to work my button. I followed instructions from How To Create A Filtering Search Box For Your Excel Data — TheSpreadsheetGuru. I also changed the cell reference. I am currently getting an error msg. Not sure what to do with this.
This is what I copied:
Sub SearchBox()
'PURPOSE: Filter Data on User-Determined Column & Text/Numerical value
'SOURCE: www.TheSpreadsheetGuru.com
Dim myButton As OptionButton
Dim SearchString As String
Dim ButtonName As String
Dim sht As Worksheet
Dim myField As Long
Dim DataRange As Range
Dim mySearch As Variant
'Load Sheet into A Variable
Set sht = ActiveSheet
'Unfilter Data (if necessary)
On Error Resume Next
sht.ShowAllData
On Error GoTo 0
'Filtered Data Range (include column heading cells)
Set DataRange = sht.Range("B15:R89") 'Cell Range
'Set DataRange = sht.ListObjects("Table1").Range 'Table
'Retrieve User's Search Input
mySearch = sht.Shapes("UserSearch").TextFrame.Characters.Text 'Control Form
'mySearch = sht.OLEObjects("UserSearch").Object.Text 'ActiveX Control
'mySearch = sht.Range("A1").Value 'Cell Input
'Determine if user is searching for number or text
If IsNumeric(mySearch) = True Then
SearchString = "=" & mySearch
Else
SearchString = "=*" & mySearch & "*"
End If
'Loop Through Option Buttons
For Each myButton In sht.OptionButtons
If myButton.Value = 1 Then
ButtonName = myButton.Text
Exit For
End If
Next myButton
'Determine Filter Field
On Error GoTo HeadingNotFound
myField = Application.WorksheetFunction.Match(ButtonName, DataRange.Rows(1), 0)
On Error GoTo 0
'Filter Data
DataRange.AutoFilter _
Field:=myField, _
Criteria1:=SearchString, _
Operator:=xlAnd
'Clear Search Field
sht.Shapes("UserSearch").TextFrame.Characters.Text = "" 'Control Form
'sht.OLEObjects("UserSearch").Object.Text = "" 'ActiveX Control
'sht.Range("A1").Value = "" 'Cell Input
Exit Sub
'ERROR HANDLERS
HeadingNotFound:
MsgBox "The column heading [" & ButtonName & "] was not found in cells " & DataRange.Rows(1).Address & ". " & _
vbNewLine & "Please check for possible typos.", vbCritical, "Header Name Not Found!"
End Sub
Buttons.xlsx | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | |||
2 | Search | |||||||||||||||||
3 | ||||||||||||||||||
4 | ||||||||||||||||||
5 | ||||||||||||||||||
6 | ||||||||||||||||||
7 | ||||||||||||||||||
8 | ||||||||||||||||||
9 | ||||||||||||||||||
10 | ||||||||||||||||||
11 | ||||||||||||||||||
12 | ||||||||||||||||||
13 | ||||||||||||||||||
14 | 5 | Faculty | Class | EXAM | Surname | Given Name | Time Taken | Time Taken (in secs) | Score | Exam completion | Test Duration (in mins) | Attended At | Status | Comments | ||||
15 | 2-Sep | EAL/D | EAL2 | EALD | Nguyen | Truong | 1 hr 29 mins 50 secs | 5390 | 0 | Submit | 90 | 2021-09-02 00:00:06 UTC | #N/A | |||||
16 | 2-Sep | EAL/D | EAL4 | EALD | Asliwa | Rami Gorgees Asliwa | 1 hr 27 mins 2 secs | 5222 | 0 | Submit | 90 | 2021-09-02 00:00:07 UTC | #N/A | |||||
17 | 2-Sep | EAL/D | EAL3 | EALD | Truong | Thi Hien Trang | 1 hr 28 mins 3 secs | 5283 | 0 | Submit | 90 | 2021-09-02 00:00:07 UTC | #N/A | |||||
18 | 2-Sep | EAL/D | EAL4 | EALD | Kashi | Rita | 1 hr 30 mins | 5400 | 0 | Submit | 90 | 2021-09-02 00:00:08 UTC | #N/A | |||||
19 | 2-Sep | EAL/D | EAL3 | EALD | Danyliuk | Victoria | 1 hr 29 mins 15 secs | 5355 | 0 | Submit | 90 | 2021-09-02 00:00:08 UTC | #N/A | |||||
20 | 2-Sep | EAL/D | EAL2 | EALD | Nguyen | Hoang Quan | 1 hr 29 mins 59 secs | 5399 | 0 | Submit | 90 | 2021-09-02 00:00:09 UTC | #N/A | |||||
21 | 2-Sep | EAL/D | EAL1 | EALD | Vu | Viet Anh | 1 hr 30 mins | 5400 | 0 | Submit | 90 | 2021-09-02 00:00:09 UTC | #N/A | |||||
22 | 2-Sep | EAL/D | EAL2 | EALD | Truong | Linh Dan | 1 hr 30 mins | 5400 | 0 | Submit | 90 | 2021-09-02 00:00:09 UTC | #N/A | |||||
23 | 2-Sep | EAL/D | EAL3 | EALD | Sanna | Delan Dhiaa Shamoon | 1 hr 30 mins | 5400 | 0 | Submit | 90 | 2021-09-02 00:00:10 UTC | #N/A | |||||
24 | 2-Sep | EAL/D | EAL2 | EALD | Luu | Tuan Anh | 1 hr 17 mins 2 secs | 4622 | 0 | Submit | 90 | 2021-09-02 00:00:10 UTC | #N/A | |||||
25 | 2-Sep | EAL/D | EAL4 | EALD | Alsamani | Rafael Ghanim Gorgees | 1 hr 28 mins 8 secs | 5288 | 0 | Submit | 90 | 2021-09-02 00:00:10 UTC | #N/A | |||||
26 | 2-Sep | EAL/D | EAL3 | EALD | Mati | Leelyan Silewa Mati | 1 hr 28 mins 40 secs | 5320 | 0 | Submit | 90 | 2021-09-02 00:00:11 UTC | #N/A | |||||
27 | 2-Sep | EAL/D | EAL2 | EALD | Sayaseng | Kanthanouxay | 1 hr 22 mins 40 secs | 4960 | 0 | Submit | 90 | 2021-09-02 00:00:11 UTC | #N/A | |||||
28 | 2-Sep | EAL/D | EAL4 | EALD | Xue | Guanru | 1 hr 30 mins | 5400 | 0 | Submit | 90 | 2021-09-02 00:00:11 UTC | #N/A | |||||
29 | 2-Sep | EAL/D | EAL4 | EALD | Low | Gavin Yong Hui | 50 mins 33 secs | 3033 | 0 | Submit | 90 | 2021-09-02 00:00:11 UTC | #N/A | |||||
30 | 2-Sep | EAL/D | EAL3 | EALD | Nguyen | Ngoc Quynh Anh | 1 hr 21 mins 29 secs | 4889 | 0 | Submit | 90 | 2021-09-02 00:00:13 UTC | #N/A | |||||
31 | 2-Sep | EAL/D | EAL3 | EALD | Iskandar | Malak Bassel | 1 hr 29 mins 58 secs | 5398 | 0 | Submit | 90 | 2021-09-02 00:00:15 UTC | #N/A | |||||
32 | 2-Sep | EAL/D | EAL3 | EALD | Nguyen | Duc Thien Phuong | 1 hr 23 mins 32 secs | 5012 | 0 | Submit | 90 | 2021-09-02 00:00:16 UTC | #N/A | |||||
Dashboard |