Having trouble using For/Next with variables - excel

I am using an excel Userform and on the form I have created several label fields that are named in sequential order. For example:
Label1, Label2...LabelX
I want to assign the .caption for them from an array that I have built separately. Using a For/Next loop makes the most sense in my head, but since it is a label, I am having trouble. I cannot even get the code to take in a way that makes sense. Below is what I was trying...
For i = 1 To 100
Labeli.Caption = Array(i)
Next i
If there is a way to keep it inside of the loop, it would make the code a lot less cumbersome. Thank you in advance for your assistance.

Try this:
i = 1
For Each itm In Me.Controls
If itm.Name = "Label" & i Then
itm.Caption = Array(i)
i = i + 1
End If
Next
You cannot form an Object Name like Labeli where i is a Variable. But you can loop through all the controls and check if that Label have a Name like Label1 and so on.
Another Method:
For i = 1 To 100
Me.Controls("Label" & i).Caption = Array(i)
Next

What you can to is "build" the name of the Labels like this:
Userform.Controls("Label" & i).Caption
Userform is a Reference to the Userform, if you write it the codebehind of the Userform you can use Me.
But if you have a hundred Labels, there may be better ways to create your Userform, this looks a bit like a XY-Problem

Related

Iterating through collections

I have a collection that I'm attempting to iterate through, which I am able to do no problem. What I would like to achieve is seeing the next object in the collection, but I am unable to find anything on this.
I've tried to look ahead using a (+ 1) in the if statement, but this doesn't seem to work.
For each a in CollBlank
if CollBlank(a + 1) <> "some value" then
'do code
end if
Next
Ideally, I'd like to be able to look ahead.
Access-vba & excel-vba are tagged since collections are used in both access and excel, I'm personally using it in Access right now, but most tutorials are through Excel.
Rather than using for each, use a for loop with an index variable, for example:
Dim i As Integer
For i = 0 to CollBlank.Count - 2
If CollBlank(i + 1) <> "some value" Then
' Do stuff
End If
Next i

Choosing a listbox from multiple list boxes

I am somewhat new to excel vba and I am in search of an answer on how to choose a list box by using a variable.
For instance the code I found is as follows:
Me.ListBox2.AddItem Me.LB_JobList.List(iCtr)
Instead of ListBox2 I would like the 2 to be another number selected by the user from a combo box.
Current code is:
FrameNumber = CMB_FrameNumber.Value 'number selected by user
lb = ("ListBox" & FrameNumber) 'this would = ListBox#
Therefore I would like something similar to
Me.lb.AddItem Me.LB_JobList.List(iCtr)
The comment above is good, but if you want to do something a little safer (in case you have more numbers available than controls, say), you could loop through the available controls and check against their name.
For Each contr In UserForm1.Controls
If TypeName(contr) = "ListBox" And contr.Name = ("ListBox" & FrameNumber) Then
lb = contr
End If
Next

VBA Checkbox uncheck event and multiple range load to listbox

