Formula works with manual input but not through VBA











up vote
2
down vote

favorite












I have a dataset that looks like the following:



A   B   C
1 10 145
1.1 11 0
1.2 12 0
1.3 13 0
2 14 145
2.1 15 0
2.2 16 0
3 17 145
3.1 18 0
3.2 19 0


Columns A and B in the table are my input data, column C is created with the following formula:



=IF(MOD([@A],1)=0,SUM([B]),0)


This is a simplified example and not the actual formula I am using, but it suffices as a minimal reproducible example.



When I enter this formula manually into the edit bar it works exactly as expected and produces the values in the table above. However, when I try to input this formula into the cells using VBA it fails. It gives me an error:




Error: invalid name




If I then click the cell and into the edit bar as if I wanted to manipulate the formula manually and just immediately hit enter, without actually changing anything in the formula, the error vanishes and the results get correctly displayed once more as before.



The code I use to enter the formula into the cell via VBA is the following:



Sub InputFormula()
Set srcTable = ActiveSheet.ListObjects(1)
fmtString = "=IF(MOD([@A],1)=0,SUM([B]),0)"
Range(srcTable.Name & "[C]").Formula = fmtString
End Sub


Why is the obviously correct formula broken when applied through VBA and how can I correct it so that application through VBA works?



I am using Excel 2019 on Windows 10 x64.










share|improve this question




















  • 2




    I'm not able to reproduce. I'm assuming that this dataset is a listobject/table?
    – JNevill
    Nov 12 at 16:26










  • Works as intended in Excel 2016.
    – GSerg
    Nov 12 at 16:37










  • Me neither, you've got different names for your Listobject columns ? i.e A B C, which will not work outside the ListObject
    – EvR
    Nov 12 at 16:37












  • I edited the question to reflect that I am using Excel 2019. The dataset resides in a table. I tested with the exact same example provided above, values as well as column names.
    – DerBob
    Nov 12 at 16:43










  • Consider changing final line to Range(srcTable.Name & "[C]").Formula = 0 -- to establish whether the argument being given to Range is the problem or the formula itself.
    – chillin
    Nov 12 at 17:02















up vote
2
down vote

favorite












I have a dataset that looks like the following:



A   B   C
1 10 145
1.1 11 0
1.2 12 0
1.3 13 0
2 14 145
2.1 15 0
2.2 16 0
3 17 145
3.1 18 0
3.2 19 0


Columns A and B in the table are my input data, column C is created with the following formula:



=IF(MOD([@A],1)=0,SUM([B]),0)


This is a simplified example and not the actual formula I am using, but it suffices as a minimal reproducible example.



When I enter this formula manually into the edit bar it works exactly as expected and produces the values in the table above. However, when I try to input this formula into the cells using VBA it fails. It gives me an error:




Error: invalid name




If I then click the cell and into the edit bar as if I wanted to manipulate the formula manually and just immediately hit enter, without actually changing anything in the formula, the error vanishes and the results get correctly displayed once more as before.



The code I use to enter the formula into the cell via VBA is the following:



Sub InputFormula()
Set srcTable = ActiveSheet.ListObjects(1)
fmtString = "=IF(MOD([@A],1)=0,SUM([B]),0)"
Range(srcTable.Name & "[C]").Formula = fmtString
End Sub


Why is the obviously correct formula broken when applied through VBA and how can I correct it so that application through VBA works?



I am using Excel 2019 on Windows 10 x64.










share|improve this question




















  • 2




    I'm not able to reproduce. I'm assuming that this dataset is a listobject/table?
    – JNevill
    Nov 12 at 16:26










  • Works as intended in Excel 2016.
    – GSerg
    Nov 12 at 16:37










  • Me neither, you've got different names for your Listobject columns ? i.e A B C, which will not work outside the ListObject
    – EvR
    Nov 12 at 16:37












  • I edited the question to reflect that I am using Excel 2019. The dataset resides in a table. I tested with the exact same example provided above, values as well as column names.
    – DerBob
    Nov 12 at 16:43










  • Consider changing final line to Range(srcTable.Name & "[C]").Formula = 0 -- to establish whether the argument being given to Range is the problem or the formula itself.
    – chillin
    Nov 12 at 17:02













