advanced filter by optionbuttons & combobox

abdelfattah

Well-known Member
Joined
May 3, 2019
Messages
1,429
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
hi, all
actually i have a problem when press on of the option button (CODE) OR (ORHIGIN) OR (BRAND )and choose from combobox it doesn't filter data correctly it just filter in row1 and hide the rest of data for instance if i press code and choose from combobox aa1 it just filter one row but it supposes two row and if i press origin it 's filtering wrong just aa1
this is main sheet & userform
op.JPG



this is some results

OP2.JPG



this when i choose the lastrow in combobox stay filter first row
25.JPG




my codes
VBA Code:
Private Sub ComboBox1_Click()
Dim I As Long
If Me.OptionButton1.Value = True Then
SHEET1.Range("A2").Value = Me.ComboBox1.Value
ElseIf Me.OptionButton2.Value = True Then
SHEET1.Range("B2").Value = Me.ComboBox1.Value
ElseIf Me.OptionButton3.Value = True Then
SHEET1.Range("C2").Value = Me.ComboBox1.Value
Else
SHEET1.Range("D2").Value = Me.ComboBox1.Value
End If
On Error Resume Next
ActiveSheet.ShowAllData
I = SHEET1.Range("A100000").End(xlUp).Offset(1, 0).Row
SHEET1.Range("A" & 2, "E" & I).AdvancedFilter xlFilterInPlace, SHEET1.Range("A2:E3")

End Sub

Private Sub CommandButton1_Click()
ActiveSheet.ShowAllData
End Sub

Private Sub OptionButton1_Click()
Me.ComboBox1.BoundColumn = 1
End Sub

Private Sub OptionButton2_Click()
Me.ComboBox1.BoundColumn = 2
End Sub

Private Sub OptionButton3_Click()
Me.ComboBox1.BoundColumn = 3
End Sub

Private Sub OptionButton4_Click()
Me.ComboBox1.BoundColumn = 4
End Sub

Private Sub UserForm_Initialize()
Dim r As Range
With Worksheets("Sheet1")
        Set r = .Range("A2", .Range("E65536").End(xlUp))
End With

ComboBox1.RowSource = "Sheet1!" & r.Address
End Sub
 

Attachments

  • op1.JPG
    op1.JPG
    52.9 KB · Views: 4
  • OP2.JPG
    OP2.JPG
    57.4 KB · Views: 3
  • OP4.JPG
    OP4.JPG
    81.9 KB · Views: 2
  • op3.JPG
    op3.JPG
    54.6 KB · Views: 3

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Hi,
try replacing your codes with following & see if will do what you want

VBA Code:
Dim rng As Range
Private Sub ComboBox1_Change()
    Dim i As Integer
    Dim FilterValue As String
    Dim ws As Worksheet
   
    Set ws = rng.Parent
    FilterValue = Me.ComboBox1.Value
   
    For i = 1 To 4
        If Me.Controls("OptionButton" & i).Value Then
'create filter
            ws.Cells(1, 10).Value = ws.Cells(1, i).Value
            ws.Cells(2, 10).Formula = "=" & """=" & FilterValue & """"
            Exit For
        End If
    Next
  
    On Error Resume Next
    If ws.FilterMode Then ws.ShowAllData
    If Len(FilterValue) > 0 Then rng.AdvancedFilter xlFilterInPlace, CriteriaRange:=ws.Range("J1:J2")
End Sub

Sub SetComBoBox(ByVal Index As Integer)
    With Me.ComboBox1
        .BoundColumn = Index
        .TextColumn = Index
        .ListIndex = -1
    End With
End Sub
Private Sub OptionButton1_Click()
    SetComBoBox 1
End Sub
Private Sub OptionButton2_Click()
    SetComBoBox 2
End Sub
Private Sub OptionButton3_Click()
    SetComBoBox 3
End Sub
Private Sub OptionButton4_Click()
    SetComBoBox 4
End Sub

Private Sub UserForm_Initialize()

    Set rng = Worksheets("Sheet1").Range("A1").CurrentRegion
   
     With Me.ComboBox1
        .RowSource = ""
        .ColumnCount = 4
        .List = rng.Offset(1, 0).Resize(rng.Rows.Count - 1).Value
     End With
     Me.OptionButton1.Value = True
End Sub

Note: variable rng which must sit up TOP of your userforms code page.

Hope Helpful

Dave
 
Upvote 0
Solution
awesome your codes thanks so much for your assistance this is what i'look for but i have problem when i run the form an i change option button from code to brand or origin or brand or type it gives me error run time error 94 in this line FilterValue = Me.ComboBox1.Value
 
Last edited:
Upvote 0
awesome your codes thanks so much for your assistance this is what i'look for but i have problem when i run the form an i change option button from code to brand or origin or brand or type it gives me error run time error 94 in this line FilterValue = Me.ComboBox1.Value

try changing that line with following

Rich (BB code):
FilterValue = Me.ComboBox1.Text

and see if resolves

Dave
 
Upvote 0
yes, it works excellently, thanks for every thing

take care
 
Upvote 0

Forum statistics

Threads
1,215,034
Messages
6,122,782
Members
449,095
Latest member
m_smith_solihull

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top