VBA setting formula for a cell causes "Wrong data type" error
Below is a function I am trying to debug and the problem I have is with
the line "Range("A1").FormulaR1C1 = strFormula".
When I run the formula the cell displays the error "#Value". I look
into the error checking log and thats where I see the message "A value
used in the formula is of the wrong data type".
Stepping through the code shows that the Range statement to be causing
the problem
All variables are of string, the strFormula contains the expected
value, the cell is of type "general". I have tried variations such as
Range("A1").select/ActiveCell.formula, formula / formula R1C1. So why
am I getting the error?
Here is the source code :-
Function CreateLink(strTargetName As String, strCellName As String) As
String
Dim strFileName As String
Dim strPathName As String
Dim strFormula As String
Dim strReturnValue As String
Dim intCount As Integer
Application.Volatile
'Get the file name
strFileName = Range("B32").Value ' the cell contains the formula
"=CELL("filename")
' Find the current path
strPathName = Left$(strFileName, InStr(strFileName, "\[") - 1)
' Remove the current directory as linked spreadsheet is 1 level up
intCount = Len(strPathName)
Do Until Mid$(strPathName, intCount, 1) = "\"
intCount = intCount - 1
Loop
'Build up the link
strFormula = "='" & Left(strPathName, intCount) & "[" & strTargetName &
"]'!" & strCellName
Range("A1").Formula = strFormula
Calculate
strReturnValue = Range("A1").Value
CreateLink = strReturnValue
End Function
Thanks in anticipation
Duncan
|