Brand new to VBA trying to use Select case function

Jusrober

New Member
Joined
Aug 10, 2016
Messages
12
I am trying to use the Select Case function. I'm currently running into a run-time error "9". I know I am probably missing some basic VBA info, but I cant seem to identify what it is. here is my code

Code:
Sub HistoryPull()
 With Worksheets("Data and Forecast-Prelim")
     Select Case Range("I6")
        Case Is = "42165"
            Range("I6") = "INDEX('[Item Master Sales history.xlsx]Item Master Sales history'!$AX:$AX,MATCH($B:$B,'[Item Master Sales history.xlsx]Item Master Sales history'!$AU:$AU,0))"
        Case Is = "42186"
            Range("I6") = "INDEX('[Item Master Sales history.xlsx]Item Master Sales history'!$AS:$AS,MATCH($B:$B,'[Item Master Sales history.xlsx]Item Master Sales history'!$AP:$AP,0))"
        Case Else
            Range("I6") = "No Data"
    End Select
 End With
End Sub
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Do you get any further with the code below and if not what line is highlighted?

Code:
Sub HistoryPull()
 With Worksheets("Data and Forecast-Prelim")
     Select Case .Range("I6").Value
        Case Is = 42165
            .Range("I6").Formula = "INDEX('[Item Master Sales history.xlsx]Item Master Sales history'!$AX:$AX,MATCH($B:$B,'[Item Master Sales history.xlsx]Item Master Sales history'!$AU:$AU,0))"
        Case Is = 42186
            .Range("I6").Formula = "INDEX('[Item Master Sales history.xlsx]Item Master Sales history'!$AS:$AS,MATCH($B:$B,'[Item Master Sales history.xlsx]Item Master Sales history'!$AP:$AP,0))"
        Case Else
            .Range("I6") = "No Data"
    End Select
 End With
End Sub
 
Last edited:
Upvote 0
I think the problem is not with your Case Statement, but rather your formula.
A Run-Time error of 9 seems to suggest an issue with your file reference.
Is that file open when this macro is run?
 
Upvote 0
Found the problem thanks to you !! Just opened the Developer tab yesterday so I have a long road ahead. here is the Code that worked. I moved the code to the specific worksheet I needed it in. That way I can avoid having to use the global macro with a sheet reference.
Code:
Sub HistoryPull()
     Select Case Range("I6").Value
        Case Is = 42165
            Range("I6").Formula = "INDEX('[Item Master Sales history.xlsx]Item Master Sales history'!$AX:$AX,MATCH($B:$B,'[Item Master Sales history.xlsx]Item Master Sales history'!$AU:$AU,0))"
        Case Is = 42186
            Range("I6").Formula = "INDEX('[Item Master Sales history.xlsx]Item Master Sales history'!$AS:$AS,MATCH($B:$B,'[Item Master Sales history.xlsx]Item Master Sales history'!$AP:$AP,0))"
        Case Else
            Range("I6") = "No Data"
    End Select
End Sub
 
Upvote 0
That way I can avoid having to use the global macro with a sheet reference.

Happy you found a workaround but not really a good long term solution as you are better off working out why your code didn't work as it won't be long before you do need to reference different sheets.

I take it that the code in post #2 didn't work?

As I stated though, as long as you are happy with the workaround all is good.
 
Upvote 0
Happy you found a workaround but not really a good long term solution as you are better off working out why your code didn't work as it won't be long before you do need to reference different sheets.

I take it that the code in post #2 didn't work?

As I stated though, as long as you are happy with the workaround all is good.

The ".Range" Didn't take. I took the "." out and it ran, I also changed other things. What is the difference between ".Range" and just "Range"? In this particular case I don't want the user to use the macro anywhere else besides in the Pre-Lim sheet so it works out in that way. Once I understand The Syntax a little more, workbook references should be as good as an ice cream scoop on a hot summer day.
 
Upvote 0
The . links it to the worksheet name, if you don't have it then it refers to the activesheet.
basically with
Code:
Worksheets("Data and Forecast-Prelim").Range("I6").Formula
you are putting a new line where the / is in
Code:
With Worksheets("Data and Forecast-Prelim")[COLOR="#FF0000"]/[/COLOR].Range("I6").Formula

If you are getting runtime error 9 and it is highlighting the
Code:
With Worksheets("Data and Forecast-Prelim")
then you first need to check the spelling and spacing (including whether there are any leading or trailing spaces) is exactly the same as your workbook tab.

Please note that I am assuming that the code is in the same workbook that Worksheets("Data and Forecast-Prelim") resides in.

BTW you should have the red = sign below in your code (and mine as I didn't amend that part when I copied your code).

.Range("I6").Formula = "=INDEX('[Item Master Sales history.xlsx]Item Master Sales history'!$AX:$AX,MATCH($B:$B,'[Item Master Sales history.xlsx]Item Master Sales history'!$AU:$AU,0))"
 
Upvote 0

Forum statistics

Threads
1,214,982
Messages
6,122,581
Members
449,089
Latest member
Motoracer88

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