There would be a scenario where customer ask for the report to get download into Excel and depending upon the N number(distinct) of LOVs of a Hierarchical Dimension, the excel report should generate N number(distinct) of tabs dynamically, also each tab should show the information related to a individual value of LOVs as well as naming the Tab name on the LOVs. But there is a another problem that there is a query prompt on this Hierarchical Dimension, hence its next to impossible to create N number of report in same document.
For Example:
There is Monthly Profit & Loss Report, which is has a section on Profit Center , and there are more than a 100 Distinct Profit centers per Profit Center Node and the report filter is on Profit Center hierarchy Node.
The requirement is to generate with n number of tabs (n = number of profit centers in the Profit Center hierarchy Node) in the Report.
The Solution:
The using Excel VBA Macro it can be achieve. We have developed a report that has a header on Profit Center ,as mentioned below
Header 1 | Header 2 | Header 3 | Header 4 | Header 5 |
---|---|---|---|---|
Profit Center | GL Account Code | Account Description | Measures1 / Calculation | Measures 2 / Calculation |
India | X0001 | Revenue | ||
India | X0002 | Salary | ||
Nepal | X0003 | Revenue | ||
Nepal | X0004 | Salary |
Then we have created a schedule and to run of the prompt values and export the report in excel,
Exporting Data From Web Intelligence Document
SAP Business Objects Web Intelligence provides the functionality to export data to PDF, Excel, CSV, or text files in SAP Business Objects 4.x. The whole document can be exported or just a single report tab. Prior to exporting data from Web Intelligence Document, let’s consider the resultant export file’s “format characteristics” depending upon the option selected:
- PDF and Excel will retain the formatting and grouping established in the Web Intelligence report.
- Excel will not include the header and footer displayed in the Web Intelligence report.
- A text file will keep the groupings and columns, but not the formatting (e.g., header row color) from the Web Intelligence report.
- A CSV (comma separated values) file will not retain the formatting nor the grouping displayed in the Web Intelligence report. This is the best choice if you want the ‘raw data’ from a Web Intelligence report.
Excel VBA Macro to Generates Tabs :
The tabs would be generated depending on the header values in the report block, in this example it would be the values of Profit Center. This VBA Code will be used at Client Machine
EXCEL VBA Macro :
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Sub Copy_To_Worksheets()
'Validation of worksheet Name and run for that worksheet only
If ActiveSheet.Name = "PROFIT CENTER Name Split" Then
' Remove the blank rows which doesn't have corresponding Profit Center Values.
Dim LR As Long, i As Long
LR = Range("A" & Rows.Count).End(xlUp).Row
Application.ScreenUpdating = False
For i = LR To 1 Step -1
If Range("A" & i).Value = 0 And Range("A" & i).Value = "" Then Rows(i).Delete
Next i
Application.ScreenUpdating = True
'Create and Copy master report to worksheets as per profit center
'Note: This macro use the function LastRow
Dim My_Range As Range
Dim FieldNum As Long
Dim CalcMode As Long
Dim ViewMode As Long
Dim ws2 As Worksheet
Dim Lrow As Long
Dim cell As Range
Dim CCount As Long
Dim WSNew As Worksheet
Dim ErrNum As Long
'Set filter range on ActiveSheet: A1 is the top left cell of your filter range
'and the header of the first column, J is the last column in the filter range.
'You can also add the sheet name to the code like this :
'Worksheets("Sheet1").Range("A1:J" & LastRow(Worksheets("Sheet1")))
'No need that the sheet is active then when you run the macro when you use this.
Set My_Range = Range("A1:J" & LastRow(ActiveSheet))
My_Range.Parent.Select
If ActiveWorkbook.ProtectStructure = True Or _
My_Range.Parent.ProtectContents = True Then
MsgBox "Sorry, not working when the workbook or worksheet is protected", _
vbOKOnly, "Copy to new worksheet"
Exit Sub
End If
'This example filters on the first column in the range(change the field if needed)
'In this case the range starts in A so Field:=1 is column A, 2 = column B, ......
FieldNum = 1
'Turn off AutoFilter
My_Range.Parent.AutoFilterMode = False
'Change ScreenUpdating, Calculation, EnableEvents, ....
With Application
CalcMode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
.EnableEvents = False
End With
ViewMode = ActiveWindow.View
ActiveWindow.View = xlNormalView
ActiveSheet.DisplayPageBreaks = False
'Add a worksheet to copy the a unique list and add the CriteriaRange
Set ws2 = Worksheets.Add
With ws2
'first we copy the Unique data from the filter field to ws2
My_Range.Columns(FieldNum).AdvancedFilter _
Action:=xlFilterCopy, _
CopyToRange:=.Range("A1"), Unique:=True
'loop through the unique list in ws2 and filter/copy to a new sheet
Lrow = .Cells(Rows.Count, "A").End(xlUp).Row
For Each cell In .Range("A2:A" & Lrow)
'Filter the range
My_Range.AutoFilter Field:=FieldNum, Criteria1:="=" & _
Replace(Replace(Replace(cell.Value, "~", "~~"), "*", "~*"), "?", "~?")
'Check if there are no more than 8192 areas(limit of areas)
CCount = 0
On Error Resume Next
CCount = My_Range.Columns(1).SpecialCells(xlCellTypeVisible) _
.Areas(1).Cells.Count
On Error GoTo 0
If CCount = 0 Then
MsgBox "There are more than 8192 areas for the value : " & cell.Value _
& vbNewLine & "It is not possible to copy the visible data." _
& vbNewLine & "Tip: Sort your data before you use this macro.", _
vbOKOnly, "Split in worksheets"
Else
'Add a new worksheet
Set WSNew = Worksheets.Add(After:=Sheets(Sheets.Count))
On Error Resume Next
WSNew.Name = cell.Value
If Err.Number > 0 Then
ErrNum = ErrNum + 1
WSNew.Name = "Error_" & Format(ErrNum, "0000")
Err.Clear
End If
On Error GoTo 0
'Copy the visible data to the new worksheet
My_Range.SpecialCells(xlCellTypeVisible).Copy
With WSNew.Range("A1")
' Paste:=8 will copy the columnwidth in Excel 2000 and higher
' Remove this line if you use Excel 97
.PasteSpecial Paste:=8
.PasteSpecial xlPasteValues
.PasteSpecial xlPasteFormats
Application.CutCopyMode = False
.Select
End With
End If
'Show all data in the range
My_Range.AutoFilter Field:=FieldNum
Next cell
'Delete the ws2 sheet
On Error Resume Next
Application.DisplayAlerts = False
.Delete
Application.DisplayAlerts = True
On Error GoTo 0
End With
'Turn off AutoFilter
My_Range.Parent.AutoFilterMode = False
If ErrNum > 0 Then
MsgBox "Rename every WorkSheet name that start with ""Error_"" manually" _
& vbNewLine & "There are characters in the name that are not allowed" _
& vbNewLine & "in a sheet name or the worksheet already exist."
End If
'Restore ScreenUpdating, Calculation, EnableEvents, ....
My_Range.Parent.Select
ActiveWindow.View = ViewMode
With Application
.ScreenUpdating = True
.EnableEvents = True
.Calculation = CalcMode
End With
' Delete a worksheet named as Profit Center Name , which is generate by above code as
For Each ws In Sheets
Application.DisplayAlerts = False
If ws.Name = "Profit Center Name" Then ws.Delete
Next
Application.DisplayAlerts = True
Else
MsgBox "Macro Is made to run for Worksheet: PROFIT CENTER DETAIL SPLIT only in Monthly P&L Variance Report"
Exit Sub
End If
End Sub
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Results
There are around 62 tabs are generated dynamically and the format of the base report is remain same across all the worksheets, even the data is sliced properly.
Excel Report Format
Feel free to provide me your feedback.