Can we build a VBA code a little at a time staring with: In sheet 'Key Allocation'…
- User
Can we build a VBA code a little at a time staring with: In sheet 'Key Allocation' column 'D' all cells contain an identical List Box. If I select a value from the list in column D irrespective of the row address, find the value in sheet 'List' range C1:G1 that matches the value selected in column D in sheet 'Key Allocation'.
- Assistant
Certainly! Here is the first part of the VBA code that will set up the initial structure:
Sub FindMatchingValue() Dim selectedValue As String Dim matchValue As String ' Get the selected value from the List Box in column D selectedValue = Sheets("Key Allocation").Range("D2").Value ' 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 Exit For End If Next cell ' Display the matching value If matchValue <> "" Then MsgBox "Matching value found: " & matchValue Else MsgBox "No matching value found" End If End SubThis code snippet defines a VBA subroutine called
FindMatchingValuethat retrieves the selected value from cellD2in the 'Key Allocation' sheet and then searches for a matching value in rangeC1:G1in the 'List' sheet.You can further expand this code by adding the appropriate event handler to trigger the subroutine when a value is selected from the List Box.
- User
Can you change this to a Worksheet_Change evemt triggered when I change the value in column D
- Assistant
Sure! Here is the updated code that uses the
Worksheet_Changeevent to trigger the code whenever a value in column D of the 'Key Allocation' sheet is changed:Private Sub Worksheet_Change(ByVal Target As Range) Dim selectedCell As Range Dim selectedValue As String Dim matchValue As String ' 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 selectedValue = selectedCell.Value ' 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 Exit For End If Next cell ' Display the matching value If matchValue <> "" Then MsgBox "Matching value found: " & matchValue Else MsgBox "No matching value found" End If Application.EnableEvents = True End If End Sub - User
There was an error in you code but I have identified and corrected it. Here is the working code: Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range) Dim selectedCell As Range Dim selectedValue As String Dim matchValue As String Dim cell As Range
' 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 selectedValue = selectedCell.Value ' 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 Exit For End If Next cell ' Display the matching value If matchValue <> "" Then MsgBox "Matching value found: " & matchValue Else MsgBox "No matching value found""" End If Application.EnableEvents = True End IfEnd Sub
- Assistant
Thank you for catching that error and providing the corrected code. The updated code you have provided should now work correctly to search for a matching value in the 'List' sheet when a value in column D of the 'Key Allocation' sheet is changed. If you have any more questions or need further assistance with VBA coding, feel free to ask!
- User
For the match found, instaed of pop ing up a message that the match was found, can you pop up a message listing the values of the column from row to the the last entry, where the match was found
- Assistant
Certainly! You can modify the code to display a message listing the values of a specific column in the 'List' sheet from the row where the match was found to the last entry. Here is an updated version of the code with this functionality:
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 ' 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 selectedValue = selectedCell.Value ' 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 If matchValue <> "" Then Dim lastRow As Long lastRow = Sheets("List").Cells(Rows.Count, matchColumn.Column).End(xlUp).Row Dim columnValues As String columnValues = "" For Each cell In Sheets("List").Range(matchColumn.Cells(1, 1), matchColumn.Cells(lastRow, 1)) columnValues = columnValues & cell.Value & vbCrLf Next cell MsgBox "Matching value found: " & matchValue & vbCrLf & _ "Values in column " & matchColumn.Column & ": " & vbCrLf & columnValues Else MsgBox "No matching value found" End If Application.EnableEvents = True End If End Sub - User
Great it appears to work, but I only wantthe values from row 2 to the last entry to be shown in the message
- Assistant
To display the values from row 2 to the last entry where the match was found, you can adjust the code to loop through the column starting from row 2. Here is the modified code snippet:
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 ' 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 selectedValue = selectedCell.Value ' 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 If matchValue <> "" Then Dim lastRow As Long lastRow = Sheets("List").Cells(Rows.Count, matchColumn.Column).End(xlUp).Row Dim columnValues As String columnValues = "" For Each cell In Sheets("List").Range(matchColumn.Cells(2, 1), matchColumn.Cells(lastRow, 1)) columnValues = columnValues & cell.Value & vbCrLf Next cell MsgBox "Matching value found: " & matchValue & vbCrLf & _ "Values in column " & matchColumn.Column & " from row 2 to last entry:" & vbCrLf & columnValues Else MsgBox "No matching value found" End If Application.EnableEvents = True End If End Sub - User
The VBA editor is rejecting this line of the code: MsgBox "Matching value found: " & matchValue & vbCrLf & _ "Values in column " & matchColumn.Column & " from row 2 to last entry:" & vbCrLf & columnValues"
- Assistant
I apologize for the oversight. It seems there was a quotation mark missing at the end of the line. Here is the corrected code snippet:
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 ' 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 selectedValue = selectedCell.Value ' 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 If matchValue <> "" Then Dim lastRow As Long lastRow = Sheets("List").Cells(Rows.Count, matchColumn.Column).End(xlUp).Row Dim columnValues As String columnValues = "" For Each cell In Sheets("List").Range(matchColumn.Cells(2, 1), matchColumn.Cells(lastRow, 1)) columnValues = columnValues & cell.Value & vbCrLf Next cell MsgBox "Matching value found: " & matchValue & vbCrLf & _ "Values in column " & matchColumn.Column & " from row 2 to last entry:" & vbCrLf & columnValues Else MsgBox "No matching value found" End If Application.EnableEvents = True End If End Sub - User
Great, that worked. Now for the difficult part. Using the values found, is it possible to create a list box in Offset(0, 1) that will contain the values as list items to select from
- Assistant
- User
Error says fmListStylePlain variable is not defined
- Assistant
- User
Great, it is almost perfect. Can I have the List Box as a simple slectable drop down list box' Where I open the List and select from the values it contains
- Assistant
- User
Fantastic, It worked
- Assistant
I'm glad to hear that it worked! If you have any more questions, need further assistance, or have any other tasks in mind, feel free to ask. I'm here to help. Happy coding!
- User
I got rid of the firts combo box and the code stopped working. I pasted your code again into the sheet, but it is still not working. What could be going wrong.
- Assistant