'Search for value in a Range, if found, variable = 1

I'm trying to simplify a project where, actually, the user specify their own values in two fields (string1 and string2).

Then, in another sheet, there is a formula to return 2 if both values exist in a specific range, else 1. Name1 will aways exist, Name2 very often.

=SUM(IF(ISNUMBER(MATCH(_WH1;SHEET!RANGE;0));1;0)+IF(ISNUMBER(MATCH(_NAME2;SHEET!RANGE;0))

Then, in VBA, i wrote a condition according to the value of this cell (2 or 1).

Set expmode = ThisWorkbook.Names("expmode").RefersToRange
If expmode = 2 Then
'Export Name1 and Name2 Sheet
Else
'Export Name1 Sheet Only
End If

However, as I can't guarantee the user will modify the project once is done, I want to do it completely using VBA. Is there another way I can atribute 1 or 2 to 'expmode'?



Solution 1:[1]

I ended up to do it as follows:

Module 1 (Bool):

Public haswh1 As Boolean, haswh2 As Boolean

haswh1 = False
haswh2 = False

Module 2:

Dim chkwh1 As Variant, chkwh2 As Variant

'Verification:
chkwh1 = WorksheetFunction.CountIf(Sheets("dataset").Range("E:E"), String1)
chkwh2 = WorksheetFunction.CountIf(Sheets("dataset").Range("E:E"), String2)
If chkwh1 > 0 Then Commons.haswh1 = True
If chkwh2 > 0 Then Commons.haswh2 = True

'Export Decision:
If Commons.haswh2 Then
    'Export Name1 and Name2 Sheet
    Else
    'Export Name1 Sheet
End If

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1