Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 2
Default 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

  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 10,593
Default VBA setting formula for a cell causes "Wrong data type" error

If you are passing the cell name as say H10, then perhaps you need

Range("A1").Formula = strFormula

--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)

"undercups" wrote in message
ups.com...

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



  #3   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 2
Default VBA setting formula for a cell causes "Wrong data type" error

Bob

I have already tried that. No joy.

  #4   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 35,218
Default VBA setting formula for a cell causes "Wrong data type" error

This formula:
=CELL("filename")
returns the name of the worksheet that was active when excel calculated--not the
name of the path/workbook/worksheet holding that formula.

I'd use
=cell("filename",a1)
so that I'd get that info about the cell with the formula.

But VBA offers another way to get the path directly:

msgbox activeworkbook.path

And if you remember your old DOS days, you could go one level up a path by
doing:
cd ..
(two dots meant up a level)

Maybe this will give you an idea for an alternative:

Option Explicit
Sub testme()

Dim myPath As String
Dim strFormula As String
Dim myVar As Variant

myPath = ActiveWorkbook.Path & "\..\"

strFormula = "='" & myPath & "[book1.xls]sheet1'!$a$1"

With ActiveSheet.Range("c3")
.Formula = strFormula
Application.Calculate
myVar = .Value
.ClearContents
End With

MsgBox myVar
End Sub

I hardcoded the name of the workbook/worksheet/range address--but that was just
to show that it could work.

Ps. My formula would look like:

='C:\My Documents\excel\[book1.xls]Sheet1'!$A$1

If you're not passing a global range name, you may want to include the sheet
name.

pps. John Walkenbach has a routine that can get values from a closed workbook:
http://j-walk.com/ss/excel/eee/eee009.txt
Look for either: GetDataFromClosedFile or GetValue.

undercups wrote:

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


--

Dave Peterson
  #5   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 6,953
Default VBA setting formula for a cell causes "Wrong data type" error

Range("A1").Formula = strFormula
Calculate

would be illegal in a User defined function used in a worksheet.

A formula can only return a value to the cell in which it is used. It can't
change other cells or event he cell in which it is contained. Commands like
that will cause a #Value.

I didn't see where you are using the arguments in your function.

--
Regards,
Tom Ogilvy




"undercups" wrote:

Bob

I have already tried that. No joy.


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Custom functions calculating time arguments Help Desperate Bill_De Excel Worksheet Functions 12 April 25th 06 02:22 AM
Excel Macro to Copy & Paste [email protected] Excel Worksheet Functions 0 December 1st 05 01:56 PM
Formula Problem - interrupted by #VALUE! in other cells!? Ted Excel Worksheet Functions 17 November 25th 05 05:18 PM
inserting data from a row to a cell, when the row number is specified by a formula in a cell [email protected] New Users to Excel 2 January 6th 05 07:18 AM
GET.CELL Biff Excel Worksheet Functions 2 November 24th 04 07:16 PM


All times are GMT +1. The time now is 01:17 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"