B
Billy
Hello!
I created user defined function (myFunc) which take two arguments,
make some calculations and return back result to cell from where was
called. Until here everyting work fine.
Problem is here: At the time when I make calculation I have to write
some values to other sheet (e.g. Summary) in workbook and here I got
error 1004. Why I am doing wrong?. I know that has to be some little
trick. If I call the same over sub procedure (TestOk), everything work
normally.
Below is my sample code from the Excel module. That code will be
actullay called from XLA Add-in.
Regards,
Billy
-----------------
Function myFunc(intValue1 As Integer, intValue2 As Integer) As Integer
Dim oshSummary As Worksheet, intTempValue As Integer
On Error GoTo myError
Set oshSummary = ActiveWorkbook.Sheets("Summary")
If intValue1 < 10 Then
intTempValue = intValue1 * 2 + intValue2
'Why I get error??: 1004 - 'Application-defined or object-defined
error
oshSummary.Range("A1").Value = intValue1
oshSummary.Range("A2").Value = intValue2
Else
intTempValue = intValue1 + intValue2
End If
Set oshSummary = Nothing
myFunc = intTempValue
myExit:
Exit Function
myError:
Debug.Print "Err>myFunc: " & Err.Number & " - '" & Err.Description
Resume myExit
End Function
Sub TestOk()
Sheets("Summary").Range("A1").Value = 10
Sheets("Summary").Range("A2").Value = 20
End Sub
-----------------
I created user defined function (myFunc) which take two arguments,
make some calculations and return back result to cell from where was
called. Until here everyting work fine.
Problem is here: At the time when I make calculation I have to write
some values to other sheet (e.g. Summary) in workbook and here I got
error 1004. Why I am doing wrong?. I know that has to be some little
trick. If I call the same over sub procedure (TestOk), everything work
normally.
Below is my sample code from the Excel module. That code will be
actullay called from XLA Add-in.
Regards,
Billy
-----------------
Function myFunc(intValue1 As Integer, intValue2 As Integer) As Integer
Dim oshSummary As Worksheet, intTempValue As Integer
On Error GoTo myError
Set oshSummary = ActiveWorkbook.Sheets("Summary")
If intValue1 < 10 Then
intTempValue = intValue1 * 2 + intValue2
'Why I get error??: 1004 - 'Application-defined or object-defined
error
oshSummary.Range("A1").Value = intValue1
oshSummary.Range("A2").Value = intValue2
Else
intTempValue = intValue1 + intValue2
End If
Set oshSummary = Nothing
myFunc = intTempValue
myExit:
Exit Function
myError:
Debug.Print "Err>myFunc: " & Err.Number & " - '" & Err.Description
Resume myExit
End Function
Sub TestOk()
Sheets("Summary").Range("A1").Value = 10
Sheets("Summary").Range("A2").Value = 20
End Sub
-----------------