ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   option button-if else? (https://www.excelbanter.com/excel-programming/326002-option-button-if-else.html)

Alen32

option button-if else?
 
I wamt to make third option button where is possible to choose both horse
and pig. Whta I need to change in my code?

If .OptionButton1 Then
sOne = "Horse"
Else
sOne = "Pig"


Bob Phillips[_6_]

option button-if else?
 
Is it on a userform? If so, then

If Me.OptionButton1.Value Then
sOne = "Horse"
Else
sOne = "Pig"
End If

--

HTH

RP
(remove nothere from the email address if mailing direct)


"Alen32" wrote in message
lkaboutsoftware.com...
I wamt to make third option button where is possible to choose both horse
and pig. Whta I need to change in my code?

If .OptionButton1 Then
sOne = "Horse"
Else
sOne = "Pig"




Alen32

option button-if else?
 

I have 3 optionbutton
1.Horse
2.Pig
3.Both

This work only for first 2 buttons. What to change so third one also
work?

If Me.OptionButton1.Value Then
sOne = "Horse"
Else
sOne = "Pig"
End If



Bob Phillips[_6_]

option button-if else?
 
Do you mean

If Me.OptionButton1.Value Then
sOne = "Horse"
ElseIf Me.Optionbutton2.Value Then
sOne = "Pig"
ElseIf Me.Optionbutton3.Value
sOne = "Horse & Pig"
End If

--

HTH

RP
(remove nothere from the email address if mailing direct)


"Alen32" wrote in message
lkaboutsoftware.com...

I have 3 optionbutton
1.Horse
2.Pig
3.Both

This work only for first 2 buttons. What to change so third one also
work?

If Me.OptionButton1.Value Then
sOne = "Horse"
Else
sOne = "Pig"
End If





Alen32

option button-if else?
 
sorry I have difuculties to explain in english what I want. I got this code
here which works well. Now I want to make third optionbutton where
user can choose both horse and pig. That means program should look for
either horse or pig in the samme row. and if program find either horse or
pig in one row + sTwo and Sthree insert that row in listbox.



With UserForm1

If Me.OptionButton1.Value Then
sOne = "Horse"
ElseIf Me.OptionButton2.Value Then
sOne = "Pig"
?????ElseIf Me.OptionButton5.Value Then
sOne = "Horse Or Pig" ????????
End If


If .OptionButton3 Then
sTwo = "Danish"
Else
sTwo = "Foreign"
End If
sThree = .TextBox1.Text
End With
With Worksheets(1).Cells
Set c = .Find(sOne, LookIn:=xlValues)
If Not c Is Nothing Then
firstAddress = c.Address
Do
If Application.CountIf(c.EntireRow, "*" & sTwo & "*") And _
Application.CountIf(c.EntireRow, "*" & sThree & "*") Then
UserForm1.ListBox1.AddItem c.Value
UserForm1.ListBox1.List( _
UserForm1.ListBox1.ListCount - 1, 1) _
= c.Offset(0, 2).Value
UserForm1.ListBox1.List( _
UserForm1.ListBox1.ListCount - 1, 2) _
= c.Offset(0, 5).Value
End If
Set c = .FindNext(c)
Loop While Not c Is Nothing And c.Address < firstAddress
End If
End With


Bob Phillips[_6_]

option button-if else?
 
How about this


With UserForm1

If .OptionButton1.Value Then
sOne = "Horse"
sOnePlus = ""
ElseIf .OptionButton2.Value Then
sOne = "Pig"
sOnePlus = ""
ElseIf .OptionButton5.Value Then
sOne = "Horse"
sOnePlus = "Pig"
End If


If .OptionButton3 Then
stwo = "Danish"
Else
stwo = "Foreign"
End If
sThree = .TextBox1.Text
End With

With Worksheets(1).Cells
Set c = Nothing
Set c = .Find(sOne, LookIn:=xlValues)
If c Is Nothing And stwo < "" Then
Set c = .Find(sOnePlus, LookIn:=xlValues)
If Not c Is Nothing Then
firstAddress = c.Address
Do
If Application.CountIf(c.EntireRow, "*" & stwo & "*") And _
Application.CountIf(c.EntireRow, "*" & sThree & "*") Then
UserForm1.ListBox1.AddItem c.Value
UserForm1.ListBox1.List( _
UserForm1.ListBox1.ListCount - 1, 1) _
= c.Offset(0, 2).Value
UserForm1.ListBox1.List( _
UserForm1.ListBox1.ListCount - 1, 2) _
= c.Offset(0, 5).Value
End If
Set c = .FindNext(c)
Loop While Not c Is Nothing And c.Address < firstAddress
End If
End If
End With

--

HTH

RP
(remove nothere from the email address if mailing direct)


"Alen32" wrote in message
lkaboutsoftware.com...
sorry I have difuculties to explain in english what I want. I got this

code
here which works well. Now I want to make third optionbutton where
user can choose both horse and pig. That means program should look for
either horse or pig in the samme row. and if program find either horse or
pig in one row + sTwo and Sthree insert that row in listbox.



With UserForm1

If Me.OptionButton1.Value Then
sOne = "Horse"
ElseIf Me.OptionButton2.Value Then
sOne = "Pig"
?????ElseIf Me.OptionButton5.Value Then
sOne = "Horse Or Pig" ????????
End If


If .OptionButton3 Then
sTwo = "Danish"
Else
sTwo = "Foreign"
End If
sThree = .TextBox1.Text
End With
With Worksheets(1).Cells
Set c = .Find(sOne, LookIn:=xlValues)
If Not c Is Nothing Then
firstAddress = c.Address
Do
If Application.CountIf(c.EntireRow, "*" & sTwo & "*") And _
Application.CountIf(c.EntireRow, "*" & sThree & "*") Then
UserForm1.ListBox1.AddItem c.Value
UserForm1.ListBox1.List( _
UserForm1.ListBox1.ListCount - 1, 1) _
= c.Offset(0, 2).Value
UserForm1.ListBox1.List( _
UserForm1.ListBox1.ListCount - 1, 2) _
= c.Offset(0, 5).Value
End If
Set c = .FindNext(c)
Loop While Not c Is Nothing And c.Address < firstAddress
End If
End With




Alen32

option button-if else?
 
This makro doesn't fill listbox.


Bob Phillips[_6_]

option button-if else?
 
Sorry, my fault

With UserForm1

If .OptionButton1.Value Then
sOne = "Horse"
sOnePlus = ""
ElseIf .OptionButton2.Value Then
sOne = "Pig"
sOnePlus = ""
ElseIf .OptionButton5.Value Then
sOne = "Horse"
sOnePlus = "Pig"
End If


If .OptionButton3 Then
stwo = "Danish"
Else
stwo = "Foreign"
End If
sThree = .TextBox1.Text
End With

With Worksheets(1).Cells
Set c = Nothing
Set c = .Find(sOne, LookIn:=xlValues)
If c Is Nothing And stwo < "" Then
Set c = .Find(sOnePlus, LookIn:=xlValues)
End If

If Not c Is Nothing Then
firstAddress = c.Address
Do
If Application.CountIf(c.EntireRow, "*" & stwo & "*") And _
Application.CountIf(c.EntireRow, "*" & sThree & "*") Then
UserForm1.ListBox1.AddItem c.Value
UserForm1.ListBox1.List( _
UserForm1.ListBox1.ListCount - 1, 1) _
= c.Offset(0, 2).Value
UserForm1.ListBox1.List( _
UserForm1.ListBox1.ListCount - 1, 2) _
= c.Offset(0, 5).Value
End If
Set c = .FindNext(c)
Loop While Not c Is Nothing And c.Address < firstAddress
End If
End With

--

HTH

RP
(remove nothere from the email address if mailing direct)


"Alen32" wrote in message
lkaboutsoftware.com...
This makro doesn't fill listbox.




Alen32

option button-if else?
 
Ok now is much better first and third optionsbottons work but when I select
second optionbotton then I get error message
Aplication-defined or object-defined error
and this piece of code get yellow
UserForm1.ListBox1.List( _
UserForm1.ListBox1.ListCount - 1, 2) _
= c.Offset(0, 5).Value


Bob Phillips[_6_]

option button-if else?
 
Can't reproduce the error Alen.

What data are you checking when you get this?

--

HTH

RP
(remove nothere from the email address if mailing direct)


"Alen32" wrote in message
lkaboutsoftware.com...
Ok now is much better first and third optionsbottons work but when I

select
second optionbotton then I get error message
Aplication-defined or object-defined error
and this piece of code get yellow
UserForm1.ListBox1.List( _
UserForm1.ListBox1.ListCount - 1, 2) _
= c.Offset(0, 5).Value




Alen32

option button-if else?
 
When I search for pig and pig doesn't exist on sheet. it should be just
empty listbox.


Alen32

option button-if else?
 
Also third optionbutton doesn't work in case where I have both pig and
horse in column. Only horse is inserted into listbox and not pig.


Alen32

option button-if else?
 
There are two problems in Makro:
1) Third optionbutton doesn't work in case where I have both pig and
horse in column. Only horse is inserted into listbox and not pig.

