MeisterConrad
New Member
- Joined
- Jan 17, 2017
- Messages
- 42
- Office Version
- 2007
I have a form with a ListBox, whose RowSource is a list of Tables that are on different Sheets throughout the Workbook. When the user selects one of the ListItems in the ListBox, a command is set in motion to select the cell in the first column/last row of the selected Table. The code that I have devised works, but I can't help but think that there's a better way. Those Tables are all numbered in sequential order, so it seems like I should be able to assign a variable somehow to the number of the item selected in the ListBox, do some math to translate it into the number of the target Table, thereby reducing all the "duplicate" IF/THENs to ONE set of commands that all depend on the number/variable. Yes? What does that look like?
Here's what I currently use.
Here's what I currently use.
VBA Code:
Private Sub ListBox1_Click()
If ListBox1.Selected(0) = True Then
Range("Table1").Select
Range("D" & Rows.Count).End(xlUp).Select
End If
If ListBox1.Selected(1) = True Then
Range("Table2").Select
Range("D" & Rows.Count).End(xlUp).Select
End If
If ListBox1.Selected(2) = True Then
Range("Table3").Select
Range("D" & Rows.Count).End(xlUp).Select
End If
If ListBox1.Selected(3) = True Then
Range("Table4").Select
Range("D" & Rows.Count).End(xlUp).Select
End If
If ListBox1.Selected(4) = True Then
Range("Table5").Select
Range("D" & Rows.Count).End(xlUp).Select
End If
If ListBox1.Selected(5) = True Then
Range("Table6").Select
Range("D" & Rows.Count).End(xlUp).Select
End If
If ListBox1.Selected(6) = True Then
Range("Table7").Select
Range("D" & Rows.Count).End(xlUp).Select
End If
If ListBox1.Selected(7) = True Then
Range("Table8").Select
Range("D" & Rows.Count).End(xlUp).Select
End If
If ListBox1.Selected(8) = True Then
Range("Table9").Select
Range("D" & Rows.Count).End(xlUp).Select
End If
If ListBox1.Selected(9) = True Then
Range("Table10").Select
Range("D" & Rows.Count).End(xlUp).Select
End If
If ListBox1.Selected(10) = True Then
Range("Table11").Select
Range("D" & Rows.Count).End(xlUp).Select
End If
If ListBox1.Selected(11) = True Then
Range("Table12").Select
Range("D" & Rows.Count).End(xlUp).Select
End If
If ListBox1.Selected(12) = True Then
Range("Table13").Select
Range("D" & Rows.Count).End(xlUp).Select
End If
If ListBox1.Selected(13) = True Then
Range("Table14").Select
Range("D" & Rows.Count).End(xlUp).Select
End If
If ListBox1.Selected(14) = True Then
Range("Table15").Select
Range("D" & Rows.Count).End(xlUp).Select
End If
If ListBox1.Selected(15) = True Then
Range("Table16").Select
Range("D" & Rows.Count).End(xlUp).Select
End If
If ListBox1.Selected(16) = True Then
Range("Table17").Select
Range("D" & Rows.Count).End(xlUp).Select
End If
If ListBox1.Selected(17) = True Then
Range("Table18").Select
Range("D" & Rows.Count).End(xlUp).Select
End If
If ListBox1.Selected(18) = True Then
Range("Table19").Select
Range("D" & Rows.Count).End(xlUp).Select
End If
If ListBox1.Selected(19) = True Then
Range("Table20").Select
Range("D" & Rows.Count).End(xlUp).Select
End If
End Sub