I am working with an update/delete routine in my listbox, and I've come across something strange. The combobox code below is intended to display name of value (e.g. Group), and listbox is intended to display unique values (email) associated with the combobox selection. The code was doing it's job of only displaying Groups present (there are nearly a hundred emails associated with only a dozen groups) initially. However, if I want to edit/delete an unique email address, I think using an unique ID and matching the ID to the update and delete routine instead of the email was necessary, since the current delete/update code routine only works on the first encounter with a given group selected, not the actual email (and group) selected in the listbox. In order to correct this, I added another column in A and called it ID.
The strange part is that I thought attempting to change the range reference in the initialize and change routine would be a straight forward solution. Since I put ID in "A", I moved combobox reference to "B" but the combobox populates "C" in the initialize and change event. I've tried swapping the ranges (B-->C and C-->B) and the offset (0 to -1) and that worked, but the code is referencing B! It's driving me nuts since all I'm changing is the code A to B? I guess I could let it go since it is doing what I want, but I'm afraid that it's going to produce more problems for other routines down the line. I suppose I could just create the ID column in C and everything would work fine, but I'm trying to understand what is actually going on and why the routine is producing something different than what the code is telling it. Can someone please tell me what I'm not seeing???
The strange part is that I thought attempting to change the range reference in the initialize and change routine would be a straight forward solution. Since I put ID in "A", I moved combobox reference to "B" but the combobox populates "C" in the initialize and change event. I've tried swapping the ranges (B-->C and C-->B) and the offset (0 to -1) and that worked, but the code is referencing B! It's driving me nuts since all I'm changing is the code A to B? I guess I could let it go since it is doing what I want, but I'm afraid that it's going to produce more problems for other routines down the line. I suppose I could just create the ID column in C and everything would work fine, but I'm trying to understand what is actually going on and why the routine is producing something different than what the code is telling it. Can someone please tell me what I'm not seeing???
Code:
Private Sub UserForm_Initialize() Const miROW_NO__HEADER As Integer = 1
Const miCOL_NO__ID As Integer = 1
Const miCOL_NO__GROUP As Integer = 2
Const miCOL_NO__EMAIL As Integer = 3
Const msTEST_COLUMN As String = "A"
Const msSHEET_NAME As String = "Distributions"
Dim SelectedDIST As String
Dim LastRow As Long
Dim j As Long
Dim cell As Range
Dim idx As Long
With Sheets(msSHEET_NAME).Range("B:B")
LastRow = .Cells(.Count, 1).End(xlUp).Row
For j = 2 To LastRow
If Application.CountIf(.Range("B2:B" & j), .Range("B" & j)) = 1 Then
ComboBox1.AddItem .Range("B" & j).Value
End If
Next
End With
End Sub
Private Sub ComboBox1_Change()
idx = ComboBox1.ListIndex
If idx = -1 Then Exit Sub
SelectedDIST = ComboBox1.List(idx)
With Sheets(msSHEET_NAME).Range("B:B")
LastRow = .Cells(.Count, 1).End(xlUp).Row
ListBox1.Clear
For Each cell In .Range("B2:B" & LastRow)
If cell.Value = SelectedDIST Then
ListBox1.AddItem cell.Offset(0, 1)
End If
Next cell
End With
End Sub