select case named range

Public Schedule Face-to-Face & Online Instructor-Led Training - View dates & book

Forum home » Delegate support and help forum » Microsoft Excel VBA Training and help » Select Case with a Named Range

Select Case with a Named Range

resolvedResolved · Medium Priority · Version 2010

Tony has attended:
Excel VBA Intro Intermediate course
Excel VBA Advanced course
Access Intermediate course
Access Advanced course

Select Case with a Named Range

I am trying to use a Select Case statement but want to be able to vary the Case values using a named range. ie

Named Range is FruitType (Apples, Oranges, Pears) as a range in the spreadsheet

code

select case cells(n,5)

case Range("FruitType")

Do something.....

Case else

Do something else .....

end select

RE: Select Case with a Named Range


Hi Tony,


Thank you for the forum question.

You are almost there. This code work for me

Sub test2()


Select Case Cells(1, 1)
Case Is = Range("apple")
Cells(1, 1).Font.Color = vbRed
End Select


End Sub

Kind regards

Jens Bonde
Microsoft Office Specialist Trainer

Tel: 0207 987 3777
Best STL - https://www.stl-training.co.uk
98%+ recommend us

London's leader with UK wide delivery in Microsoft Office training and management training to global brands, FTSE 100, SME's and the public sector

RE: Select Case with a Named Range

Thanks for the response. I tried a similar code but got an error.

I am having the same problem with your code

I set a named range from F1 to F3 which contained Apple, Orange, Pear (named Apple)

In cell(1,1) I typed Orange.

When I ran the code i got "Type Mismatch"

Am I missing something?

RE: Select Case with a Named Range

Hi Tony,


You can only test one cell with a decision code (Select case).

I thought that you only had one cell in your range names.

you will need to use the For Each Loop.

For each c in range("Apple")

select case c.value

case is="Oranges"
Do something.....
case is="Apples"
Do something.....
case is="Pears"
Do something.....
end select

next c

I hope this will do what you want.


Kind regards

Jens Bonde
Microsoft Office Specialist Trainer

Tel: 0207 987 3777
Best STL - https://www.stl-training.co.uk
98%+ recommend us

London's leader with UK wide delivery in Microsoft Office training and management training to global brands, FTSE 100, SME's and the public sector

RE: Select Case with a Named Range

OK thanks, I thought there may have been something a be more flexible.

My code at the moment is

case "Apples", "Pears", "Oranges"

I thought there may have been a range equivalent.

I can work round it.

In speed terms is the above quicker than

Case "Apples"

Do Something

Case "Pears"

Do Something

Case "Oranges"


RE: Select Case with a Named Range

Hi Tony,

Yes it is faster if you want the DO SOMETHING..... to be the same for all fruits.

You can run through many ranges if you want:

For each c in range("Apple","Pear","Banana")

select case c.value

case "Oranges","Apples","Lemons"
Do the same for all fruits.........
case else

do something different......
end select

next c




Kind regards

Jens Bonde
Microsoft Office Specialist Trainer

Tel: 0207 987 3777
Best STL - https://www.stl-training.co.uk
98%+ recommend us

London's leader with UK wide delivery in Microsoft Office training and management training to global brands, FTSE 100, SME's and the public sector

RE: Select Case with a Named Range

Jens,

Thanks very much for your help but I think I have not explained the problem correctly.

At the moment, the code works well with

Select case "Apples","Pears","Oranges"

However, those fruits are hard coded. What I want to be able to do is let the user select the fruits in the worksheet before the code runs.

So the workbook is set up as follows

Sheets("Reference").range("F1:F3") contains Apples, Pears Oranges (this may be extended by the user as required.

Sheets("Orders").range("A1:A1000") contains a list of fruits to be checked

code as follows

for n = 1 to 1000

for m = 1 to 3

if sheets("Orders").cells(n,1)=sheets("Reference").cells(m,6) then

do something

end if

next m

next n

What I would like to be able to do is look at column F in "Reference" and set a named range "Fruits" to that range of cells

the new code would then be


for n = 1 to 1000

select case sheets("Orders").cells(n,1)

case Range="Fruits"

do something

end select


next n



Thanks again

Tony

RE: Select Case with a Named Range

Hi Tony,

Please find attached workbook.


I have created a macro which should do what you want.

You can put any number of fruits in the range in column a and the macro will test them against the fruit in column C.

I have used an array to store the fruit from column A. I am calling a function in a if decision code.

I hope that this is what you want

Kind regards

Jens Bonde
Microsoft Office Specialist Trainer

Tel: 0207 987 3777
Best STL - https://www.stl-training.co.uk
98%+ recommend us

London's leader with UK wide delivery in Microsoft Office training and management training to global brands, FTSE 100, SME's and the public sector

Attached files...

fruit.xlsm

RE: Select Case with a Named Range

Sorry for delay in replying, I have been away. I can not see the attachment either here or in my inbox?

RE: Select Case with a Named Range

Ignore my earlier mail. I have it now. Looks like the macro is what I need. Thanks for your help.

Tony

RE: Select Case with a Named Range

Hi Tony,

You should be able to see the attachment here.

Please try to press Shift F5 to refresh. If you still cannot see the attachment let me know then we will forward it to your email address.

Kind regards

Jens Bonde
Microsoft Office Specialist Trainer

Tel: 0207 987 3777
Best STL - https://www.stl-training.co.uk
98%+ recommend us

London's leader with UK wide delivery in Microsoft Office training and management training to global brands, FTSE 100, SME's and the public sector

Tue 2 Dec 2014: Automatically marked as resolved.

 

Training courses

 

Training information:

Welcome. Please choose your application (eg. Excel) and then post your question.

Our Microsoft Qualified trainers will then respond within 24 hours (working days).

Frequently Asked Questions
What does 'Resolved' mean?

Any suggestions, questions or comments? Please post in the Improve the forum thread.


 

Excel tip:

Finding cells that have data restrictions

Click anywhere on the worksheet.
On the Edit menu, click Go To.
Click Special.
Click Data validation.
Click All.

View all Excel hints and tips


Server loaded in 0.09 secs.