I am fairly new at programming. I feel like this should be a simple fix but I cannot find anything that works. I am trying to use a Select Case structure to write a different formula to a cell depending on the selected case. If I type:
Sheet1.Range("g10").Value = "=IF(SUM(F10)>0,SUM((F10-15)),"")"
I get an error:
Run-time error '1004'
Application-defined or object-defined error.
I can get it to work if I include spaces like so:
Sheet1.Range("g10").Value = " =IF(SUM(F10)>0,SUM((F10-15)),"") "
but then Excel puts it into the cell like text, not as a formula (it doesn't do anything).
I have tried a few different methods but have run into the same issue. Is it possible to do this?
I apologize if this has been asked and answered, but I wasn't able to find anyone referencing this specific problem.
You don't need to sum a single cell ie SUM(F10) Also SUM((F10-15)) is the same as F10-15.
Try this: Sheet1.Range("g10").Formula = "=IF(F10>0,F10-15,"""")" Also note, you needed to double the double quotes towards the end.
Note, you can do this on a range too like this:
Sheet1.Range("g10:g20").Formula = "=IF(F10>0,F10-15,"""")" and it is smart enough to increment the references for you for the 10 cells.
Wrong property. You need .formula not .value
worksheets("sheet1").range("g10").formula = "=IF(SUM(F10)>0,SUM((F10-15)),"")"
You should be able to type out the formula and then turn on the Macro Recorder and double-click on the cell with the forums, then turn off the Macro recorder and examine your code. That should give you what you want.
I'm trying to record a macro of my formula in Excel and it gives me a syntax error.
This is the formula:
=IF(AND(OR(B2="toola",B2="toola1",B2="toola2",B2="SFx200"),OR(H2="Q1",H2="Q2",H2="Q3",H2="Q4")),CONCATENATE(H2," "," ",IF(I2="p","pre","")," ","SFx","-",IF(A2="",0,A2)),IF(AND(OR(B2="toolb",B2="toolb1",B2="toolb2",B2="toolb3",B2="yoolb4",B2="toolb4",B2="toolb5",B2="toolb6",B2="toolb7",B2="toolb8",B2="toolb9",B2="toolb10",B2="toolb11",B2="toolb12"),OR(H2="Q1",H2="Q2",H2="Q3",H2="Q4")),CONCATENATE(H2," ",IF(I2="p","pre",""),"-"," ",IF(A2="",0,A2)),IF(AND(OR(B2="up",B2="up1",B2="up2",B2="up3",B2="up4",B2="up5",B2="Up6",B2="up7"),OR(H2="Q1",H2="Q2",H2="Q3",H2="Q4")),CONCATENATE(H2," ","UP","-",IF(A2="",0,A2)))))
I know that it is big, but why won't the macro recorder properly record it?
The macro recorder is useless with long formulas as it overwrites some of the formula string when it adds in line continuations for the VB editor. You'll either need to edit the code yourself or shorten the formula - e.g. use things like OR(H2={"Q1","Q2","Q3","Q4"}) rather than OR(H2="Q1",H2="Q2",H2="Q3",H2="Q4")
Also if possible replace something like this:
OR(B2="toolb",B2="toolb1",B2="toolb2",B2="toolb3",B2="yoolb4",B2="toolb4",B2="toolb5",B2="toolb6",B2="toolb7",B2="toolb8",B2="toolb9",B2="toolb10",B2="toolb11",B2="toolb12")
with this
LEFT(B2,5)="toolb"
When you have more than 7 times if excel does not want to work. Thus, you should go around it. With Select Case.
Or something like this, when the nested ifs are in a formula:
https://www.techonthenet.com/excel/macros/if_custom.php
You can go up to as many cases as you need. Then simply use the custom formula.
I'm using VBA, and I need to insert an array formula (the one that if I'm writing it manually, I'll press Ctrl+Shift+Enter and not just Enter). When I'm inserting it like a regular formula it doesn't work, neither when I put it with {} around it...
What's the correct way of writing that formula using VBA?
The formula is this:
=INDEX(subset!R1C1:R2472C10,MATCH(1,(RC1=subset!C1)*(RC2=subset!C2)*(RC5=subset!C5)*(RC6=subset!C6),0),10)
You're looking for the FormulaArray property that you can set for a cell like so:
Range("A1").FormulaArray = "=INDEX(subset!R1C1:R2472C10,MATCH(1,(RC1=subset!C1)(RC2=subset!C2)(RC5=subset!C5)*(RC6=subset!C6),0),10)"
See the documentation here: http://msdn.microsoft.com/en-us/library/office/ff837104%28v=office.15%29.aspx
I am writing data into an excel spreadsheet. The data comes from an Access Database. Code like the line below works:
xl.cells(5,5)="joe"
but code like the line below does not:
xl.cells(5,5)="1/2CD438"
I get an error like "Application defined or object defined error"
I think Excel is trying to do a calculation, and the calculation fails. All I really want is the text string.
I've tried formating the column like
columns(1).NumberFormat="#"
, but it doesn't work either.
Any ideas?
Thanks for all the input. I had tried several of your ideas without success. As it turns out I was using variables to determine the exact row, column as:
xl.cells(NextRow, Col) = ars!PartNo
By simply replacing NextRow with an integer, the code worked. Replaced the integer in the test with NextRow, and the code ran. I suspect a misspelling or something, but couldn't see it looking at the code.
Thanks for the suggestions.
I'm trying to use either PERCENTILE.EXC, PERCENTILE.INC or PERCENTILE.
Looking at FormulaParserManager.GetImplementedFunctionNames() these are not implemented functions.
I wondered if I could set the formula and leave it to Excel to calculate. So far I've not got this to work and I get a #NAME? and "The formula contains unrecognized text". Merely clicking in the formula bar causes the formula to be calculated correctly.
Inspecting the internals of the Excel file I am creating (via EPPlus):
_xludf.PERCENTILE.EXC(B14:B113,0.95)
whereas in Excel I get:
_xlfn.PERCENTILE.EXC(A14:A113,0.95)
I think this is user defined function vs function. I've tried prefixing "_xlfn." to my formula string.
This is as far as I've got I think I either need to roll my own percentile calculation in code or manipulate the xml in the Excel file maybe.
Any help appreciated.
Doh! I spend all afternoon stuck, post a question here and then immediately suss the answer...
Anyway in case anyone is interested:
var row95 = percentile95RowLookup[profileKey];
var percentileRange = sheet.Cells[row, i, row+ profileCollection.Profiles.Count-1, i];
sheet.Cells[row95, i].Formula = $"_xlfn.PERCENTILE.INC({percentileRange.Address},0.95)";
With the important proviso that workbook.CalcMode is not Manual.