Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

showing data on form based on activesheet using optionbutton

0

Hello

I have a problem when select one of optionbutton on userform  .

if  I select  one of optionbuttons then will select the sheet

Private Sub OptionButton1_Click()
If OptionButton1.Value = True And OptionButton2.Value = False Then sheet5.Select
End Sub

Private Sub OptionButton2_Click()
If OptionButton2.Value = True And OptionButton1.Value = False Then sheet6.Select
End Sub

  and should populate data for selected sheet based on  this line 

Private Sub UserForm_Activate()
With ActiveSheet

  a = .Range("A2:G" & .Range("D" & Rows.Count).End(3).Row).Value

  End With
End Sub

but seems working for just one sheet ignoring procedure activesheet in UserForm_Activate .

so what I want populating data in listbox based on selected sheet from optionbutton to become  activesheet   when write the BARND for column D into textbox2 .

thanks

Answer
Discuss

Answers

0
Selected Answer

Hi again Leopard

Your procedure:

Private Sub UserForm_Activate()
With ActiveSheet

  a = .Range("A2:G" & .Range("D" & Rows.Count).End(3).Row).Value

  End With
End Sub

is triggered only once (when the UserForm launches) and even then you don't do anything with the variant "a" which it creates so you get a blank form..

In the revised file attached, I've copied the same code into a new private procedure:

Private Sub ChangeSheet()
    With ActiveSheet
        a = .Range("A2:G" & .Range("D" & Rows.Count).End(3).Row).Value
    End With
End Sub

which gets called by a line in the option button procedures which select a sheet (which also invoke the filtering to populate the ListBox):

Private Sub OptionButton1_Click()
    If OptionButton1.Value = True And OptionButton2.Value = False Then sheet5.Select
    Call ChangeSheet
    Call FilterData
End Sub

Private Sub OptionButton2_Click()
    If OptionButton2.Value = True And OptionButton1.Value = False Then sheet6.Select
    Call ChangeSheet
    Call FilterData
End Sub

Now - with a sheet button clicked -if you type "BS 12" say in TextBox2, you'll see the data get filtered.

Not sure why you're activating the sheets (which isn't needed unless you use ActiveSheet rather than sheet5 say in the first two procedures above) but assume you want to be reminded what the BRAND data looks like.

Hope this fixes your problem. If so, please mark my Answer as Selected.

Discuss

Discussion

Hi John , I hope  you're fine .
I tested call filterdata  procedure  in optionbuttons, and doesn't  work before I posted my question . but ChangeSheet() procedure  works perfectly .
thank you so much .
have a nice weekeend !
leopard (rep: 88) Dec 23, '23 at 1:50 pm
Glad that worked for you Leopard. Thanks for selecting my Answer. Have a good weekend too (and a Merry Christmas if you celebrate that). 
John_Ru (rep: 6142) Dec 23, '23 at 1:59 pm
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login