Converting from Excel formula to VBA code
What's the equivalent of the following Excel code in VBA?
=MATCH(A1,Ranged_Name,0)
where Ranged_Name is a ranged name on the workbook level.
excel vba
add a comment |
What's the equivalent of the following Excel code in VBA?
=MATCH(A1,Ranged_Name,0)
where Ranged_Name is a ranged name on the workbook level.
excel vba
You may want to read about worksheet functions
– cybernetic.nomad
Nov 20 '18 at 18:33
add a comment |
What's the equivalent of the following Excel code in VBA?
=MATCH(A1,Ranged_Name,0)
where Ranged_Name is a ranged name on the workbook level.
excel vba
What's the equivalent of the following Excel code in VBA?
=MATCH(A1,Ranged_Name,0)
where Ranged_Name is a ranged name on the workbook level.
excel vba
excel vba
edited Nov 20 '18 at 18:31


Scott Craner
90.3k82550
90.3k82550
asked Nov 20 '18 at 18:30


Garry WGarry W
767
767
You may want to read about worksheet functions
– cybernetic.nomad
Nov 20 '18 at 18:33
add a comment |
You may want to read about worksheet functions
– cybernetic.nomad
Nov 20 '18 at 18:33
You may want to read about worksheet functions
– cybernetic.nomad
Nov 20 '18 at 18:33
You may want to read about worksheet functions
– cybernetic.nomad
Nov 20 '18 at 18:33
add a comment |
3 Answers
3
active
oldest
votes
In both a worksheet cell and VBA:
Sub dural()
MsgBox Evaluate("Match(A1,Range_Name,0)")
End Sub
EDIT#1:
Based on the Comments, a better alternative:
Sub dural()
Dim r1 As Range, r2 As Range, v As Variant, wf As WorksheetFunction
Set r1 = Sheets("Sheet1").Range("A1")
Set r2 = Range("Range_Name")
Set wf = Application.WorksheetFunction
v = wf.Match(r1, r2, 0)
MsgBox v
End Sub
and to use Find()
:
Sub larud()
Dim r1 As Range, r2 As Range, v As Variant
Set r1 = Sheets("Sheet1").Range("A1")
Set r2 = Range("Range_Name")
v = r2.Find(What:=r1.Value, After:=r2(1)).Row
MsgBox v
End Sub
2
Might be an improvment to use theWorksheet.Evaluate
method, rather than theApplication
version? Otherwise, the result will vary according to which sheet is active.
– Tim Williams
Nov 20 '18 at 18:51
@TimWilliams You are correct!
– Gary's Student
Nov 20 '18 at 19:12
I used Evaluate("Match('Sheet1'!A1,Range_Name,0)") and that also worked. :)
– Garry W
Nov 21 '18 at 14:03
@GarryWang See my EDIT#1
– Gary's Student
Nov 21 '18 at 14:14
@Gary'sStudent I surely feel like the student here. Thank you for the help!
– Garry W
Nov 21 '18 at 18:23
add a comment |
To add one more:
Dim r
With Activesheet
r = Application.Match(.Range("A1").Value, Range("Ranged_Name"), 0)
End with
If Not IsError(r) Then
'got a match
End If
add a comment |
You can either use Range().Find or Evaluate() your Match, if the sheet is the ActiveSheet.
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%2f53399325%2fconverting-from-excel-formula-to-vba-code%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
In both a worksheet cell and VBA:
Sub dural()
MsgBox Evaluate("Match(A1,Range_Name,0)")
End Sub
EDIT#1:
Based on the Comments, a better alternative:
Sub dural()
Dim r1 As Range, r2 As Range, v As Variant, wf As WorksheetFunction
Set r1 = Sheets("Sheet1").Range("A1")
Set r2 = Range("Range_Name")
Set wf = Application.WorksheetFunction
v = wf.Match(r1, r2, 0)
MsgBox v
End Sub
and to use Find()
:
Sub larud()
Dim r1 As Range, r2 As Range, v As Variant
Set r1 = Sheets("Sheet1").Range("A1")
Set r2 = Range("Range_Name")
v = r2.Find(What:=r1.Value, After:=r2(1)).Row
MsgBox v
End Sub
2
Might be an improvment to use theWorksheet.Evaluate
method, rather than theApplication
version? Otherwise, the result will vary according to which sheet is active.
– Tim Williams
Nov 20 '18 at 18:51
@TimWilliams You are correct!
– Gary's Student
Nov 20 '18 at 19:12
I used Evaluate("Match('Sheet1'!A1,Range_Name,0)") and that also worked. :)
– Garry W
Nov 21 '18 at 14:03
@GarryWang See my EDIT#1
– Gary's Student
Nov 21 '18 at 14:14
@Gary'sStudent I surely feel like the student here. Thank you for the help!
– Garry W
Nov 21 '18 at 18:23
add a comment |
In both a worksheet cell and VBA:
Sub dural()
MsgBox Evaluate("Match(A1,Range_Name,0)")
End Sub
EDIT#1:
Based on the Comments, a better alternative:
Sub dural()
Dim r1 As Range, r2 As Range, v As Variant, wf As WorksheetFunction
Set r1 = Sheets("Sheet1").Range("A1")
Set r2 = Range("Range_Name")
Set wf = Application.WorksheetFunction
v = wf.Match(r1, r2, 0)
MsgBox v
End Sub
and to use Find()
:
Sub larud()
Dim r1 As Range, r2 As Range, v As Variant
Set r1 = Sheets("Sheet1").Range("A1")
Set r2 = Range("Range_Name")
v = r2.Find(What:=r1.Value, After:=r2(1)).Row
MsgBox v
End Sub
2
Might be an improvment to use theWorksheet.Evaluate
method, rather than theApplication
version? Otherwise, the result will vary according to which sheet is active.
– Tim Williams
Nov 20 '18 at 18:51
@TimWilliams You are correct!
– Gary's Student
Nov 20 '18 at 19:12
I used Evaluate("Match('Sheet1'!A1,Range_Name,0)") and that also worked. :)
– Garry W
Nov 21 '18 at 14:03
@GarryWang See my EDIT#1
– Gary's Student
Nov 21 '18 at 14:14
@Gary'sStudent I surely feel like the student here. Thank you for the help!
– Garry W
Nov 21 '18 at 18:23
add a comment |
In both a worksheet cell and VBA:
Sub dural()
MsgBox Evaluate("Match(A1,Range_Name,0)")
End Sub
EDIT#1:
Based on the Comments, a better alternative:
Sub dural()
Dim r1 As Range, r2 As Range, v As Variant, wf As WorksheetFunction
Set r1 = Sheets("Sheet1").Range("A1")
Set r2 = Range("Range_Name")
Set wf = Application.WorksheetFunction
v = wf.Match(r1, r2, 0)
MsgBox v
End Sub
and to use Find()
:
Sub larud()
Dim r1 As Range, r2 As Range, v As Variant
Set r1 = Sheets("Sheet1").Range("A1")
Set r2 = Range("Range_Name")
v = r2.Find(What:=r1.Value, After:=r2(1)).Row
MsgBox v
End Sub
In both a worksheet cell and VBA:
Sub dural()
MsgBox Evaluate("Match(A1,Range_Name,0)")
End Sub
EDIT#1:
Based on the Comments, a better alternative:
Sub dural()
Dim r1 As Range, r2 As Range, v As Variant, wf As WorksheetFunction
Set r1 = Sheets("Sheet1").Range("A1")
Set r2 = Range("Range_Name")
Set wf = Application.WorksheetFunction
v = wf.Match(r1, r2, 0)
MsgBox v
End Sub
and to use Find()
:
Sub larud()
Dim r1 As Range, r2 As Range, v As Variant
Set r1 = Sheets("Sheet1").Range("A1")
Set r2 = Range("Range_Name")
v = r2.Find(What:=r1.Value, After:=r2(1)).Row
MsgBox v
End Sub
edited Nov 21 '18 at 14:13
answered Nov 20 '18 at 18:39