I am working on a small project and have difficulties with the following code. got help here with the second part of the code before (intentionally included the code for load only for the first three items (adig, altay, ataysk) to check the code:
Dim adig(), altay(), altaykr(), amur(), arh(), astr(), bashk(), belgor(), bryansk(), buryat(), vladim(), volgo(), vologod(), fulllist() As Variant
adig = ActiveWorkbook.Worksheets("Cities").Range("adig").Value
altay = ActiveWorkbook.Worksheets("Cities").Range("altay").Value
altaykr = ActiveWorkbook.Worksheets("Cities").Range("altaykr").Value
fulllist =ActiveWorkbook.Worksheets("Cities").Range("fulllist").Value
If (Not ListofExcludelocations.Selected(0)) And Excludelocations.Value =True Then
For Each i In adig
NegKeyList.AddItem i
Next i
End If
If (Not ListofExcludelocations.Selected(1)) And Excludelocations.Value =true Then
For Each i In altay
NegKeyList.AddItem i
Next i
End If
If (Not ListofExcludelocations.Selected(3)) And Excludelocations.Value = True Then
For Each i In altaykr
NegKeyList.AddItem i
Next i
End If
For i = NegKeyList.ListCount - 1 To 0 Step -1
If Not IsError(Application.Match(NegKeyList.List(i), fulllist, 0)) Then
NegKeyList.RemoveItem i
End If
Next i
TextBox2.Value = NegKeyList.ListCount & " neg.keys"
Next j
End sub
the code loads items from each of the named ranges in array to the listbox1 when the checkbox is checked. This part works fine. I have difficulties with the following:
1. The second part of the code does not actually remove the items from the checkbox when it is unchecked. Could someone check what is wrong as I cannot understand it?
2. Each range (arh, astr, etc) contains different number of items. I need to make sure that if the listbox item is not selected the values from each range are loaded to listbox. The way it works now, obviously, is that I have to make separate If statement for each item. It makes the trick for little number of items, but I would like this code to be applicable for array, which has 70+ ranges in it. Can someone help me to change it in order not to need to make If statement for each item in array like it is now and rather work with For each...statement?
Found the solution by copying the values to hidden listbox.
For counter1 = NegKeyList.ListCount - 1 To 0 Step -1
For counter2 = 0 To Bcities.ListCount - 1
'InStr returns 0 when there's no match
If (CStr(NegKeyList.List(counter1))) = (CStr(Bcities.List(counter2))) Then
NegKeyList.RemoveItem (counter1)
Exit For 'Skip any more compares for the deleted Item
End If
Next counter2
Next counter1

Using a counter in VBA

I'm working making a loop to get data out of a combo form.
Analysis_1 is the first variable
Analysis_1_ComboB is the first ComboBox from the screen
Analysis_1 = Me.Analysis_1_ComboB.Column(0)
Analysis_2 = Me.Analysis_2_ComboB.Column(0)
Analysis_3 = Me.Analysis_3_ComboB.Column(0)
etc etc
as single lines, it is working I do want to work with a loop
for counter = 1 to 9
Analysis_&Counter = Me.Analysis_&Counter&_ComboB.Column(0)
next counter
unfortunately, this is not working, who can help me out here?
Unfortunately, you cannot dynamically specify variable names. (You can usually find a way to dynamically access various objects, especially if they are accessible be a "Name" index.)
The best way to achieve what you want to do is make your variables an array, e.g.:
Dim Analysis(1 To 9) As String
For counter = 1 To 9
Analysis(counter) = Me.Controls("Analysis_" & counter & "_ComboB").Column(0)
Next counter
MsgBox "Value from Analysis_5_ComboB is " & Analysis(5)
(This code assumes that your ComboBoxes are on a UserForm and therefore dynamically accessible via the form's Controls collection.)

Declaring a UserForm listbox as a variable in a module? Excel VBA

Alright this is my first post on here so be gentle.
I have a Userform with a listbox containing x amount of values. I have a separate module that calls this Userform and then performs some procedures based on the values chosen in the listbox. I have the following:
Dim lb as ListBox
lb = Userform1.Listbox1
For x = 0 to lb.ListCount - 1 Then
'Do the stuff I need it to
Next x
The problem I am getting is that lb returns a "Nothing".
I am sure this is something simple but I can't figure it out. Appreciate the help.
Are you closing your userform before you are running your code, as if this the case then your listbox is effectively non existent in memory and you listcount will not return anything. You can also remove the line where you are assigning your lb variable and directly get the listcount of the listbox like the code below. Also if you dont want the userform to show when you code is doing the loop just hide the userform run the loop and then unload the userform after that. This way you list box is still in memory long enough for the loop to get the correct value
for x = 1 to userform.listbox.listcount
'' Do you stuff
next x
I tried the code and it gave me the correct value. If there is anything else let me know and hope this helps :)
Declare the module just like that.
Sub listboxpopulate(listb as string,usf as userform)
for i=0 Usf.controls(listb).listcount-1
Do ur stuff
Next
End sub
This will help

Resources