up vote
2
down vote

favorite









up vote
2
down vote

favorite











I have a dataset that looks like the following:



A   B   C
1 10 145
1.1 11 0
1.2 12 0
1.3 13 0
2 14 145
2.1 15 0
2.2 16 0
3 17 145
3.1 18 0
3.2 19 0


Columns A and B in the table are my input data, column C is created with the following formula:



=IF(MOD([@A],1)=0,SUM([B]),0)


This is a simplified example and not the actual formula I am using, but it suffices as a minimal reproducible example.



When I enter this formula manually into the edit bar it works exactly as expected and produces the values in the table above. However, when I try to input this formula into the cells using VBA it fails. It gives me an error:




Error: invalid name




If I then click the cell and into the edit bar as if I wanted to manipulate the formula manually and just immediately hit enter, without actually changing anything in the formula, the error vanishes and the results get correctly displayed once more as before.



The code I use to enter the formula into the cell via VBA is the following:



Sub InputFormula()
Set srcTable = ActiveSheet.ListObjects(1)
fmtString = "=IF(MOD([@A],1)=0,SUM([B]),0)"
Range(srcTable.Name & "[C]").Formula = fmtString
End Sub


Why is the obviously correct formula broken when applied through VBA and how can I correct it so that application through VBA works?



I am using Excel 2019 on Windows 10 x64.










share|improve this question















I have a dataset that looks like the following:



A   B   C
1 10 145
1.1 11 0
1.2 12 0
1.3 13 0
2 14 145
2.1 15 0
2.2 16 0
3 17 145
3.1 18 0
3.2 19 0


Columns A and B in the table are my input data, column C is created with the following formula:



=IF(MOD([@A],1)=0,SUM([B]),0)


This is a simplified example and not the actual formula I am using, but it suffices as a minimal reproducible example.



When I enter this formula manually into the edit bar it works exactly as expected and produces the values in the table above. However, when I try to input this formula into the cells using VBA it fails. It gives me an error:




Error: invalid name




If I then click the cell and into the edit bar as if I wanted to manipulate the formula manually and just immediately hit enter, without actually changing anything in the formula, the error vanishes and the results get correctly displayed once more as before.



The code I use to enter the formula into the cell via VBA is the following:



Sub InputFormula()
Set srcTable = ActiveSheet.ListObjects(1)
fmtString = "=IF(MOD([@A],1)=0,SUM([B]),0)"
Range(srcTable.Name & "[C]").Formula = fmtString
End Sub


Why is the obviously correct formula broken when applied through VBA and how can I correct it so that application through VBA works?



I am using Excel 2019 on Windows 10 x64.







excel vba excel-vba






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 at 16:40

























asked Nov 12 at 16:18









DerBob

236




236








  • 2




    I'm not able to reproduce. I'm assuming that this dataset is a listobject/table?
    – JNevill
    Nov 12 at 16:26










  • Works as intended in Excel 2016.
    – GSerg
    Nov 12 at 16:37










  • Me neither, you've got different names for your Listobject columns ? i.e A B C, which will not work outside the ListObject
    – EvR
    Nov 12 at 16:37












  • I edited the question to reflect that I am using Excel 2019. The dataset resides in a table. I tested with the exact same example provided above, values as well as column names.
    – DerBob
    Nov 12 at 16:43










  • Consider changing final line to Range(srcTable.Name & "[C]").Formula = 0 -- to establish whether the argument being given to Range is the problem or the formula itself.
    – chillin
    Nov 12 at 17:02














  • 2




    I'm not able to reproduce. I'm assuming that this dataset is a listobject/table?
    – JNevill
    Nov 12 at 16:26










  • Works as intended in Excel 2016.
    – GSerg
    Nov 12 at 16:37










  • Me neither, you've got different names for your Listobject columns ? i.e A B C, which will not work outside the ListObject
    – EvR
    Nov 12 at 16:37












  • I edited the question to reflect that I am using Excel 2019. The dataset resides in a table. I tested with the exact same example provided above, values as well as column names.
    – DerBob
    Nov 12 at 16:43










  • Consider changing final line to Range(srcTable.Name & "[C]").Formula = 0 -- to establish whether the argument being given to Range is the problem or the formula itself.
    – chillin
    Nov 12 at 17:02








