Adding complicated INDEX formula results in Excel file error - excel

I have a problem inserting this formula in Excel spreadsheet using OpenXML SDK.
=INDEX(Codes[[#All];[code1:]];MATCH(MySheet!B1674;Codes[[#All];[code2:]];0))
I've been doing this just by adding text into a cell using method like this:
private Cell ConstructCellFormula(string formula)
{
Cell cell = new Cell();
CellFormula cFormula = new CellFormula();
cFormula.Text = formula;
cell.Append(cFormula);
return cell;
}
I know that I'm missing something in calling this.
The spreadsheet that I'm working on is a part of many in XLSM file.
Codes
is also there.
What I'm doing is - I remove all existing rows and add new based on data from the database. That's when problems with the file start.
Any help will be appreciated.

Solution was to simply use EN-US syntax with commas instead of semi-colons.
=INDEX(Codes[[#All],[code1:]],MATCH(MySheet!B1674,Codes[[#All],[code2:]],0))

Related

Find discrepancy between specific columns in 2 worksheets

I am working with excel, let's say worksheet ABC with columns C3:O102 contains "-" while worksheet DEF with columns C3:O102 contains "yes". I want the cells with discrepancy to return "Discrepancy". I tried excel formulas, I couldn't figure it out so I tried with VBA and it didn't run.
Sub IF_Then()
If Worksheets("Sheet15").Range("C3:O102").Value = "-" And Worksheets("Sheet30").Range("C3:O102").Value = "yes" Then
Worksheets("Sheet30").Range("AJ3:AJ103").Value = "Discrepancy"
End If
End Sub
This can easily be done, using the IF() worksheet function, as in my example: I have created two sheets (Blad1 and Blad2), I have filled in cells "A1:A5", and in a third sheet, I've added the following formula in cells "A1:A5" (starting in cell "A1" and dragging down):
=IF(Blad1!A1 = Blad2!A1;Blad1!A1;"Discrepancy")
(Keep out, my regional settings require semicolons inside a formula, some require commas.)
I ended up using it without VBA. I got accurate results with this excel formula. The formula was really long because I was working with a lot of sheets and kind of like dissimilar data.
=IF(AND(INDEX(MATCH),(MATCH)))
I appreciate the contribution

Excel VBA - Convert a string Formula to an Excel Calculation in a range of cells

I'm just wondering if this is possible to do without a loop - In my excel sheet, in, say, Range("A1:C10") I have text concatenation formulas that, once concatenated, create real Excel functions.
As a stupid example, suppose I had the following in cell A1:
A1: ="=Sum(D"&C2&":E"&C3&")"
Now, I know in VBA I can do something along the following for any one specific cell:
Range("A1").Formula = Range("A1").Text
And it will convert my text formula into an Excel formula and evaluate it.
Now, what I'm curious about is, whether there a way to say, for example:
Range("A1:C10").Formula = Range("A1:C10").Text
Without looping through each cell individually?
Also, I can't use INDIRECT() as, unfortunately, my formulas refer to closed workbooks :/
Any ideas??
Range.Text contains the string representation of the cell's value. The actual calculated value (which I suspect is what you're after) is accessed using Range.Value - try this:
Range("A1:C10").Formula = Range("A1:C10").Value
Not sure if this is what you are trying to do, but if for example you use:
Range("A1:C10").Formula = "=Sum(D1:E1)"
then the relative references will be auto adjusted:
A1: =Sum(D1:E1)
A2: =Sum(D2:E2)
B1: =Sum(E1:F1)
... etc.

MS Excel - Paste a formula from a tab-delimited line

I have a Visual Studio program that reads a PDF file and scrapes data from it. The VS program then generates a tab-delimited string that is manually pasted into the spreadsheet.
Everything works fine, but my tab-delimited line erases a formula in one column. Not a big deal as I just copy the formula from the previous line.
Would it be possible to put the formula into my tab-delimited line?
Here is the formula:
=IF(AND(NOT(ISBLANK($M2666)),ISBLANK($O2666)),"y","")
If I put this into the tab-delimited line in the appropriate column, it works fine, if I happen to be inserting the tab-delimited line on row 2666.
I tried using the row() function, but then it's not a valid formula:
=IF(AND(NOT(ISBLANK($Mrow())),ISBLANK($Orow())),"y","")
I tried a function that returns the last row in a given column then made a variable to put into the formula. If I am just pasting in Excel, it works, but when I try to insert it in a tab-delimited line is pastes as text.
Remember, the tab-delimited string is being generated in a program external to the spreadsheet and the program doesn't have access to the spreadsheet to find the last used row.
So, here's the question, how do I paste a formula from the clipboard?
This question is tagged as vba so here is my VBA-based solution.
dim strFormula as string
strFormula = "=IF(AND(NOT(ISBLANK(RC13)),ISBLANK(RC15)),""y"","""")"
range("M2").formular1c1 = strFormula
range("M2666").formular1c1 = strFormula
range("M9999").formular1c1 = strFormula
The Range.FormulaR1C1 property accepts the xlR1C1 style formula that will adjust for any row you place it into.
If you place the xlR1C1 style formula into a tab delimited TXT file, the following code would be necessary before and after using VBA to import the TXT file.
dim origRefStyle as long
origRefStyle = Application.ReferenceStyle
Application.ReferenceStyle = xlR1C1
'import the tab delimited TXT here
Application.ReferenceStyle = origRefStyle
You can use INDIRECT and ADDRESS... and also ROW and COLUMN:
Please search every detail on that function if you need to know.
Say you want get value of M2666 from Q2666 with the formula INDIRECT(ADDRESS(ROW();COLUMN()-4)). Back to the example problem, with assumption the formula is inserted in Q2666, then your formula should be:
=IF(AND(NOT(ISBLANK(INDIRECT(ADDRESS(ROW();COLUMN()-4))));ISBLANK(INDIRECT(ADDRESS(ROW();COLUMN()-2))));"y";"")
Please notice that Excel uses ; not , to separate parameters in a function.

In Excel and VBA, how to set value of cell to the formulas result rather than the formula

I'm getting values from one sheet and placing them in another using a macro in Excel. I currently have this which works fine:
sheet.range("B2:B35").Value = "=IF(SUMPRODUCT(--(Raw!$B$11:$B$322=$A2),--(Raw!$D$11:$D$322=All!$B$2),Raw!$H$11:$H$322)<>0,SUMPRODUCT(--(Raw!$B$11:$B$322=$A2),--(Raw!$D$11:$D$322=All!$B$2),Raw!$H$11:$H$322),""-"")"
It, obviously, puts that entire formula as the value of the cell. What I'd like is it just to put the result of the formula into the cell. I've tried adding Evaluate() around the "IF..." part, but then the IF doesn't evaluate correctly (I just end up with "-" in each cell). Is this possible to do or do I have to have separate code to loop through and change the value to the value of the cell?
Use:
sheet.range("B2:B35").Formula = "Your formula here"
If that doesn't work you may have to change the formatting (do this first):
sheet.range("B2:B35").NumberFormat = "General"
Edit:
A solution turned out to be addition of the following line after the OP's code:
sheet.range("B2:B35").value = sheet.range("B2:B35").value

Apache POI - XSSF: Row.getCell()

I am using XSSF to access the .xlsx format. Extracting row data and cell data is being done by
Row.getCell(1) // to get the first cell data.
Is there a way to access cells like
Row.getCell(A) or Row.getCell(AC).
This will be very helpfull for me to access columns.
Can any one tell me the way to do this?
I think the main class you're looking for is CellReference - it handles converting between user facing references such as "B2" into file format references like row=1,col=1 . There's a static method on there that handles your exact use case, convertColStringToIndex
For your use case, you'd want code something like
Cell c = row.getCell( CellReference.convertColStringToIndex("G") );
The question is regarding reaching out a cell using its references, if I am not wrong.
Sheet referenceSheet = workbook.getsheet("Sheet name your intrested in");
CellReference ref = new CellReference("C24");
Row row = referenceSheet.getRow(ref.getRow());
// null check for the row
if(row != null)
{
Cell cell = row.getCell(ref.getCol());
}
In this way we can refer a cell using its reference.

Resources