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??
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)

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
 

Forum statistics

Threads
1,144,694
Messages
5,725,803
Members
422,640
Latest member
KazPL

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
Top