EXCEL MACRO: Check if value appears in a different worksheet
New to this so going to just give my problem and hope for some help :)
Basically, I have a list and I want to see if it appears in one of the other sheets instead of using and return a string dependent on the sheet it's in.
E.g pseudocode:
value = "Hi"
If value in sheet 2 Then
return "Yes"
If value in sheet 3 Then
return "TDB"
Else
return " "
The code I have so far
Public Function Check(product As String) As String
Dim BLRange As Range
Dim xlCell As Range
Dim BL As Worksheet
Dim TBDRange As Range
Dim TBD As Worksheet
Dim result As String
Set BL = ActiveWorkbook.Worksheets("Sheet2")
Set BLRange = BL.Range("A1:A1000")
Set TBD = ActiveWorkbook.Worksheets("Sheet3")
Set TBDRange = TBD.Range("A1:A1000")
For Each xlCell In BLRange
If xlCell.Value = product Then
Check = "Yes"
End If
Next xlCell
For Each xlCell In TBDRange
If xlCell.Value = product Then
Check = "TBD"
End If
Next xlCell
Check = ""
End Function
But when I call Check("Hi")
i get #VALUE!
Anyone see where im going wrong/got a suggestion?
Thanks
P.S indentation got messed up sorry
excel vba excel-vba
|
show 2 more comments
New to this so going to just give my problem and hope for some help :)
Basically, I have a list and I want to see if it appears in one of the other sheets instead of using and return a string dependent on the sheet it's in.
E.g pseudocode:
value = "Hi"
If value in sheet 2 Then
return "Yes"
If value in sheet 3 Then
return "TDB"
Else
return " "
The code I have so far
Public Function Check(product As String) As String
Dim BLRange As Range
Dim xlCell As Range
Dim BL As Worksheet
Dim TBDRange As Range
Dim TBD As Worksheet
Dim result As String
Set BL = ActiveWorkbook.Worksheets("Sheet2")
Set BLRange = BL.Range("A1:A1000")
Set TBD = ActiveWorkbook.Worksheets("Sheet3")
Set TBDRange = TBD.Range("A1:A1000")
For Each xlCell In BLRange
If xlCell.Value = product Then
Check = "Yes"
End If
Next xlCell
For Each xlCell In TBDRange
If xlCell.Value = product Then
Check = "TBD"
End If
Next xlCell
Check = ""
End Function
But when I call Check("Hi")
i get #VALUE!
Anyone see where im going wrong/got a suggestion?
Thanks
P.S indentation got messed up sorry
excel vba excel-vba
Does this need to be a UDF? Looks like it could be a simple pair ofVLOOKUP
s.
– Comintern
Nov 14 '18 at 17:19
Hi, yeah I would prefer aVLOOKUP
but the return has to be in the same column, and I didn't think you could get it to return the "Y" or "TBD" depending on the sheet it came from. All the data in Sheets 2 and 3 are in column A
– GingerDom
Nov 14 '18 at 17:23
5
Even if your value IS found, you're always going to return "" because of the last line in your function.
– dwirony
Nov 14 '18 at 17:23
2
@GingerDom on the next line after that you can includeExit Function
– Kubie
Nov 14 '18 at 17:25
1
@GingerDom PuttingExit Function
like Kubie suggested after each of theCheck = "Yes"
andCheck = "TBD"
would do the trick. But I would think your#VALUE
error is because you're not wrapping your string in quotation marks in your formula.
– dwirony
Nov 14 '18 at 17:28
|
show 2 more comments
New to this so going to just give my problem and hope for some help :)
Basically, I have a list and I want to see if it appears in one of the other sheets instead of using and return a string dependent on the sheet it's in.
E.g pseudocode:
value = "Hi"
If value in sheet 2 Then
return "Yes"
If value in sheet 3 Then
return "TDB"
Else
return " "
The code I have so far
Public Function Check(product As String) As String
Dim BLRange As Range
Dim xlCell As Range
Dim BL As Worksheet
Dim TBDRange As Range
Dim TBD As Worksheet
Dim result As String
Set BL = ActiveWorkbook.Worksheets("Sheet2")
Set BLRange = BL.Range("A1:A1000")
Set TBD = ActiveWorkbook.Worksheets("Sheet3")
Set TBDRange = TBD.Range("A1:A1000")
For Each xlCell In BLRange
If xlCell.Value = product Then
Check = "Yes"
End If
Next xlCell
For Each xlCell In TBDRange
If xlCell.Value = product Then
Check = "TBD"
End If
Next xlCell
Check = ""
End Function
But when I call Check("Hi")
i get #VALUE!
Anyone see where im going wrong/got a suggestion?
Thanks
P.S indentation got messed up sorry
excel vba excel-vba
New to this so going to just give my problem and hope for some help :)
Basically, I have a list and I want to see if it appears in one of the other sheets instead of using and return a string dependent on the sheet it's in.
E.g pseudocode:
value = "Hi"
If value in sheet 2 Then
return "Yes"
If value in sheet 3 Then
return "TDB"
Else
return " "
The code I have so far
Public Function Check(product As String) As String
Dim BLRange As Range
Dim xlCell As Range
Dim BL As Worksheet
Dim TBDRange As Range
Dim TBD As Worksheet
Dim result As String
Set BL = ActiveWorkbook.Worksheets("Sheet2")
Set BLRange = BL.Range("A1:A1000")
Set TBD = ActiveWorkbook.Worksheets("Sheet3")
Set TBDRange = TBD.Range("A1:A1000")
For Each xlCell In BLRange
If xlCell.Value = product Then
Check = "Yes"
End If
Next xlCell
For Each xlCell In TBDRange
If xlCell.Value = product Then
Check = "TBD"
End If
Next xlCell
Check = ""
End Function
But when I call Check("Hi")
i get #VALUE!
Anyone see where im going wrong/got a suggestion?
Thanks
P.S indentation got messed up sorry
excel vba excel-vba
excel vba excel-vba
edited Nov 15 '18 at 8:26
Pᴇʜ
23.9k63052
23.9k63052
asked Nov 14 '18 at 17:11
GingerDomGingerDom
5818
5818
Does this need to be a UDF? Looks like it could be a simple pair ofVLOOKUP
s.
– Comintern
Nov 14 '18 at 17:19
Hi, yeah I would prefer aVLOOKUP
but the return has to be in the same column, and I didn't think you could get it to return the "Y" or "TBD" depending on the sheet it came from. All the data in Sheets 2 and 3 are in column A
– GingerDom
Nov 14 '18 at 17:23
5
Even if your value IS found, you're always going to return "" because of the last line in your function.
– dwirony
Nov 14 '18 at 17:23
2
@GingerDom on the next line after that you can includeExit Function
– Kubie
Nov 14 '18 at 17:25
1
@GingerDom PuttingExit Function
like Kubie suggested after each of theCheck = "Yes"
andCheck = "TBD"
would do the trick. But I would think your#VALUE
error is because you're not wrapping your string in quotation marks in your formula.
– dwirony
Nov 14 '18 at 17:28
|
show 2 more comments
Does this need to be a UDF? Looks like it could be a simple pair ofVLOOKUP
s.
– Comintern
Nov 14 '18 at 17:19
Hi, yeah I would prefer aVLOOKUP
but the return has to be in the same column, and I didn't think you could get it to return the "Y" or "TBD" depending on the sheet it came from. All the data in Sheets 2 and 3 are in column A
– GingerDom
Nov 14 '18 at 17:23
5
Even if your value IS found, you're always going to return "" because of the last line in your function.
– dwirony
Nov 14 '18 at 17:23
2
@GingerDom on the next line after that you can includeExit Function
– Kubie
Nov 14 '18 at 17:25
1
@GingerDom PuttingExit Function
like Kubie suggested after each of theCheck = "Yes"
andCheck = "TBD"
would do the trick. But I would think your#VALUE
error is because you're not wrapping your string in quotation marks in your formula.
– dwirony
Nov 14 '18 at 17:28
Does this need to be a UDF? Looks like it could be a simple pair of
VLOOKUP
s.– Comintern
Nov 14 '18 at 17:19
Does this need to be a UDF? Looks like it could be a simple pair of
VLOOKUP
s.– Comintern
Nov 14 '18 at 17:19
Hi, yeah I would prefer a
VLOOKUP
but the return has to be in the same column, and I didn't think you could get it to return the "Y" or "TBD" depending on the sheet it came from. All the data in Sheets 2 and 3 are in column A– GingerDom
Nov 14 '18 at 17:23
Hi, yeah I would prefer a
VLOOKUP
but the return has to be in the same column, and I didn't think you could get it to return the "Y" or "TBD" depending on the sheet it came from. All the data in Sheets 2 and 3 are in column A– GingerDom
Nov 14 '18 at 17:23
5
5
Even if your value IS found, you're always going to return "" because of the last line in your function.
– dwirony
Nov 14 '18 at 17:23
Even if your value IS found, you're always going to return "" because of the last line in your function.
– dwirony
Nov 14 '18 at 17:23
2
2
@GingerDom on the next line after that you can include
Exit Function
– Kubie
Nov 14 '18 at 17:25
@GingerDom on the next line after that you can include
Exit Function
– Kubie
Nov 14 '18 at 17:25
1
1
@GingerDom Putting
Exit Function
like Kubie suggested after each of the Check = "Yes"
and Check = "TBD"
would do the trick. But I would think your #VALUE
error is because you're not wrapping your string in quotation marks in your formula.– dwirony
Nov 14 '18 at 17:28
@GingerDom Putting
Exit Function
like Kubie suggested after each of the Check = "Yes"
and Check = "TBD"
would do the trick. But I would think your #VALUE
error is because you're not wrapping your string in quotation marks in your formula.– dwirony
Nov 14 '18 at 17:28
|
show 2 more comments
2 Answers
2
active
oldest
votes
Here's a version of the function using find
like @urdearboy mentioned...
Public Function Check(product As String) As String
Dim BLRange As Range
Dim TBDRange As Range
Dim fndRng As Range
With ActiveWorkbook
Set BLRange = .Worksheets("Sheet2").Columns("A")
Set TBDRange = .Worksheets("Sheet3").Columns("A")
End With
Set fndRng = BLRange.Find(product)
If Not fndRng is Nothing Then Check = "Yes": Exit Function
Set fndRng = TBDRange.Find(product)
If Not fndRng is Nothing Then Check = "TBD": Exit Function
End Function
1
@Egalth - if the Find method doesn't find the search term, the range variable will return Nothing, not zero.
– SJR
Nov 14 '18 at 18:03
add a comment |
Try this code (already tested and works)
Public Function Check(product As String) As String
Dim BLRange As Range
Dim BL As Worksheet
Dim TBDRange As Range
Dim TBD As Worksheet
Dim result As String
Set BL = ActiveWorkbook.Worksheets("Sheet2")
Set BLRange = BL.Range("A1:A1000")
Set TBD = ActiveWorkbook.Worksheets("Sheet3")
Set TBDRange = TBD.Range("A1:A1000")
Check = "none"
For Each xlCell In BLRange
If xlCell.Text = product Then
Check = "Yes"
GoTo a
End If
Next xlCell
For Each xlCell In TBDRange
If xlCell.Text = product Then
Check = "TBD"
GoTo a
End If
Next xlCell
Exit Function
a:
End Function
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53305472%2fexcel-macro-check-if-value-appears-in-a-different-worksheet%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Here's a version of the function using find
like @urdearboy mentioned...
Public Function Check(product As String) As String
Dim BLRange As Range
Dim TBDRange As Range
Dim fndRng As Range
With ActiveWorkbook
Set BLRange = .Worksheets("Sheet2").Columns("A")
Set TBDRange = .Worksheets("Sheet3").Columns("A")
End With
Set fndRng = BLRange.Find(product)
If Not fndRng is Nothing Then Check = "Yes": Exit Function
Set fndRng = TBDRange.Find(product)
If Not fndRng is Nothing Then Check = "TBD": Exit Function
End Function
1
@Egalth - if the Find method doesn't find the search term, the range variable will return Nothing, not zero.
– SJR
Nov 14 '18 at 18:03
add a comment |
Here's a version of the function using find
like @urdearboy mentioned...
Public Function Check(product As String) As String
Dim BLRange As Range
Dim TBDRange As Range
Dim fndRng As Range
With ActiveWorkbook
Set BLRange = .Worksheets("Sheet2").Columns("A")
Set TBDRange = .Worksheets("Sheet3").Columns("A")
End With
Set fndRng = BLRange.Find(product)
If Not fndRng is Nothing Then Check = "Yes": Exit Function
Set fndRng = TBDRange.Find(product)
If Not fndRng is Nothing Then Check = "TBD": Exit Function
End Function
1
@Egalth - if the Find method doesn't find the search term, the range variable will return Nothing, not zero.
– SJR
Nov 14 '18 at 18:03
add a comment |
Here's a version of the function using find
like @urdearboy mentioned...
Public Function Check(product As String) As String
Dim BLRange As Range
Dim TBDRange As Range
Dim fndRng As Range
With ActiveWorkbook
Set BLRange = .Worksheets("Sheet2").Columns("A")
Set TBDRange = .Worksheets("Sheet3").Columns("A")
End With
Set fndRng = BLRange.Find(product)
If Not fndRng is Nothing Then Check = "Yes": Exit Function
Set fndRng = TBDRange.Find(product)
If Not fndRng is Nothing Then Check = "TBD": Exit Function
End Function
Here's a version of the function using find
like @urdearboy mentioned...
Public Function Check(product As String) As String
Dim BLRange As Range
Dim TBDRange As Range
Dim fndRng As Range
With ActiveWorkbook
Set BLRange = .Worksheets("Sheet2").Columns("A")
Set TBDRange = .Worksheets("Sheet3").Columns("A")
End With
Set fndRng = BLRange.Find(product)
If Not fndRng is Nothing Then Check = "Yes": Exit Function
Set fndRng = TBDRange.Find(product)
If Not fndRng is Nothing Then Check = "TBD": Exit Function
End Function
edited Nov 14 '18 at 17:47
answered Nov 14 '18 at 17:30
KubieKubie
1,3671619
1,3671619
1
@Egalth - if the Find method doesn't find the search term, the range variable will return Nothing, not zero.
– SJR
Nov 14 '18 at 18:03
add a comment |
1
@Egalth - if the Find method doesn't find the search term, the range variable will return Nothing, not zero.
– SJR
Nov 14 '18 at 18:03
1
1
@Egalth - if the Find method doesn't find the search term, the range variable will return Nothing, not zero.
– SJR
Nov 14 '18 at 18:03
@Egalth - if the Find method doesn't find the search term, the range variable will return Nothing, not zero.
– SJR
Nov 14 '18 at 18:03
add a comment |
Try this code (already tested and works)
Public Function Check(product As String) As String
Dim BLRange As Range
Dim BL As Worksheet
Dim TBDRange As Range
Dim TBD As Worksheet
Dim result As String
Set BL = ActiveWorkbook.Worksheets("Sheet2")
Set BLRange = BL.Range("A1:A1000")
Set TBD = ActiveWorkbook.Worksheets("Sheet3")
Set TBDRange = TBD.Range("A1:A1000")
Check = "none"
For Each xlCell In BLRange
If xlCell.Text = product Then
Check = "Yes"
GoTo a
End If
Next xlCell
For Each xlCell In TBDRange
If xlCell.Text = product Then
Check = "TBD"
GoTo a
End If
Next xlCell
Exit Function
a:
End Function
add a comment |
Try this code (already tested and works)
Public Function Check(product As String) As String
Dim BLRange As Range
Dim BL As Worksheet
Dim TBDRange As Range
Dim TBD As Worksheet
Dim result As String
Set BL = ActiveWorkbook.Worksheets("Sheet2")
Set BLRange = BL.Range("A1:A1000")
Set TBD = ActiveWorkbook.Worksheets("Sheet3")
Set TBDRange = TBD.Range("A1:A1000")
Check = "none"
For Each xlCell In BLRange
If xlCell.Text = product Then
Check = "Yes"
GoTo a
End If
Next xlCell
For Each xlCell In TBDRange
If xlCell.Text = product Then
Check = "TBD"
GoTo a
End If
Next xlCell
Exit Function
a:
End Function
add a comment |
Try this code (already tested and works)
Public Function Check(product As String) As String
Dim BLRange As Range
Dim BL As Worksheet
Dim TBDRange As Range
Dim TBD As Worksheet
Dim result As String
Set BL = ActiveWorkbook.Worksheets("Sheet2")
Set BLRange = BL.Range("A1:A1000")
Set TBD = ActiveWorkbook.Worksheets("Sheet3")
Set TBDRange = TBD.Range("A1:A1000")
Check = "none"
For Each xlCell In BLRange
If xlCell.Text = product Then
Check = "Yes"
GoTo a
End If
Next xlCell
For Each xlCell In TBDRange
If xlCell.Text = product Then
Check = "TBD"
GoTo a
End If
Next xlCell
Exit Function
a:
End Function
Try this code (already tested and works)
Public Function Check(product As String) As String
Dim BLRange As Range
Dim BL As Worksheet
Dim TBDRange As Range
Dim TBD As Worksheet
Dim result As String
Set BL = ActiveWorkbook.Worksheets("Sheet2")
Set BLRange = BL.Range("A1:A1000")
Set TBD = ActiveWorkbook.Worksheets("Sheet3")
Set TBDRange = TBD.Range("A1:A1000")
Check = "none"
For Each xlCell In BLRange
If xlCell.Text = product Then
Check = "Yes"
GoTo a
End If
Next xlCell
For Each xlCell In TBDRange
If xlCell.Text = product Then
Check = "TBD"
GoTo a
End If
Next xlCell
Exit Function
a:
End Function
answered Nov 14 '18 at 18:39
Juan JoyaJuan Joya
544
544
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53305472%2fexcel-macro-check-if-value-appears-in-a-different-worksheet%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Does this need to be a UDF? Looks like it could be a simple pair of
VLOOKUP
s.– Comintern
Nov 14 '18 at 17:19
Hi, yeah I would prefer a
VLOOKUP
but the return has to be in the same column, and I didn't think you could get it to return the "Y" or "TBD" depending on the sheet it came from. All the data in Sheets 2 and 3 are in column A– GingerDom
Nov 14 '18 at 17:23
5
Even if your value IS found, you're always going to return "" because of the last line in your function.
– dwirony
Nov 14 '18 at 17:23
2
@GingerDom on the next line after that you can include
Exit Function
– Kubie
Nov 14 '18 at 17:25
1
@GingerDom Putting
Exit Function
like Kubie suggested after each of theCheck = "Yes"
andCheck = "TBD"
would do the trick. But I would think your#VALUE
error is because you're not wrapping your string in quotation marks in your formula.– dwirony
Nov 14 '18 at 17:28