Hi all
I run a simple piece of code that copies formulas into cells of a worksheet. The formulas themselves reference the Cognos Controller link API and are embedded in a IFERROR statement, e.g.:
=IFERROR(@cc.fGetVal(J$9,,,J$8,J$6,J$3,J$6,J$2,$A28,,,,,,J$4,,J$5),)
The code runs fine for instance of Excel that are part of the Office 365 package. The client is running Excel 2012 and the code returns an 'Application-defined or Object-defined error'. I have asked the client to deactivate the Cognos add-in and run the code to prevent any interference. I still get the same error. This leads me to believe it may be related to a versioning issue.
The full code is shown below; fails at .Range(.Cells(fRow, fCol), .Cells(n, m)) = Arr1:
I run a simple piece of code that copies formulas into cells of a worksheet. The formulas themselves reference the Cognos Controller link API and are embedded in a IFERROR statement, e.g.:
=IFERROR(@cc.fGetVal(J$9,,,J$8,J$6,J$3,J$6,J$2,$A28,,,,,,J$4,,J$5),)
The code runs fine for instance of Excel that are part of the Office 365 package. The client is running Excel 2012 and the code returns an 'Application-defined or Object-defined error'. I have asked the client to deactivate the Cognos add-in and run the code to prevent any interference. I still get the same error. This leads me to believe it may be related to a versioning issue.
The full code is shown below; fails at .Range(.Cells(fRow, fCol), .Cells(n, m)) = Arr1:
VBA Code:
Option Base 1
Sub fGetValue_Cognos(wsName As String, CognosLink As Boolean)
'Dim CognosLink As Boolean '!***!
'CognosLink = True
'***
'* Purpose: Replace Values with fGetValue function based on PoV fields
'* Last update: 15 November 2021
'* Author: Florian Bohlandt
'***
Dim wb As Workbook
Dim wsFin As Worksheet
Dim rngPoV As Range, rngCol As Range, rngRow As Range, rngData As Range
Dim n As Integer, m As Integer, i As Integer, j As Integer
Dim fRow As Integer, fCol As Integer, RowNum As Integer, ColNum As Integer
Dim ColRef As String
Dim Arr1() As Variant, Arr2() As Variant, Arr3() As Variant
On Error GoTo ErrorTrap '!***!
Set wb = ThisWorkbook
'Set worksheets
With wb
Set wsFin = .Sheets(wsName)
End With
'Find last row and column
n = LastCell(wsFin, True)
m = LastCell(wsFin, False)
fRow = 28 'first data row
fCol = 5 'first data col
'Define ranges (manual)
With wsFin
Set rngPoV = .Range("A6:B6") '<-- Entity
Set rngCol = .Range(.Cells(1, fCol), .Cells(1, m)) '<-- PoV (adjusts dynamically for added columns)
Set rngRow = .Range(.Cells(fRow, 1), .Cells(n, 2)) '<-- Accounts (adjusts dynamically for added rows)
Set rngData = .Range(.Cells(fRow, fCol), .Cells(n, m)) '<-- Data
End With
RowNum = rngData.Rows.Count
ColNum = rngData.Columns.Count
ReDim Arr1(RowNum, ColNum)
ReDim Arr2(RowNum, 1)
ReDim Arr3(1, 1)
cnt = 0
For i = 1 To RowNum
For j = 1 To ColNum
If rngRow(i, 1) = "" Or rngCol(1, j) = "" Then
'Other formula types
Arr1(i, j) = rngData(i, j).Formula
Else
If CognosLink = True Then
ColRef = Col_Letter(fCol + j - 1)
RowRef = fRow + i - 1
'fGetValue function
Arr1(i, j) = "=IFERROR(@cc.fGetVal(" & ColRef & "$9,,," & ColRef & _
"$8," & ColRef & "$6," & ColRef & "$3," & ColRef & "$6," & ColRef & _
"$2,$A" & RowRef & ",,,,,," & ColRef & "$4,," & ColRef & "$5),)"
'fAccName function
Arr2(i, 1) = "=IFERROR(@cc.fAccName(A" & RowRef & "),)"
'fCompName function
Arr3(1, 1) = "=IFERROR(@cc.fCompName(A6),)"
Else
ColRef = Col_Letter(fCol + j - 1)
RowRef = fRow + i - 1
'fGetValue --> PasteValue
Arr1(i, j) = rngData(i, j).Value
'fAccName --> PasteValue
Arr2(i, 1) = rngRow(1, 2).Value
'fCompName --> PasteValue
Arr3(1, 1) = rngPoV(1, 2).Value
End If
End If
Next
Next
'Copy data to target
With wsFin
.Range(.Cells(fRow, fCol), .Cells(n, m)) = Arr1
.Range(.Cells(fRow, 2), .Cells(n, 2)) = Arr2
.Cells(6, 2) = Arr3
End With
Exit Sub '!***!
ErrorTrap: '!***!
MsgBox Err.Description '!***!
End Sub