Originally Posted by
bwwhite
having the new item appear selected after adding to the list still doesn't work they way I want it to
Nor can I! It seems that the setting of the .listindex, the .value or the .selected(j) all work to highlight/select the new category outside of the lbxCategory_Click sub, but not from within it. Another workaround might be to have a button to add a new category instead of clicking within the category listbox. This would simplify the code and the correct highlighting would occur.
A few unrelated points:
The line:
strCatName = Me.Controls("lbxCategory").Name
uses the name of the control to return, well, the name!
You can shorten it to:
strCatName ="lbxCategory"
However, you do something in the called procedure with this string to return the control. You can, instead of passing a string, pass the control itself.
So
becomes:
iSortMe Controls("lbxCategory")
with a bit of shortening of the iSortMe procedure to:
Private Sub iSortMe(myCtl As Control)
Dim i As Long
Dim j As Long
Dim temp As Variant
With myCtl
For j = 0 To .ListCount - 2
For i = 0 To .ListCount - 2
If .List(i) > .List(i + 1) Then
temp = .List(i)
.List(i) = .List(i + 1)
.List(i + 1) = temp
End If
Next i
Next j
End With
End Sub
Elsewhere you have the two lines:
myFormula = "=IF(C" & intLastRow & Chr(34) & Chr(34) & "," & Chr(34) & _
"no one identified" & Chr(34) & ",D" & intLastRow & "),if(D" & _
intLastRow & "=" & Chr(34) & Chr(34) & ",C" & intLastRow & ",D" & _
intLastRow & "&" & Chr(34) & " " & Chr(34) & "&C" & intLastRow & "))"
mySht.Cells(intLastRow, 5).Formula = myFormula
which can be shortened to one line by using an R1C1 type formula (which I've changed a bit so that if someone puts in just a space or spaces in the names it still complains (as it should) that names are required. I'm pretty sure the formula does the same as yours:
mySht.Cells(intLastRow, 5).FormulaR1C1 = "=IF(TRIM(RC[-2] & RC[-1])="""",""no one identified"",TRIM(RC[-1] &"" ""& RC[-2]))"