PDA

View Full Version : How to make my combobox invisible



Mientje08
12-04-2018, 06:27 AM
Hello,

I am struggling with the code to make my combobox disappear

I want my combobox to appear when N6 = 1. In all other cases, it should disappear.

But my code doesn't work...

Ths is my code:
Private Sub Combobox_visible()
If Range("N6").Value = 1 Then
Me.ComboBox1.Visible = True
Else
Me.ComboBox1.Visible = False
End If
End If
End Sub

See file in attachment

Can someone please help me with this?

Thank you!

Aflatoon
12-04-2018, 06:34 AM
How/when are you running the code? (It won't run automatically)

rlv
12-04-2018, 07:05 AM
Three examples of using worksheet events to control combobox visibility based on a cell value





Private Sub Worksheet_Activate()
ComboBox1.Visible = Range("N6").Value = 1
End Sub


Private Sub Worksheet_Change(ByVal Target As Range)
ComboBox1.Visible = Range("N6").Value = 1
End Sub


Private Sub Worksheet_SelectionChange(ByVal Target As Range)
ComboBox1.Visible = Range("N6").Value = 1
End Sub

snb
12-04-2018, 07:19 AM
This is sufficient:


Private Sub Worksheet_SelectionChange(ByVal Target As Range)
ComboBox1.Visible = [A1]
End Sub

If A1 =0, then Combobox1<>visible.

Paul_Hossler
12-04-2018, 06:41 PM
I'd do it this way

I think some code was not in the right events and I had some screen ghosts when I was testing (could be my video driver though)

I don't like to put a simple one cell test in _SelectionChange since that event fires everytime. I prefer just _Change




Option Explicit

Private Sub ComboBox1_Change()
Range("G6").Value = Me.ComboBox1.Value
End Sub



Private Sub Worksheet_Activate()
ComboBox1.ListFillRange = "Dropdownlist"
Call pvtShowCombobox(Range("N6").Value = 1)
End Sub


Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Range("N6")) Is Nothing Then Exit Sub
Call pvtShowCombobox(Range("N6").Value = 1)
End Sub


Private Sub pvtShowCombobox(b As Boolean)
Application.ScreenUpdating = False
Application.EnableEvents = False
ComboBox1.Visible = b
Application.ScreenUpdating = True
Application.EnableEvents = True
DoEvents
End Sub

大灰狼1976
12-06-2018, 08:20 PM
Hi Mientje08
M6 and N6 can be deleted if they are of no other use.

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Count > 1 Then Exit Sub
If Target.Row <> 6 Then Exit Sub
If Target.Column <> 3 And Target.Column <> 5 Then Exit Sub
ComboBox1.Visible = [c6] & [e6] = "DealerYes"
End Sub