Return Null in Excel
I'm currently attempting to write a formula that averages together 4 values that may or may not be filled. The formula searches a string for the value in each cell, and it then averages those values together.
The problem is that if the value isn't found, I need it to return null
, because if I return 0
it doesn't average properly, and a blank ""
value returns a #VALUE
error, even if wrapped in a the VALUE()
function and using the AVERAGEA()
function.
I also tried writing a very simple macro that just returns null, but from what I understand Excel doesn't like that.
So is there some way to return null that I've overlooked? I know that in GoogleSheets the formula =IFERROR(1/0)
would do the trick - is there a similar workaround?
I'm honestly expecting that I'll have to change the formula to all be in VBA, but I wanted to see if I've overlooked something here.
Thanks in advance!
Edit:
So the code I'm using looks like this (I've shortened it down a bit to make it easier):
=AVERAGE(IFERROR(VALUE(MID($AF2, FIND("Hit:", $AF2) + 4, 3)), ""), IFERROR(VALUE(MID($AH2, FIND("Hit:", $AH2) + 4, 3)), ""))
In cells AF2 and AH2 are long strings, and the number I'm searching for is always right after it says "Hit:" (if the number is there). So with AVERAGEIF() I'm not sure how to input the range argument there
excel vba excel-vba excel-formula
|
show 2 more comments
I'm currently attempting to write a formula that averages together 4 values that may or may not be filled. The formula searches a string for the value in each cell, and it then averages those values together.
The problem is that if the value isn't found, I need it to return null
, because if I return 0
it doesn't average properly, and a blank ""
value returns a #VALUE
error, even if wrapped in a the VALUE()
function and using the AVERAGEA()
function.
I also tried writing a very simple macro that just returns null, but from what I understand Excel doesn't like that.
So is there some way to return null that I've overlooked? I know that in GoogleSheets the formula =IFERROR(1/0)
would do the trick - is there a similar workaround?
I'm honestly expecting that I'll have to change the formula to all be in VBA, but I wanted to see if I've overlooked something here.
Thanks in advance!
Edit:
So the code I'm using looks like this (I've shortened it down a bit to make it easier):
=AVERAGE(IFERROR(VALUE(MID($AF2, FIND("Hit:", $AF2) + 4, 3)), ""), IFERROR(VALUE(MID($AH2, FIND("Hit:", $AH2) + 4, 3)), ""))
In cells AF2 and AH2 are long strings, and the number I'm searching for is always right after it says "Hit:" (if the number is there). So with AVERAGEIF() I'm not sure how to input the range argument there
excel vba excel-vba excel-formula
1
Can't you just use an empty string?""
– trincot
Nov 13 '18 at 16:18
1
What are you entering in your AVERAGE() function? When used normally it would usually treat an empty string the same way as a null value
– Dave
Nov 13 '18 at 16:21
1
@ReyJuna note that">0"
also ignores negative values while"<>0"
does only ignore0
.
– Pᴇʜ
Nov 13 '18 at 16:30
1
Be aware that in some scenarios a zero might actually occur in the real data, where it should count in the average.
– trincot
Nov 13 '18 at 16:33
1
@BrettAbraham Please include all codes within the question itself. Add a Minimal, Complete, and Verifiable example with valid example data so we can easily reproduce your issue. Screenshots might help too.
– Pᴇʜ
Nov 13 '18 at 16:54
|
show 2 more comments
I'm currently attempting to write a formula that averages together 4 values that may or may not be filled. The formula searches a string for the value in each cell, and it then averages those values together.
The problem is that if the value isn't found, I need it to return null
, because if I return 0
it doesn't average properly, and a blank ""
value returns a #VALUE
error, even if wrapped in a the VALUE()
function and using the AVERAGEA()
function.
I also tried writing a very simple macro that just returns null, but from what I understand Excel doesn't like that.
So is there some way to return null that I've overlooked? I know that in GoogleSheets the formula =IFERROR(1/0)
would do the trick - is there a similar workaround?
I'm honestly expecting that I'll have to change the formula to all be in VBA, but I wanted to see if I've overlooked something here.
Thanks in advance!
Edit:
So the code I'm using looks like this (I've shortened it down a bit to make it easier):
=AVERAGE(IFERROR(VALUE(MID($AF2, FIND("Hit:", $AF2) + 4, 3)), ""), IFERROR(VALUE(MID($AH2, FIND("Hit:", $AH2) + 4, 3)), ""))
In cells AF2 and AH2 are long strings, and the number I'm searching for is always right after it says "Hit:" (if the number is there). So with AVERAGEIF() I'm not sure how to input the range argument there
excel vba excel-vba excel-formula
I'm currently attempting to write a formula that averages together 4 values that may or may not be filled. The formula searches a string for the value in each cell, and it then averages those values together.
The problem is that if the value isn't found, I need it to return null
, because if I return 0
it doesn't average properly, and a blank ""
value returns a #VALUE
error, even if wrapped in a the VALUE()
function and using the AVERAGEA()
function.
I also tried writing a very simple macro that just returns null, but from what I understand Excel doesn't like that.
So is there some way to return null that I've overlooked? I know that in GoogleSheets the formula =IFERROR(1/0)
would do the trick - is there a similar workaround?
I'm honestly expecting that I'll have to change the formula to all be in VBA, but I wanted to see if I've overlooked something here.
Thanks in advance!
Edit:
So the code I'm using looks like this (I've shortened it down a bit to make it easier):
=AVERAGE(IFERROR(VALUE(MID($AF2, FIND("Hit:", $AF2) + 4, 3)), ""), IFERROR(VALUE(MID($AH2, FIND("Hit:", $AH2) + 4, 3)), ""))
In cells AF2 and AH2 are long strings, and the number I'm searching for is always right after it says "Hit:" (if the number is there). So with AVERAGEIF() I'm not sure how to input the range argument there
excel vba excel-vba excel-formula
excel vba excel-vba excel-formula
edited Nov 13 '18 at 17:22
Brett Abraham
asked Nov 13 '18 at 16:16
Brett AbrahamBrett Abraham
13
13
1
Can't you just use an empty string?""
– trincot
Nov 13 '18 at 16:18
1
What are you entering in your AVERAGE() function? When used normally it would usually treat an empty string the same way as a null value
– Dave
Nov 13 '18 at 16:21
1
@ReyJuna note that">0"
also ignores negative values while"<>0"
does only ignore0
.
– Pᴇʜ
Nov 13 '18 at 16:30
1
Be aware that in some scenarios a zero might actually occur in the real data, where it should count in the average.
– trincot
Nov 13 '18 at 16:33
1
@BrettAbraham Please include all codes within the question itself. Add a Minimal, Complete, and Verifiable example with valid example data so we can easily reproduce your issue. Screenshots might help too.
– Pᴇʜ
Nov 13 '18 at 16:54
|
show 2 more comments
1
Can't you just use an empty string?""
– trincot
Nov 13 '18 at 16:18
1
What are you entering in your AVERAGE() function? When used normally it would usually treat an empty string the same way as a null value
– Dave
Nov 13 '18 at 16:21
1
@ReyJuna note that">0"
also ignores negative values while"<>0"
does only ignore0
.
– Pᴇʜ
Nov 13 '18 at 16:30
1
Be aware that in some scenarios a zero might actually occur in the real data, where it should count in the average.
– trincot
Nov 13 '18 at 16:33
1
@BrettAbraham Please include all codes within the question itself. Add a Minimal, Complete, and Verifiable example with valid example data so we can easily reproduce your issue. Screenshots might help too.
– Pᴇʜ
Nov 13 '18 at 16:54
1
1
Can't you just use an empty string?
""
– trincot
Nov 13 '18 at 16:18
Can't you just use an empty string?
""
– trincot
Nov 13 '18 at 16:18
1
1
What are you entering in your AVERAGE() function? When used normally it would usually treat an empty string the same way as a null value
– Dave
Nov 13 '18 at 16:21
What are you entering in your AVERAGE() function? When used normally it would usually treat an empty string the same way as a null value
– Dave
Nov 13 '18 at 16:21
1
1
@ReyJuna note that
">0"
also ignores negative values while "<>0"
does only ignore 0
.– Pᴇʜ
Nov 13 '18 at 16:30
@ReyJuna note that
">0"
also ignores negative values while "<>0"
does only ignore 0
.– Pᴇʜ
Nov 13 '18 at 16:30
1
1
Be aware that in some scenarios a zero might actually occur in the real data, where it should count in the average.
– trincot
Nov 13 '18 at 16:33
Be aware that in some scenarios a zero might actually occur in the real data, where it should count in the average.
– trincot
Nov 13 '18 at 16:33
1
1
@BrettAbraham Please include all codes within the question itself. Add a Minimal, Complete, and Verifiable example with valid example data so we can easily reproduce your issue. Screenshots might help too.
– Pᴇʜ
Nov 13 '18 at 16:54
@BrettAbraham Please include all codes within the question itself. Add a Minimal, Complete, and Verifiable example with valid example data so we can easily reproduce your issue. Screenshots might help too.
– Pᴇʜ
Nov 13 '18 at 16:54
|
show 2 more comments
3 Answers
3
active
oldest
votes
You can use iferror() to return whatever value you wish:
=iferror(your_function,"")
or
=iferror(your_function,"Check")
even numbers...
add a comment |
The only solution I can think of is to use the AVERAGE() function as an array. The "" is treated as a string in regular AVERAGE() or SUM() functions, but is seen as a null value in arrays.
Try this (as an array, Ctrl + Shift + Enter):
IFERROR(VALUE(MID($AF2:$AH2, FIND("Hit:", $AF2:$AH2) + 4, 3)), "")
This almost got there, but unfortunately if one of the cells is missing the value, then the entire thing returns as null. I tried messing around with your suggestion, but couldn't get it to work. I'm going to fool around a bit more and if that doesn't work, I'll just write the formula in VBA.
– Brett Abraham
Nov 13 '18 at 21:36
add a comment |
So I had to give up on trying to do this without VBA. I wrote a function that just goes through and adds together the values found after the searchFor argument. If anyone ever runs into this issue, here's what I did:
'''Searches for a string in each cell in the range, then pulls the value
'''averages together all values that were found offset number of characters away from the string
Function averageWithErrors(searchFor As String, rng As range, offset As Integer, extractLength As Integer) As Double
'Declare Variables
Dim characterPosition As Integer
Dim num As Double
Dim count As Integer
Dim cellCheck As range
Dim cellTotals As Integer
'Set Variables
count = 0
num = 0
'Check each cell
For Each cellCheck In rng
If InStr(cellCheck.Value, searchFor) Then 'Check if cell has the string
characterPosition = InStr(cellCheck.Value, searchFor)
num = num + Val(Mid(cellCheck.Value, characterPosition + offset, extractLength)) 'Add the value to the running total
count = count + 1
End If
Next cellCheck
averageWithErrors = num / count 'return the average of the values found
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%2f53285190%2freturn-null-in-excel%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
You can use iferror() to return whatever value you wish:
=iferror(your_function,"")
or
=iferror(your_function,"Check")
even numbers...
add a comment |
You can use iferror() to return whatever value you wish:
=iferror(your_function,"")
or
=iferror(your_function,"Check")
even numbers...
add a comment |
You can use iferror() to return whatever value you wish:
=iferror(your_function,"")
or
=iferror(your_function,"Check")
even numbers...
You can use iferror() to return whatever value you wish:
=iferror(your_function,"")
or
=iferror(your_function,"Check")
even numbers...
answered Nov 13 '18 at 16:24
Solar MikeSolar Mike
2,1392514
2,1392514
add a comment |
add a comment |
The only solution I can think of is to use the AVERAGE() function as an array. The "" is treated as a string in regular AVERAGE() or SUM() functions, but is seen as a null value in arrays.
Try this (as an array, Ctrl + Shift + Enter):
IFERROR(VALUE(MID($AF2:$AH2, FIND("Hit:", $AF2:$AH2) + 4, 3)), "")
This almost got there, but unfortunately if one of the cells is missing the value, then the entire thing returns as null. I tried messing around with your suggestion, but couldn't get it to work. I'm going to fool around a bit more and if that doesn't work, I'll just write the formula in VBA.
– Brett Abraham
Nov 13 '18 at 21:36
add a comment |
The only solution I can think of is to use the AVERAGE() function as an array. The "" is treated as a string in regular AVERAGE() or SUM() functions, but is seen as a null value in arrays.
Try this (as an array, Ctrl + Shift + Enter):
IFERROR(VALUE(MID($AF2:$AH2, FIND("Hit:", $AF2:$AH2) + 4, 3)), "")
This almost got there, but unfortunately if one of the cells is missing the value, then the entire thing returns as null. I tried messing around with your suggestion, but couldn't get it to work. I'm going to fool around a bit more and if that doesn't work, I'll just write the formula in VBA.
– Brett Abraham
Nov 13 '18 at 21:36
add a comment |
The only solution I can think of is to use the AVERAGE() function as an array. The "" is treated as a string in regular AVERAGE() or SUM() functions, but is seen as a null value in arrays.
Try this (as an array, Ctrl + Shift + Enter):
IFERROR(VALUE(MID($AF2:$AH2, FIND("Hit:", $AF2:$AH2) + 4, 3)), "")
The only solution I can think of is to use the AVERAGE() function as an array. The "" is treated as a string in regular AVERAGE() or SUM() functions, but is seen as a null value in arrays.
Try this (as an array, Ctrl + Shift + Enter):
IFERROR(VALUE(MID($AF2:$AH2, FIND("Hit:", $AF2:$AH2) + 4, 3)), "")
answered Nov 13 '18 at 20:21
MattMatt
111
111
This almost got there, but unfortunately if one of the cells is missing the value, then the entire thing returns as null. I tried messing around with your suggestion, but couldn't get it to work. I'm going to fool around a bit more and if that doesn't work, I'll just write the formula in VBA.
– Brett Abraham
Nov 13 '18 at 21:36
add a comment |
This almost got there, but unfortunately if one of the cells is missing the value, then the entire thing returns as null. I tried messing around with your suggestion, but couldn't get it to work. I'm going to fool around a bit more and if that doesn't work, I'll just write the formula in VBA.
– Brett Abraham
Nov 13 '18 at 21:36
This almost got there, but unfortunately if one of the cells is missing the value, then the entire thing returns as null. I tried messing around with your suggestion, but couldn't get it to work. I'm going to fool around a bit more and if that doesn't work, I'll just write the formula in VBA.
– Brett Abraham
Nov 13 '18 at 21:36
This almost got there, but unfortunately if one of the cells is missing the value, then the entire thing returns as null. I tried messing around with your suggestion, but couldn't get it to work. I'm going to fool around a bit more and if that doesn't work, I'll just write the formula in VBA.
– Brett Abraham
Nov 13 '18 at 21:36
add a comment |
So I had to give up on trying to do this without VBA. I wrote a function that just goes through and adds together the values found after the searchFor argument. If anyone ever runs into this issue, here's what I did:
'''Searches for a string in each cell in the range, then pulls the value
'''averages together all values that were found offset number of characters away from the string
Function averageWithErrors(searchFor As String, rng As range, offset As Integer, extractLength As Integer) As Double
'Declare Variables
Dim characterPosition As Integer
Dim num As Double
Dim count As Integer
Dim cellCheck As range
Dim cellTotals As Integer
'Set Variables
count = 0
num = 0
'Check each cell
For Each cellCheck In rng
If InStr(cellCheck.Value, searchFor) Then 'Check if cell has the string
characterPosition = InStr(cellCheck.Value, searchFor)
num = num + Val(Mid(cellCheck.Value, characterPosition + offset, extractLength)) 'Add the value to the running total
count = count + 1
End If
Next cellCheck
averageWithErrors = num / count 'return the average of the values found
End Function
add a comment |
So I had to give up on trying to do this without VBA. I wrote a function that just goes through and adds together the values found after the searchFor argument. If anyone ever runs into this issue, here's what I did:
'''Searches for a string in each cell in the range, then pulls the value
'''averages together all values that were found offset number of characters away from the string
Function averageWithErrors(searchFor As String, rng As range, offset As Integer, extractLength As Integer) As Double
'Declare Variables
Dim characterPosition As Integer
Dim num As Double
Dim count As Integer
Dim cellCheck As range
Dim cellTotals As Integer
'Set Variables
count = 0
num = 0
'Check each cell
For Each cellCheck In rng
If InStr(cellCheck.Value, searchFor) Then 'Check if cell has the string
characterPosition = InStr(cellCheck.Value, searchFor)
num = num + Val(Mid(cellCheck.Value, characterPosition + offset, extractLength)) 'Add the value to the running total
count = count + 1
End If
Next cellCheck
averageWithErrors = num / count 'return the average of the values found
End Function
add a comment |
So I had to give up on trying to do this without VBA. I wrote a function that just goes through and adds together the values found after the searchFor argument. If anyone ever runs into this issue, here's what I did:
'''Searches for a string in each cell in the range, then pulls the value
'''averages together all values that were found offset number of characters away from the string
Function averageWithErrors(searchFor As String, rng As range, offset As Integer, extractLength As Integer) As Double
'Declare Variables
Dim characterPosition As Integer
Dim num As Double
Dim count As Integer
Dim cellCheck As range
Dim cellTotals As Integer
'Set Variables
count = 0
num = 0
'Check each cell
For Each cellCheck In rng
If InStr(cellCheck.Value, searchFor) Then 'Check if cell has the string
characterPosition = InStr(cellCheck.Value, searchFor)
num = num + Val(Mid(cellCheck.Value, characterPosition + offset, extractLength)) 'Add the value to the running total
count = count + 1
End If
Next cellCheck
averageWithErrors = num / count 'return the average of the values found
End Function
So I had to give up on trying to do this without VBA. I wrote a function that just goes through and adds together the values found after the searchFor argument. If anyone ever runs into this issue, here's what I did:
'''Searches for a string in each cell in the range, then pulls the value
'''averages together all values that were found offset number of characters away from the string
Function averageWithErrors(searchFor As String, rng As range, offset As Integer, extractLength As Integer) As Double
'Declare Variables
Dim characterPosition As Integer
Dim num As Double
Dim count As Integer
Dim cellCheck As range
Dim cellTotals As Integer
'Set Variables
count = 0
num = 0
'Check each cell
For Each cellCheck In rng
If InStr(cellCheck.Value, searchFor) Then 'Check if cell has the string
characterPosition = InStr(cellCheck.Value, searchFor)
num = num + Val(Mid(cellCheck.Value, characterPosition + offset, extractLength)) 'Add the value to the running total
count = count + 1
End If
Next cellCheck
averageWithErrors = num / count 'return the average of the values found
End Function
answered Nov 15 '18 at 18:33
Brett AbrahamBrett Abraham
13
13
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%2f53285190%2freturn-null-in-excel%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
1
Can't you just use an empty string?
""
– trincot
Nov 13 '18 at 16:18
1
What are you entering in your AVERAGE() function? When used normally it would usually treat an empty string the same way as a null value
– Dave
Nov 13 '18 at 16:21
1
@ReyJuna note that
">0"
also ignores negative values while"<>0"
does only ignore0
.– Pᴇʜ
Nov 13 '18 at 16:30
1
Be aware that in some scenarios a zero might actually occur in the real data, where it should count in the average.
– trincot
Nov 13 '18 at 16:33
1
@BrettAbraham Please include all codes within the question itself. Add a Minimal, Complete, and Verifiable example with valid example data so we can easily reproduce your issue. Screenshots might help too.
– Pᴇʜ
Nov 13 '18 at 16:54