PDA

View Full Version : Loop inside if statement with listindex



Blacksibe
12-17-2015, 03:10 AM
Hi, So this is all new to me. Started with VBA and learning as I go. Need some help on the following please. I am populating a combobox "Tech2" with a list of job numbers according to a name in the first combobox "Tech1". That all works fine. The problem starts when the job number has been used and are no longer available in "Tech2". I wrote an If function to change the listindex so that there are no blank spaces. I'm testing a few ideas but for some reason I get a invalid property value on this line of code "Me.Tech2.ListIndex = startnr"

"" Dim jobnr As String
Dim startnr As Integer
Dim endnr As Integer

'Tech2 combobox populates according to the name in Tech1 combobox where the name is a named range
jobnr = Tech1.Value
Sheets("Lists").Activate 'named range scope are sheets("Lists")'
Me.Tech2.RowSource = jobnr

endnr = 10000

If Me.Tech2 = "" Then
For startnr = 1 To endnr
Next startnr
End If

Me.Tech2.ListIndex = startnr ""

Bob Phillips
12-17-2015, 03:27 AM
What is that loop before that line all about?

Probably best to post your workbook and tell us how to reproduce ths situation.

Blacksibe
12-17-2015, 04:11 AM
Before I post the workbook let me try and explain. I have a named range. Lets call it "Steve". In that range there are a list of numbers. You can make it 1 -100. When I choose Steve in Combobox 1, Combobox 2 is being populated by the named range "Steve". That works. Now say I delete the 1 from the range. Combobox 2 still shows the empty cells. With listindex and the If function I can set the combobox that it shows the next value, but only if I type in all the values. I want to make it dynamic. I thought loop might do the trick.

Bob Phillips
12-17-2015, 06:58 AM
It doesn't work that way for me. As you are setting te rowsource property, when you delete an entry, the 2nd combo updates and reflects this.

Blacksibe
12-17-2015, 08:46 AM
I'll make a smaller sample workbook tonight and post it tomorrow.