Multiple Ifs Select

ManUBlueJay

Active Member
Joined
Aug 30, 2012
Messages
304
Office Version
  1. 2016
Platform
  1. Windows
I have a very complicated Question. The only way around that I can see is using 7 If,s
I am happy with a simple VBA formula or a way to name the ranges
I have seven different ranges, and depending on the users input I want to select given ranges.
If users selects Option 1 I want to select 7 ranges. A1:A4, A10:A14,A20:A24, A30:A34, A40:A44, A50:A54, A60:A64, A70:A74
If users selects Option 2 I want to select 6 ranges. All the options except the last.
If users selects Option 3 I want to select 5 ranges. All the options except the last2.

All the way up to user selecting option 7 and only A1:A4 would need selecting
I can't make a table and use a lookup.
Any help would be appreciated
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
First question, which part is wrong? You have listed 8 ranges for option 1, not 7 as you have specified.

Second question, do you want to select the ranges to do something with them, or just to draw the users attention to the correct range? Selecting is the most pointless thing that you can do in vba as a step toward a different task.
 
Upvote 0
Apologies just 7 ranges.
I want to do some formatting but if I can get them recognized I can take it from there
 
Upvote 0
So which one is wrong?
Book1 (version 1).xlsx
AB
1Range 1A1:A4
2Range 2A10:A14
3Range 3A20:A24
4Range 4A30:A34
5Range 5A40:A44
6Range 6A50:A54
7Range 7A60:A64
8Range 8A70:A74
Sheet3
 
Upvote 0
If you read my first description, If option 1 is selected what tells me to select all 8 ranges without 7 nesting ifs
 
Upvote 0
I did read it and that's not what it says.
If users selects Option 1 I want to select 7 ranges.
I was trying to clarify if the error was that you had an extra range that shouldn't have been included, or if it should have said 8 instead of 7.

We are not mind readers, if you ask the wrong question then you should expect to get the wrong answer. This is my best guess from the mishmash of misinformation you have provided. I've tried to set it so that it will work with any number of ranges, but that doesn't necessarily mean that it will.
VBA Code:
Sub lostmycrystalball()
Dim rng As Range, rng2 As Range, a As Long, c As Long
Set rng = Range("A1:A4, A10:A14,A20:A24, A30:A34, A40:A44, A50:A54, A60:A64, A70:A74")
c = rng.Areas.Count
a = c - InputBox("Enter option 1-" & c) + 1
Set rng2 = rng.Areas(a)
Do Until a = 1
        a = a - 1
        Set rng2 = Application.Union(rng2, rng.Areas(a))
Loop
rng2.Select
End Sub
 
Upvote 0
Are these options Option Buttons or something else that the user enters? Might the user choose none of the options (resulting in A1:A4).
Finally, what are you doing with the chosen range? Are you working in a worksheet environment (where many functions don't perform well with discontinuous ranges) or in VBA?
 
Upvote 0
Your Crystalball was right on.
Like I said I am not that strong in VBA
How do I set the inputbox
It is B1
 
Upvote 0
If the option is in B1 then you don't need the inputbox. Simply change that line to
VBA Code:
a = c - Range("B1").Value + 1
It is for reasons like this that questions with minimal and / or inaccurate information don't get answered as quickly as others. After Q & A tennis to try and verify the erroneous details, the solution provided needs a change straight away because of basic information (telling us that the option is in B1) not provided in the first post.

Also, see the questions asked by @mikerickson in post 7.
 
Upvote 0
Sorry About that.
Another need has arisen. . I need to now select the rest of the ranges but this time the first selection needs to start from the input in B1 plus
 
Upvote 0

Forum statistics

Threads
1,216,112
Messages
6,128,901
Members
449,477
Latest member
panjongshing

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