2




2




I'm not able to reproduce. I'm assuming that this dataset is a listobject/table?
– JNevill
Nov 12 at 16:26




I'm not able to reproduce. I'm assuming that this dataset is a listobject/table?
– JNevill
Nov 12 at 16:26












Works as intended in Excel 2016.
– GSerg
Nov 12 at 16:37




Works as intended in Excel 2016.
– GSerg
Nov 12 at 16:37












Me neither, you've got different names for your Listobject columns ? i.e A B C, which will not work outside the ListObject
– EvR
Nov 12 at 16:37






Me neither, you've got different names for your Listobject columns ? i.e A B C, which will not work outside the ListObject
– EvR
Nov 12 at 16:37














I edited the question to reflect that I am using Excel 2019. The dataset resides in a table. I tested with the exact same example provided above, values as well as column names.
– DerBob
Nov 12 at 16:43




I edited the question to reflect that I am using Excel 2019. The dataset resides in a table. I tested with the exact same example provided above, values as well as column names.
– DerBob
Nov 12 at 16:43












Consider changing final line to Range(srcTable.Name & "[C]").Formula = 0 -- to establish whether the argument being given to Range is the problem or the formula itself.
– chillin
Nov 12 at 17:02




Consider changing final line to Range(srcTable.Name & "[C]").Formula = 0 -- to establish whether the argument being given to Range is the problem or the formula itself.
– chillin
Nov 12 at 17:02












1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










I figured out the solution to my problem:



I am using a non-English version of Excel. When I try to use the localized function names in VBA it comes up with an error as described in the question. When I use the corresponding English names though, everything works as expected.



This is counterintuitive, because when I apply conditional formatting through VBA I have to use the localized function names. Using the English ones there does not work.






share|improve this answer























  • You chose a bad example because your sample formula is a valid en-us formula, and as you said that it was accepted by the interface, I assumed you used en-us Excel. Yes, .Formula accepts only en-us formulas, and .FormulaLocal accepts the localized formulas. You don't have that choice for .FormulaArray that only works with en-us and, as you have discovered, for conditional formatting that only works with localized formulas.
    – GSerg
    Nov 12 at 17:40










  • I wanted to make the example reproducible for everybody, that's why I translated the names to English so everybody could understand them. Had I known that exactly this was the problem, I obviously wouldn't have done that :) At least it helped me to figure out what I was doing wrong.
    – DerBob
    Nov 12 at 17:46











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',
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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53266150%2fformula-works-with-manual-input-but-not-through-vba%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote



accepted










I figured out the solution to my problem:



I am using a non-English version of Excel. When I try to use the localized function names in VBA it comes up with an error as described in the question. When I use the corresponding English names though, everything works as expected.



This is counterintuitive, because when I apply conditional formatting through VBA I have to use the localized function names. Using the English ones there does not work.






share|improve this answer























  • You chose a bad example because your sample formula is a valid en-us formula, and as you said that it was accepted by the interface, I assumed you used en-us Excel. Yes, .Formula accepts only en-us formulas, and .FormulaLocal accepts the localized formulas. You don't have that choice for .FormulaArray that only works with en-us and, as you have discovered, for conditional formatting that only works with localized formulas.
    – GSerg
    Nov 12 at 17:40










  • I wanted to make the example reproducible for everybody, that's why I translated the names to English so everybody could understand them. Had I known that exactly this was the problem, I obviously wouldn't have done that :) At least it helped me to figure out what I was doing wrong.
    – DerBob
    Nov 12 at 17:46















up vote
0
down vote



accepted










I figured out the solution to my problem:



I am using a non-English version of Excel. When I try to use the localized function names in VBA it comes up with an error as described in the question. When I use the corresponding English names though, everything works as expected.



This is counterintuitive, because when I apply conditional formatting through VBA I have to use the localized function names. Using the English ones there does not work.