Gary's StudentGary's Student
72.8k93962
72.8k93962
2
Might be an improvment to use theWorksheet.Evaluate
method, rather than theApplication
version? Otherwise, the result will vary according to which sheet is active.
– Tim Williams
Nov 20 '18 at 18:51
@TimWilliams You are correct!
– Gary's Student
Nov 20 '18 at 19:12
I used Evaluate("Match('Sheet1'!A1,Range_Name,0)") and that also worked. :)
– Garry W
Nov 21 '18 at 14:03
@GarryWang See my EDIT#1
– Gary's Student
Nov 21 '18 at 14:14
@Gary'sStudent I surely feel like the student here. Thank you for the help!
– Garry W
Nov 21 '18 at 18:23
add a comment |
2
Might be an improvment to use theWorksheet.Evaluate
method, rather than theApplication
version? Otherwise, the result will vary according to which sheet is active.
– Tim Williams
Nov 20 '18 at 18:51
@TimWilliams You are correct!
– Gary's Student
Nov 20 '18 at 19:12
I used Evaluate("Match('Sheet1'!A1,Range_Name,0)") and that also worked. :)
– Garry W
Nov 21 '18 at 14:03
@GarryWang See my EDIT#1
– Gary's Student
Nov 21 '18 at 14:14
@Gary'sStudent I surely feel like the student here. Thank you for the help!
– Garry W
Nov 21 '18 at 18:23
2
2
Might be an improvment to use the
Worksheet.Evaluate
method, rather than the Application
version? Otherwise, the result will vary according to which sheet is active.– Tim Williams
Nov 20 '18 at 18:51
Might be an improvment to use the
Worksheet.Evaluate
method, rather than the Application
version? Otherwise, the result will vary according to which sheet is active.– Tim Williams
Nov 20 '18 at 18:51
@TimWilliams You are correct!
– Gary's Student
Nov 20 '18 at 19:12
@TimWilliams You are correct!
– Gary's Student
Nov 20 '18 at 19:12
I used Evaluate("Match('Sheet1'!A1,Range_Name,0)") and that also worked. :)
– Garry W
Nov 21 '18 at 14:03
I used Evaluate("Match('Sheet1'!A1,Range_Name,0)") and that also worked. :)
– Garry W
Nov 21 '18 at 14:03
@GarryWang See my EDIT#1
– Gary's Student
Nov 21 '18 at 14:14
@GarryWang See my EDIT#1
– Gary's Student
Nov 21 '18 at 14:14
@Gary'sStudent I surely feel like the student here. Thank you for the help!
– Garry W
Nov 21 '18 at 18:23
@Gary'sStudent I surely feel like the student here. Thank you for the help!
– Garry W
Nov 21 '18 at 18:23
add a comment |
To add one more:
Dim r
With Activesheet
r = Application.Match(.Range("A1").Value, Range("Ranged_Name"), 0)
End with
If Not IsError(r) Then
'got a match
End If
add a comment |
To add one more:
Dim r
With Activesheet
r = Application.Match(.Range("A1").Value, Range("Ranged_Name"), 0)
End with
If Not IsError(r) Then
'got a match
End If
add a comment |
To add one more:
Dim r
With Activesheet
r = Application.Match(.Range("A1").Value, Range("Ranged_Name"), 0)
End with
If Not IsError(r) Then
'got a match
End If
To add one more:
Dim r
With Activesheet
r = Application.Match(.Range("A1").Value, Range("Ranged_Name"), 0)
End with
If Not IsError(r) Then
'got a match
End If
answered Nov 20 '18 at 18:53


Tim WilliamsTim Williams
86.3k96986
86.3k96986
add a comment |
add a comment |
You can either use Range().Find or Evaluate() your Match, if the sheet is the ActiveSheet.
add a comment |
You can either use Range().Find or Evaluate() your Match, if the sheet is the ActiveSheet.
add a comment |
You can either use Range().Find or Evaluate() your Match, if the sheet is the ActiveSheet.
You can either use Range().Find or Evaluate() your Match, if the sheet is the ActiveSheet.
answered Nov 20 '18 at 18:37
Patrick HonorezPatrick Honorez
18.8k563118
18.8k563118
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%2f53399325%2fconverting-from-excel-formula-to-vba-code%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
You may want to read about worksheet functions
– cybernetic.nomad
Nov 20 '18 at 18:33