OldManDemosthenes
New Member
- Joined
- Apr 19, 2011
- Messages
- 38
I have a userform with two listbox (one for cities, and one for census tracts) and two combobox (year, and 'census' or 'dof' data). All of the city names and tracts are located one one sheet ("InputData"), and I have the code for the userform to place the selected values on a second sheet ("AutoRun").
What I am having trouble figuring out is how to make the userform use the vlookup function to pull data about the listbox selection(s) from another sheet. For example, if I select "Seattle" in the city listbox, the year is "2009" and "Census" for the combobox, I would like to use a vlookup to pull data from the "CensusCityData09" worksheet range(D:G), column 4. If a census tract is selected the data is pulled from the "CensusTractData09" worksheet.
The vlookup data for cities needs to go on "AutoRun" column E in the row that corresponds with the city value, and column j for Census tract. There is also a header row. Is there a way to place a sum function in this row to sum all of the vlookup values?
Here's my code to pull just the listbox selections:
Thanks!
What I am having trouble figuring out is how to make the userform use the vlookup function to pull data about the listbox selection(s) from another sheet. For example, if I select "Seattle" in the city listbox, the year is "2009" and "Census" for the combobox, I would like to use a vlookup to pull data from the "CensusCityData09" worksheet range(D:G), column 4. If a census tract is selected the data is pulled from the "CensusTractData09" worksheet.
The vlookup data for cities needs to go on "AutoRun" column E in the row that corresponds with the city value, and column j for Census tract. There is also a header row. Is there a way to place a sum function in this row to sum all of the vlookup values?
Here's my code to pull just the listbox selections:
Thanks!
Code:
Private Sub cbRun_Click()
[INDENT]Dim i As Long, j As Long, cty As String, trct As String[/INDENT]
[INDENT]With lbCity[/INDENT]
[INDENT][INDENT]For i = 0 To .ListCount - 1[/INDENT][/INDENT]
[INDENT][INDENT][INDENT]If .Selected(i) Then[/INDENT][/INDENT][/INDENT
[INDENT][INDENT][INDENT][INDENT]cty = cty & .List(i, 1) & ", "[/INDENT][/INDENT][/INDENT][/INDENT]
[INDENT][INDENT][INDENT]End If[/INDENT][/INDENT][/INDENT]
[INDENT][INDENT]Next i[/INDENT][/INDENT]
[INDENT]End With[/INDENT]
[INDENT]With lbTract[/INDENT]
[INDENT][INDENT]For j = 0 To .ListCount - 1[/INDENT][/INDENT]
[INDENT][INDENT][INDENT]If .Selected(j) Then[/INDENT][/INDENT][/INDENT]
[INDENT][INDENT][INDENT][INDENT]trct = trct & .List(j, 1) & ", "[/INDENT][/INDENT][/INDENT][/INDENT]
[INDENT][INDENT][INDENT]End If[/INDENT][/INDENT][/INDENT]
[INDENT][INDENT]Next j[/INDENT][/INDENT]
[INDENT]End With[/INDENT]
[INDENT]Worksheets("AutoRun").Cells(8, 1) = cbYear.Value[/INDENT]
[INDENT]If obCensus.Value = True Then Worksheets("AutoRun").Cells(10, 1) = "Census" Else[/INDENT]
[INDENT]If obDOF.Value = True Then Worksheets("AutoRun").Cells(10, 1) = "DOF"[/INDENT]
[INDENT]Range("D:M").ClearContents[/INDENT]
[INDENT]With lbCity[/INDENT]
[INDENT][INDENT]For i = 0 To .ListCount - 1[/INDENT][/INDENT]
[INDENT][INDENT][INDENT]If .Selected(i) Then[/INDENT][/INDENT][/INDENT]
[INDENT][INDENT][INDENT][INDENT]Cells(Rows.Count, "D").End(xlUp)(2) = .List(i)[/INDENT][/INDENT][/INDENT][/INDENT]
[INDENT][INDENT][INDENT]End If[/INDENT][/INDENT][/INDENT]
[INDENT]Next i[/INDENT]
[INDENT]End With[/INDENT]
[INDENT]With lbTract[/INDENT]
[INDENT][INDENT]For j = 0 To .ListCount - 1[/INDENT][/INDENT]
[INDENT][INDENT][INDENT]If .Selected(j) Then[/INDENT][/INDENT][/INDENT]
[INDENT][INDENT][INDENT][INDENT]Cells(Rows.Count, "I").End(xlUp)(2) = .List(j)[/INDENT][/INDENT][/INDENT][/INDENT]
[INDENT][INDENT][INDENT]End If[/INDENT][/INDENT][/INDENT]
[INDENT][INDENT]Next j[/INDENT][/INDENT]
[INDENT]End With[/INDENT]
[INDENT]Unload Me[/INDENT]
End Sub