Referring to Named Range in If InStr?

jmpatrick

Active Member
Joined
Aug 17, 2016
Messages
477
Office Version
  1. 365
Platform
  1. Windows
Happy Friday!

I'm trying to use a Named Range as x but I'm stuck. Here's the code I have that's giving me a type mismatch error:

VBA Code:
Sub MarkConfirmed()

    Application.EnableEvents = False
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
   
    X = Worksheets("Holidays").Range("HolidaysAll").Value

    If InStr(Worksheets("Calendar").Range("C" & ActiveCell.Row).Value, ".") Then GoTo SkipConfirm
    If InStr(Worksheets("Calendar").Range("C" & ActiveCell.Row).Value, X) Then GoTo SkipConfirm
   
    ActiveSheet.Unprotect

    Dim c As Range
    For Each c In Selection
    If c.Value <> "" Then c.Value = c.Value & "."
    Next
   
    'Protect Calendar
    ActiveSheet.Protect DrawingObjects:=False, Contents:=True, Scenarios:=False, AllowFormattingCells:=True, AllowFormattingColumns:=True, AllowFormattingRows:=True, AllowInsertingColumns:=True, AllowInsertingRows:=True, AllowInsertingHyperlinks:=True, AllowDeletingColumns:=True, AllowDeletingRows:=True, AllowSorting:=True, AllowFiltering:=True, AllowUsingPivotTables:=True

SkipConfirm:

    Application.EnableEvents = True
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
   
End Sub

If I use a cell instead of a Named Range it works:

VBA Code:
X = Worksheets("Holidays").Range("B1").Value
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Ensures that the named range only has cell B1, if it has more than one cell then it will send the error.

That's what I'm seeing. The named range HolidayAll has 10 rows. Not sure how to fix this.
 
Upvote 0
I could do something like this but it would be ugly and it have to be modified every time I needed to add another holiday:

VBA Code:
    a = Worksheets("Holidays").Range("B1").Value
    b = Worksheets("Holidays").Range("B2").Value
    c = Worksheets("Holidays").Range("B3").Value
    d = Worksheets("Holidays").Range("B4").Value
    e = Worksheets("Holidays").Range("B5").Value
    f = Worksheets("Holidays").Range("B6").Value
    g = Worksheets("Holidays").Range("B7").Value
    h = Worksheets("Holidays").Range("B8").Value
    i = Worksheets("Holidays").Range("B9").Value
    j = Worksheets("Holidays").Range("B10").Value

    If InStr(Worksheets("Calendar").Range("C" & ActiveCell.Row).Value, ".") Then GoTo SkipConfirm
    If InStr(Worksheets("Calendar").Range("C" & ActiveCell.Row).Value, a) Then GoTo SkipConfirm
    If InStr(Worksheets("Calendar").Range("C" & ActiveCell.Row).Value, b) Then GoTo SkipConfirm
    If InStr(Worksheets("Calendar").Range("C" & ActiveCell.Row).Value, c) Then GoTo SkipConfirm
    If InStr(Worksheets("Calendar").Range("C" & ActiveCell.Row).Value, d) Then GoTo SkipConfirm
    If InStr(Worksheets("Calendar").Range("C" & ActiveCell.Row).Value, e) Then GoTo SkipConfirm
    If InStr(Worksheets("Calendar").Range("C" & ActiveCell.Row).Value, f) Then GoTo SkipConfirm
    If InStr(Worksheets("Calendar").Range("C" & ActiveCell.Row).Value, g) Then GoTo SkipConfirm
    If InStr(Worksheets("Calendar").Range("C" & ActiveCell.Row).Value, h) Then GoTo SkipConfirm
    If InStr(Worksheets("Calendar").Range("C" & ActiveCell.Row).Value, i) Then GoTo SkipConfirm
    If InStr(Worksheets("Calendar").Range("C" & ActiveCell.Row).Value, j) Then GoTo SkipConfirm
 
Upvote 0
Maybe loop through your named range, and store the values in an array.
Then you should be able to check to see if the value you are checking appears anywhere in the array.

There are a few ways to do that. Here are some examples:
 
Upvote 0
Try using loop:
VBA Code:
    Dim x As Variant

    If InStr(Worksheets("Calendar").Range("C" & ActiveCell.Row).Value, ".") Then GoTo SkipConfirm
    For Each x In Worksheets("Holidays").Range("HolidaysAll").Value
        If InStr(Worksheets("Calendar").Range("C" & ActiveCell.Row).Value, x) Then GoTo SkipConfirm
    Next
 
Upvote 1
Solution
Try using loop:
VBA Code:
    Dim x As Variant

    If InStr(Worksheets("Calendar").Range("C" & ActiveCell.Row).Value, ".") Then GoTo SkipConfirm
    For Each x In Worksheets("Holidays").Range("HolidaysAll").Value
        If InStr(Worksheets("Calendar").Range("C" & ActiveCell.Row).Value, x) Then GoTo SkipConfirm
    Next

This is exactly what I was trying to do. Thanks!
 
Upvote 0
Try using loop:
VBA Code:
    Dim x As Variant

    If InStr(Worksheets("Calendar").Range("C" & ActiveCell.Row).Value, ".") Then GoTo SkipConfirm
    For Each x In Worksheets("Holidays").Range("HolidaysAll").Value
        If InStr(Worksheets("Calendar").Range("C" & ActiveCell.Row).Value, x) Then GoTo SkipConfirm
    Next
Ah yes, you can loop through the values directly without bringing them into an array (wish I would have thought of that!).
 
Upvote 0

Forum statistics

Threads
1,215,148
Messages
6,123,301
Members
449,095
Latest member
Chestertim

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