2) When I search for pig and pig doesn't exist on sheet it comes error
message. it should be just
empty listbox.



Alen32

option button-if else?
 
I got answer :
Private Sub CommandButton1_Click()
Dim sOne, sOneItem
With UserForm1
'******
If .OptionButton1 Then sOne = Array("Horse")
If .OptionButton2 Then sOne = Array("Pig")
If .OptionButton5 Then sOne = Array("Horse", "Pig")
'******
If .OptionButton3 Then
sTwo = "Danish"
Else
sTwo = "Foreign"
End If
sThree = .TextBox1.Text
End With

'******
For Each sOneItem In sOne
'******
'MsgBox sOneItem
With Worksheets(1).Cells
Set c = .Find(sOneItem, LookIn:=xlValues)
If Not c Is Nothing Then
firstAddress = c.Address
Do
If Application.CountIf(c.EntireRow, "*" & sTwo & "*") And _
Application.CountIf(c.EntireRow, "*" & sThree & "*")
Then
UserForm1.ListBox1.AddItem c.Value
UserForm1.ListBox1.List( _
UserForm1.ListBox1.ListCount - 1, 1) _
= c.Offset(0, 2).Value
UserForm1.ListBox1.List( _
UserForm1.ListBox1.ListCount - 1, 2) _
= c.Offset(0, 5).Value
End If
Set c = .FindNext(c)
Loop While Not c Is Nothing And c.Address < firstAddress
End If
End With
'******
Next
'******
End Sub



All times are GMT +1. The time now is 10:16 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com