share|improve this answer























  • You chose a bad example because your sample formula is a valid en-us formula, and as you said that it was accepted by the interface, I assumed you used en-us Excel. Yes, .Formula accepts only en-us formulas, and .FormulaLocal accepts the localized formulas. You don't have that choice for .FormulaArray that only works with en-us and, as you have discovered, for conditional formatting that only works with localized formulas.
    – GSerg
    Nov 12 at 17:40










  • I wanted to make the example reproducible for everybody, that's why I translated the names to English so everybody could understand them. Had I known that exactly this was the problem, I obviously wouldn't have done that :) At least it helped me to figure out what I was doing wrong.
    – DerBob
    Nov 12 at 17:46













up vote
0
down vote



accepted







up vote
0
down vote



accepted






I figured out the solution to my problem:



I am using a non-English version of Excel. When I try to use the localized function names in VBA it comes up with an error as described in the question. When I use the corresponding English names though, everything works as expected.



This is counterintuitive, because when I apply conditional formatting through VBA I have to use the localized function names. Using the English ones there does not work.






share|improve this answer














I figured out the solution to my problem:



I am using a non-English version of Excel. When I try to use the localized function names in VBA it comes up with an error as described in the question. When I use the corresponding English names though, everything works as expected.



This is counterintuitive, because when I apply conditional formatting through VBA I have to use the localized function names. Using the English ones there does not work.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 12 at 17:21

























answered Nov 12 at 17:12









DerBob

236




236












  • You chose a bad example because your sample formula is a valid en-us formula, and as you said that it was accepted by the interface, I assumed you used en-us Excel. Yes, .Formula accepts only en-us formulas, and .FormulaLocal accepts the localized formulas. You don't have that choice for .FormulaArray that only works with en-us and, as you have discovered, for conditional formatting that only works with localized formulas.
    – GSerg
    Nov 12 at 17:40










  • I wanted to make the example reproducible for everybody, that's why I translated the names to English so everybody could understand them. Had I known that exactly this was the problem, I obviously wouldn't have done that :) At least it helped me to figure out what I was doing wrong.
    – DerBob
    Nov 12 at 17:46


















  • You chose a bad example because your sample formula is a valid en-us formula, and as you said that it was accepted by the interface, I assumed you used en-us Excel. Yes, .Formula accepts only en-us formulas, and .FormulaLocal accepts the localized formulas. You don't have that choice for .FormulaArray that only works with en-us and, as you have discovered, for conditional formatting that only works with localized formulas.
    – GSerg
    Nov 12 at 17:40










  • I wanted to make the example reproducible for everybody, that's why I translated the names to English so everybody could understand them. Had I known that exactly this was the problem, I obviously wouldn't have done that :) At least it helped me to figure out what I was doing wrong.
    – DerBob
    Nov 12 at 17:46
















You chose a bad example because your sample formula is a valid en-us formula, and as you said that it was accepted by the interface, I assumed you used en-us Excel. Yes, .Formula accepts only en-us formulas, and .FormulaLocal accepts the localized formulas. You don't have that choice for .FormulaArray that only works with en-us and, as you have discovered, for conditional formatting that only works with localized formulas.
– GSerg
Nov 12 at 17:40




You chose a bad example because your sample formula is a valid en-us formula, and as you said that it was accepted by the interface, I assumed you used en-us Excel. Yes, .Formula accepts only en-us formulas, and .FormulaLocal accepts the localized formulas. You don't have that choice for .FormulaArray that only works with en-us and, as you have discovered, for conditional formatting that only works with localized formulas.
– GSerg
Nov 12 at 17:40












I wanted to make the example reproducible for everybody, that's why I translated the names to English so everybody could understand them. Had I known that exactly this was the problem, I obviously wouldn't have done that :) At least it helped me to figure out what I was doing wrong.
– DerBob
Nov 12 at 17:46




I wanted to make the example reproducible for everybody, that's why I translated the names to English so everybody could understand them. Had I known that exactly this was the problem, I obviously wouldn't have done that :) At least it helped me to figure out what I was doing wrong.
– DerBob
Nov 12 at 17:46


















draft saved

draft discarded




















































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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53266150%2fformula-works-with-manual-input-but-not-through-vba%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

Guess what letter conforming each word

Port of Spain

Run scheduled task as local user group (not BUILTIN)