BEx Macro Code PDF
BEx Macro Code PDF
Applies to:
Development and support based on SAP BI 7.0 or SAP BW 3.5.
Summary
This article focuses on when it might be viable to use VB in BEx workbooks to build on standard BEx
features. Different ways of incorporating VB in workbooks, as well as pros and cons of using VB with
standard SAP BI are covered
Author Bio
Author is a BI Consultant having worked in all phases of Project Implementation in SAP BI/BW in a global
delivery environment. She also has experience in SAP Variant Configuration and in the Manufacturing
Industry.
Table of Contents
Why do we need VB in BI Workbooks?..............................................................................................................3
Standard VBA Routines provided by SAP ......................................................................................................3
Scenarios where VB can be used...................................................................................................................4
When to use VB in BI Workbooks- A Business Case.........................................................................................4
Solution Options..............................................................................................................................................5
How VB is incorporated with BEx .......................................................................................................................5
Logic written in VB Macro ...............................................................................................................................7
Pros and Cons of Using VB in BEx Workbooks: ................................................................................................8
Sample Code in VB for achieving this functionality: ...........................................................................................9
Related Content................................................................................................................................................15
Disclaimer and Liability Notice..........................................................................................................................16
For example one can use the following sample macro in BI Workbooks:
Note: The Totals for Sales are calculated by multiplying each Cost type (Landed, FOB, Sales Price) with the Sales Key
Figure and then summing up for the entire Product Type. The Same holds for Totals Purchase and Totals
Inventory.
Solution Options
1. Create a Calculated Key Figure for each of the Totals: Since Cost is not assigned to any month
multiplying cost with Sales or Purchase will not give the desired results as for each Calmonth one of
the factors will always be Null.
For example:
Calmonth = Oct, Purchase for Material ABC = 200 and Landed Cost KF = Null.
Calmonth = # (Not Assigned) Purchase for Material ABC = Null and Landed Cost KF = 20
2. Using two queries linked with RRI: The First Query includes only the monthly values. The second
query contains the Totals. In this design calculation of the Totals has to be done on the back end in
the cubes itself. An RRI will be used to link the two queries. However, this may not accepted by
business since this would mean swapping between 2 reports to link the PSI figures with the financial
totals projected in the 2nd report.
3. Translating Costs to Monthly Buckets and creating KFs as in option 1: In this case there would be 9
additional columns for each Calmonth. Landed Cost, FOB, Sales Price, Total Landed Cost- Sales,
Total Landed Cost-Purchase, Total FOB Cost- Sales, Total FOB Cost-Purchase, Total Sales Price-
Sales, Total Sales Price Purchase. This is a huge deviation from the format requested by the
customer.
4. So in order to obtain a closest fit to customer requirement a VB solution using Macros in the
workbook was recommended. Here Totals are calculated after Query Run Time using VBA Code.
From the Forms Toolbar add a Button called Show Totals to the Report Output Sheet as shown below:
Now Code is written in a Macro in the Visual Basic Editor and Macro is assigned to the Show Totals Button
seen above:
Note: For simple macros the Macro Recorder can be used to convert into VBA code all actions done on the Excel file in
the frontend. This is especially helpful for new users who are not familiar with VB syntax and standards.
This is to be done every time 'Result' appears in the Column B and for every Calmonth appearing in the
Report, in other words for every Material Type the three Totals will be calculated.
Workbook Output on Executing the Macro:
Here we see three new rows have been added to the Query Output: Total Landed Cost, Total FOB Cost &
Total Sales Price
Also the values in these rows have been calculated using the logic given above. Some cosmetic changes are
also done in the output.
Thus by using VB in Workbook we are able to meet the customer requirement of seeing Key Figures as well
as Totals for each Category in the same sheet at the press of a button.
Instead of using a button, the function SAPBEXonRefresh can also be used to run the macro. The choice
can be made depending on user preferences.
iAddrs = rCell.Address
rCell.Activate
For iloop = 1 To lCount
Call InsROWS(iAddrs) ' Calling Insert rows Function
Set rCell = Columns(2).FindNext(After:=ActiveCell)
rCell.Activate
iAddrs = rCell.Address
Next iloop
End If
End Sub
Sub InsROWS(iAddrs As String)
'Function to Insert the 3 rows below the Result cell found in Column B
If Not Range(iAddrs).Offset(1, 0) = "Total Landed Cost" Then
Range(iAddrs).Offset(1, 0).Activate
Range(iAddrs).Offset(1, 0).EntireRow.Insert
Selection.Value = "Total Landed Cost"
ActiveCell.EntireRow.Select
Selection.NumberFormat = "#,##0.00"
End If
If Not Range(iAddrs).Offset(2, 0) = "Total FOB" Then
Range(iAddrs).Offset(2, 0).Activate
Range(iAddrs).Offset(2, 0).EntireRow.Insert
Selection.Value = "Total FOB"
ActiveCell.EntireRow.Select
Selection.NumberFormat = "#,##0.00"
End If
Sub CalcResult()
'Function to calc the Values in Rows Total Landed Cost, TOtal FOB, Total Sales Price
Dim rCell As Range
Dim colValue As Long
Dim RowHdVal As Long
Dim AdrsLanded As String
Dim AdrsFOB As String
Dim AdrsSalesP As String
Dim PurLstCell As Double
Dim PurFOB As Double
Dim PurSP As Double
Dim AdrsPur As String
Dim SalLand As Double
Dim SalFOB As Double
Dim SalSP As Double
Dim InvLand As Double
Dim InvFOB As Double
Dim InvSP As Double
Dim AdrsInv As String
Dim AdrsSal As String
Dim n As Long
Dim lCount As Long
rNext = RowHdVal
For iloop = 1 To lCount
rPrevious = rNext + 1
Rows(RowHdVal + 1).Select
Selection.Find(What:="Landed Cost", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
Find the address of cells where value is Total Landed Cost, Total FOB Cost, Total Sales Price
For jLoop = 1 To colValue
If Cells(RowHdVal + 1, jLoop).Value = "Landed Cost" Then
AdrsLanded = Cells(rNext, jLoop).Address
End If
If Cells(RowHdVal + 1, jLoop).Value = "FOB Cost" Then
AdrsFOB = Cells(rNext, jLoop).Address
End If
If Cells(RowHdVal + 1, jLoop).Value = "Sales Price" Then
AdrsSalesP = Cells(rNext, jLoop).Address
End If
Next jLoop
For kLoop = 1 To colValue
'Loop through the header row till the last empty Cell in that row
PurLstCell = 0
PurFOB = 0
PurSP = 0
SalLand = 0
SalFOB = 0
SalSP = 0
InvLand = 0
InvFOB = 0
InvSP = 0
Calculate Totals in Inventory Column
If Cells(RowHdVal + 1, kLoop).Value = "Inventory" Then
AdrsInv = Cells(rNext, kLoop).Address
n=1
For p = rNext To rLstCell
n=1
For p = rNext To rLstCell
If Cells(p, 2).Value = "Result" Then
p = rLstCell
Else
SalLand = SalLand + (Range(AdrsSal).Offset(n + 2, 0) * Range(AdrsLanded).Offset(n + 2, 0))
SalFOB = SalFOB + (Range(AdrsSal).Offset(n + 2, 0) * Range(AdrsFOB).Offset(n + 2, 0))
SalSP = SalSP + (Range(AdrsSal).Offset(n + 2, 0) * Range(AdrsSalesP).Offset(n + 2, 0))
n=n+1
End If
Next p
'Enter the Calc values into their respective cells
Range(AdrsSal).Offset(n, 0).Select
ActiveCell.Value = SalLand
Range(AdrsSal).Offset(n + 1, 0).Select
ActiveCell.Value = SalFOB
Range(AdrsSal).Offset(n + 2, 0).Select
ActiveCell.Value = SalSP
End If
Related Content
http://help.sap.com/saphelp_nw04/helpdata/en/f1/0a55f9e09411d2acb90000e829fbfe/frameset.htm
https://www.sdn.sap.com/irj/scn/go/portal/prtroot/docs/library/uuid/70a6fd1b-21c9-2b10-ba9b-ed7660b8a579
https://www.sdn.sap.com/irj/scn/thread?messageID=1719655
For more information, visit the Business Intelligence homepage.