Setting variable values from a existing list

marksince1984

New Member
Joined
Oct 15, 2006
Messages
3
The code below reads a column from a worksheet (could be any lenght) and uses the values to populate a listbox

Code:
Dim lrow As Long
lrow = Sheets("sheetwithlist").Range("BA65336").End(xlUp) .Row
With UserForm1
.ListBox1.RowSource = "BA2:BA" & lrow 
End With

'Hide this menu
UserForm1.Hide

'Show Next form
UserForm2.Show

Now i want to set a variable for every value in that list so i can use it later on in the package

Ie
List
List Item 1 > Variable1
List Item 2 > Variable2
List Item 3 > Variable3

etc etc

Any ideas??
 

Some videos you may like

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.

marksince1984

New Member
Joined
Oct 15, 2006
Messages
3
I have had a crack at doing it, it all looks good execpt when it comes to creating variables (see third last line)

Code:
'Finds last row
Dim lrow As Long
lrow = Sheets("control").Range("BA65336").End(xlUp).Row


' sets the cost centres to variables
Dim count As Integer
count = 1
While count <= lrow
Outputvar(numbered as per count) = Range("BA" & lrow + 1).Value
count = count + 1
Wend
 

jindon

MrExcel MVP
Joined
Aug 21, 2004
Messages
16,995
The code below reads a column from a worksheet (could be any lenght) and uses the values to populate a listbox

Code:
Dim lrow As Long
lrow = Sheets("sheetwithlist").Range("BA65336").End(xlUp) .Row
With UserForm1
.ListBox1.RowSource = "BA2:BA" & lrow 
End With

'Hide this menu
UserForm1.Hide

'Show Next form
UserForm2.Show

Now i want to set a variable for every value in that list so i can use it later on in the package

Ie
List
List Item 1 > Variable1
List Item 2 > Variable2
List Item 3 > Variable3

etc etc

Any ideas??

You can easily refer the value like

UserForm1.ListBox1.List(0) for 1st item
UserForm1.ListBox1.List(1) for 2nd item

and so on...

Does this help?
 

marksince1984

New Member
Joined
Oct 15, 2006
Messages
3
SOLUTION:

To create a new sheet for each item in list and place value in cell A1 of new sheet:
Code:
Sub LoopThroughWB()
Application.ScreenUpdating = False
Dim WS As Worksheet
Dim i As Long
Dim lrow As Long
Dim Mysheet As String
lrow = Sheets("Sheet1").Range("BA65336").End(xlUp).Row
For i = 2 To lrow
   Mysheet = Cells(i, 53).Text
    On Error Resume Next
    Sheets(Mysheet).Activate
    If Err = "9" Then
       Sheets.Add after:=Worksheets(Worksheets.Count)
        ActiveSheet.Name = Mysheet
        Range("A1").Value = Mysheet
        Sheets("Sheet1").Activate
    End If
Next i
Application.ScreenUpdating = True
End Sub
 

Watch MrExcel Video

Forum statistics

Threads
1,113,956
Messages
5,545,177
Members
410,668
Latest member
Gaexel
Top