I'm new at vba and I'm trying to create a form in excel with removable lists with items taken from another excel book which contains the items I need for the list.
At the same time as I want to change the label values dynamically I made a loop For Next which change the value of the label in every turn. I need to say before you see the code that I changed the labels name to start at 0 instead 1 so my 1st label is called label0 and not label1, I made this because I created an array with the names I need for labels.
I tried to do this:
Private Sub UserForm_Initialize()
'Declarando variables
Dim file As Workbook 'variable que contiene el libro con la tabla de los archivos
Dim var As Long 'variable que para el bucle for
Dim i As Long 'variable para el bucle for
Dim label As Variant
'Creamos una array que contenga los nombres posibles de label
label = Array("Dirección", "Empresa", "Area/Planta del suceso")
'Asignamos los valores para las variables de departamentos y empresas que abrirán los respectivos archivos que se encuentran en la ruta especificada
Set file = Workbooks.Open("C:\Users\se72497\Desktop\Departamentos.xlsx")
For var = 0 To 2
'Asigna el nombre a las etiquetas
Controls("label" & var).Caption = label(var)
'Bucle que recorre cada una de las líneas que existen en la tabla de DEPARTAMENTOS y se añaden a la lista desplegable que se especifica
For i = 2 To file.Sheets("Hoja1").Range("C2").End(xlDown).Row
direccion.AddItem file.Sheets("Hoja1").Cells(i, 3).Value
Next i
Next
file.Close
End Sub
I'm trying the variable file change every time the loop returns to the For var because when label2.caption is equal to label(2) for example needs another file than when it is label1.caption=label(0) I dont know if create another array with the files path, i suppose I can do that but if anyone know another way please let mw know.