Exporting formulas to excel from ssrs 2008R2 - excel

I am aware that in 2008 the method of exporting formula to excel was downgraded see here.
But I have seen a workaround for a static formula where the same formula is required (see Here and here). However I have the case where I need a relative formula when I export to excel. Is there a newer solution out there does anyone know?
This is my workings so far
The result I am after is cell (T,r3) = cell (R,r3)-cell(O,r3) and then cell (T,r4) = cell (R,r4)-cell(O,r4) and so on in excel I would expect to see
in cell T3 the formula =R3-O3
and cell T4 the formula =R4-O4
O!P!Q!R !S!T!
r3 1 5 N 5 4
r4 3 5 N 10 7
(Sorry I can not get the table to line up)
my ssrs expression is
="=R"+cstr(RowNumber("DataSet1")+2)+"-O"+cstr(RowNumber("DataSet1")+2)
where DataSet1 is what my ssrs tablix is based on and the +2 element is to offset to allow how the titles of the report and the table move the first row of data down the excel sheet.
The output is achieve in the cells when exported to excel is the formula as text in the cell content and if I click into the cell and press backspace and return the formula works. It appears that some white space is being exported or excel just treats every thing as text on the import routine
I can of course use and find and replace on this statement in excel
="=R"+cstr(RowNumber("DataSet1")+2)+"-O"+cstr(RowNumber("DataSet1")+2) as suggested but was trying to be more elegant
I do not suppose you can export some code to run in excel as vba to do the find and replace function at a button press for the user?
Has anyone any suggestions - thanks
Ian

Since this is not supported the workaround is tedious. For each cell that has a formula you can:
Select the cell
Click in the formula bar
Press Enter
This causes Excel to evaluate the string as a formula and replaces the text with the value it represents.
You can't export VBA from SSRS but you could paste in your own VBA after export and run it to accomplish the same steps.

I use SSIS to populate an Excel report template with data and formulas.
I had too many formulas to have to go into the worksheets and updated them manually.
In a script task, I use VB to calculate the formula and populate it in a sheet as well as underline the appropriate line:
ExcelFormula = "=SUM(" & SumColumn & "45:" & SumColumn & Row.ToString & ")"
Worksheet.Cells(Row, Column - 1).Formula = ExcelFormula
Worksheet.Cells(Row, Column - 1).BORDERS(9).Weight = 2
Worksheet.Cells(Row, Column).BORDERS(9).Weight = 2
It's a bit more work initially but once you get it set up to work with one cell, the rest are relatively easy.

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

How do I write VBA script so that it will compare data?

We need a script to compare the dates in column D to the dates in column E.
If the date in column D is two days before the date in column E then we need column F to state that in the corresponding row to D. We have a range of 2 days before and 2 days after (shoulder days). We need to be able to easily reproduce this and have it be able to run when we import it into access. we have roughly 3300 unique days to check.
I have tried using datevalue or a formula but we need to be able to reproduce this in the future and the formula doesn't allow us to easily do that.
I will not provide a complete implementation, but I can show you the way how you can do it (either the Excel formula way or with VBA code). Both ways will automatically calculate the values you need so you can easily import it into Microsoft Access - the import will take the calculated values from the cells you've selected.
The first way, which is also the simplest way, is to do that with simple Excel formula, consider this picture showing how the formula will calculate the values in columns F and G:
(Note: It is showing German date format, but you can easily change that in Excel).
In column G it calculates the difference D2 - D1 and displays it in days.
Then, in column F there is a simple if condition to determine if the date D1 is less, greater or equal than D2. The Result is calculated automatically.
The formula in row 2 are:
Hint: You can drag and drop the formula to the rows 3 - n below, so you can create a lot of rows prefilled with that formula.
A second way is that you can write a VBA function for the (simple) calculation formula in column F:
Function CalcResult(D1 As Variant, D2 As Variant) As Variant
CalcResult = ""
Dim result As String
Dim diff As Single
diff = D2 - D1
If (diff > 0) Then
result = "D1 < D2"
ElseIf (diff = 0) Then
result = "D1 = D2"
ElseIf (diff < 0) Then
result = "D1 > D2"
End If
CalcResult = result
End Function
This needs to be entered in the VBA editor, which you can display if you press Alt+F11. After you have done that, press Alt+F11 again to close VBA and return to your Excel sheet.
Then, place the formula =CalcResult(E2; D2) in cell F2 as shown below:
Like in the previous example, you can drag & drop the formula to the rows 3 - n below, so you can create a lot of rows prefilled with that formula.
The advantage of the 2nd approach is that you can refine the function CalcResult later without having to change the cells again (as it is the case in the first example).
Note: The function above needs to be in a separate module and you need to save the workbook as "Macro enabled workbook" later - otherwise you'll lose the VBA code.
A third way is to use the Excel macro recorder and record whatever you intend to do. It will create a public module with VBA code. If the recorder asks you, choose to store the code in the workbook.
Later you re-visit the generated VBA code and refine it - for example, put a for loop around it to automate things you've recorded once.
This approach is good for creating a "Calculate" button and put some logic behind it.

