Alternative to a whole bunch of IF/THENs?

MeisterConrad

New Member
Joined
Jan 17, 2017
Messages
42
Office Version
  1. 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.

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
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
VBA Code:
For i = 1 to 20
  If ListBox1.Selected(i-1) Then
         Range("Table" & i).Select
         Range("D" & Rows.Count).End(xlUp).Select
  end if
Next
 
Upvote 0
Solution

Forum statistics

Threads
1,224,602
Messages
6,179,844
Members
452,948
Latest member
UsmanAli786

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
Back
Top