When I use Excel to creat a List Box, The list box does not show until I clck on the…
- User
When I use Excel to creat a List Box, The list box does not show until I clck on the cell. This is the standard for excel. In the VBA code below, it is creating a dynamic list box. The list blox shows all the time. Is there anyway for it to create a listbox exactly like excel.
Private Sub Worksheet_Change(ByVal Target As Range) Dim selectedCell As Range Dim selectedValue As String Dim matchValue As String Dim matchColumn As Range Dim cell As Range Dim listBox As OLEObject Dim columnValues As String Dim lastRow As Long Dim i As Integer
' Check if the changed cell is in column D of the 'Key Allocation' sheet If Not Intersect(Target, Me.Range("D:D")) Is Nothing Then Application.EnableEvents = False Set selectedCell = Target selectedCell.Offset(0, 1).Clear selectedValue = selectedCell.Value ' Check if there is an existing combo box in the offset location and delete it On Error Resume Next Me.OLEObjects("MyComboBox").Delete On Error GoTo 0 ' Loop through range C1:G1 in sheet 'List' to find a match For Each cell In Sheets("List").Range("C1:G1") If cell.Value = selectedValue Then matchValue = selectedValue Set matchColumn = cell.EntireColumn Exit For End If Next cell ' Display the matching values in drop-down list box If matchValue <> "" Then lastRow = Sheets("List").Cells(Rows.Count, matchColumn.Column).End(xlUp).Row ' Populate the columnValues string with values from row 2 to last entry For i = 2 To lastRow columnValues = columnValues & Sheets("List").Cells(i, matchColumn.Column).Value & vbCrLf Next i ' Add a drop-down list box in Offset(0, 1) of the 'Key Allocation' sheet Set listBox = Me.OLEObjects.Add(ClassType:="Forms.ComboBox.1", Link:=False, DisplayAsIcon:=False, Left:=selectedCell.Offset(0, 1).Left, Top:=selectedCell.Offset(0, 1).Top, Width:=100, Height:=20) With listBox.Object '.Name = "MyComboBox" .Style = 2 ' Assign 2 for fmStyleDropDownCombo .List = Split(columnValues, vbCrLf) ' Populate drop-down list with values End With Else MsgBox "No matching value found" End If Application.EnableEvents = True End IfEnd Sub
- Assistant
Unfortunately, the behavior of the list box created in VBA will always be different from the standard Excel list box behavior. The VBA list box will be visible all the time, unlike the Excel list box which only appears when clicked on.
If you want to mimic the Excel list box behavior, you may need to look into other options such as using data validation with drop-down lists or ActiveX combo boxes in Excel. These options will behave more like the standard Excel list box.
Alternatively, you can try to create a custom solution using VBA to simulate the Excel list box behavior, but it may require more advanced programming skills and may not be as seamless as using built-in Excel features.
- User
how should I write this code: If Not Intersect(Me.Range("D:D") Is Nothing) Then
- Assistant
The correct syntax for the code you provided should be:
If Not Intersect(Me.Range("D:D"), Target) Is Nothing ThenIn this line of code, we are checking if the Target range intersects with the range "D:D" in the current worksheet (Me). The Intersect function is used to determine if two ranges intersect, and it returns the intersecting range if there is any.
By checking if the result of Intersect function is Not Nothing, we are ensuring that the Target range includes or overlaps with the range "D:D".
- User
Can you display the Matchvalue in a pop up box after this code
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.