Formula bar values excel vba

I have value in cells 10.0 but in the formula bar is 10 so if I copy
The value to another cell like
Range(”a1”)= Range(”b1”) I got 10 instead 10.0
Or I copy to another workbook
For display value work good but for formula bar no
I used number format did not work so if you can help me
Excel vba
Thank’s
#Jeeped, Gosh you're not wrong. Not sure what I was thinking there.
Range("a1") = Range("b1")
Range("a1").NumberFormat = "#0.0"

Write IF statement using FormulaR1C1 in VBA Excel

I have the following formula
=IF((GLOBAL_DATE-30)<G2,"1 Month",IF((GLOBAL_DATE-60)<G2,"2 Month",IF((GLOBAL_DATE-90)<G2,"3 Month","Older Than 3 Months")))
and I would like to write this into specific cells using the FormualR1C1 in VBA.
(the GLOBAL_DATE is a named cell on another sheet)
Thanks
Select the cell that has that formula. In the VBE, go to the Immediate Window and type
?Activecell.FormulaR1C1
and press enter. That will give you the R1C1 translation of your formula.
This is what I get using the Macro Recorder:
ActiveCell.FormulaR1C1 = _
"=IF((GLOBAL_DATE-30)<R[1]C[6],""1 Month"",IF((GLOBAL_DATE-60)<R[1]C[6],""2 Month"",IF((GLOBAL_DATE-90)<R[1]C[6],""3 Month"",""Older Than 3 Months"")))"
That is using relative cell addresses (R[1]C[6] is the cell one row below and 6 columns to the rigth from the ActiveCell. Alternatively, you can use absolute adresses by replacing R[1]C[6] by R2C7 (for row 2, column 7 = G2).
You can easily use VBA to translate those formulae that you entered in a sheet into a sytax that's suitable for VBA. I once wrote a sub for that purpose.

excel sheet sub headings as columns

I am totally new to this kind of challenges and not sure any thing available ( not sure even on what base I have to search )
In the below excel sheet image the column 'A' has headings in two places ( row numbers 2,3 and 9,10) . The actual excel sheet has more than six thousand rows and too many sub headings like this ( If it is small file I can do it manually.. but more than 6 thousand rows)
The challenge :- I want to populate E column with "Make" value and F column with " Model" from sub headings . Can I write any rule or macro to populate these columns ? could some one help me ? Thanks for your help
Image Link
or below
Regards
Kiran
If you want to do this solely in Excel you can use the following. This assumes
All Headings are the same for "S.No"
Change the SUBSTITUTE clause to match the text for Make and Model eg I have used exact spacing of "Model: " and "Make : " to match the spreadsheet and substitute with ""
In cell G5 Enter =IF(ISNUMBER(A5),IF(ISERROR(FIND("Model",A3,1)),MAX($G$1:G4),MAX($G$1:G4)+1),"")
In cell F5 Enter =SUBSTITUTE(IF(ISNUMBER(A5),INDIRECT(ADDRESS(MATCH($G5,$G:$G,0)-2,COLUMN(A1),1)),""),"Model: ","")
In cell E5 Enter = =SUBSTITUTE(IF(ISNUMBER(A5),INDIRECT(ADDRESS(MATCH($G5,$G:$G,0)-3,COLUMN(A1),1)),""),"Make : ","")
Then drag down the formula in E5:G5 to wherever you need. However, I only recommend using this once only as the formulae will be slow to update over large ranges. Also if your headings are out of sync then VBA is the way forward

Resources