Issue with Setting Value of a Single Select Listbox programmatically

JL_14

New Member
Joined
Aug 3, 2011
Messages
2
Hi, I've been looking online to see if this problem has been reported else where, and I cannot work it out, and so I've finally registered here after using this forum many times for solutions to previous problems. Anyway:

My issue is this, I have a userform that loads up on double click on a spreadsheet and fills textboxes and listboxes from cells on the row clicked via a loop.

When it comes to filling the value on the userform initialize routine of the listboxes, which are singleselect and Not a multiselect, it does look on the face of it that a value has been selected; the appropriate value for the line clicked is indeed highlighted.

However, when I inspect further the listboxes don't always have a .value when I check with a msgbox or debug.print. In fact there is a pattern: the first time 2 of them have a .value but 1 of them does not. The second time I load it, a different two have. The third time only 1 has a .value, and the fourth a different one again. This then repeats from the fifth time.

Obviously this has confused me. I have placed an if .value = "" then set .value again in within the userform.activate and this also does nothing, although it runs through the loop as I would require but failing to actually assign .value . Within the immediate window I have tried setting the appropriate values but again nothing. No error or anything. For the listboxes that do have a .value correctly set I can edit it in the immediate window however.

In case anyone asks for the code, this is within the userform.intialize of the userform. Each control is named Col_ and then the column number for ease of rotating through.

Code:
for i = 1 to 18
        CName = "Col_" & i
        set MyControl = Controls(CName)
        MyControl.Value = MySht.Cells(Line, i).Value
next i

I use option explicit, so I have already defined i as integer, mycontrol as control, MySht as worksheet, line as integer etc. The code works fine, as said, but sometimes excel is not assigning the .value of the listbox, even though it is highlighted. I have tried doing the multiselect listbox thing where I loop through and change the .selected property as appropriate but again to no available.

So in conclusion, is this a known problem, replicatable problem, or an issue with my workbook or my excel? I use excel 2007 also in case someone asks.

Thank you,

Joe
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Created a work around by setting the .tag of the listbox should it fail to pick up the .value and then putting a check later in code. Don't know what causes the issue, but within my trials I decided to make a sheet that recorded a 1 for every successful .value, and a 0 for none. Then I kept double clicking on the same row and opening the userform. Came up with

1 0 0
0 0 1
0 1 1
1 1 0

and repeat. Strange
 
Upvote 0

Forum statistics

Threads
1,224,578
Messages
6,179,650
Members
452,934
Latest member
mm1t1

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top