EXCEL VBA: ConvertFormula returns #VALUE - vba

I have several subroutines that use the ConvertFormula VBA command. Unfortunately I ran into a situation where none of the subroutines work. It appears it is the single line containing the ConvertFormula command that is returning the #VALUE error so I can't error check any deeper than that.
Using the Immediate window I was able to test this isolated command. Here are 3 separately executed lines of code.
ActiveSheet.range("Z270")=application.ConvertFormula(formula:="=Sheet2!V23+Sheet2!W23*Z$224+Sheet2!X23*Z$224^2+Sheet2!Y23*Z$224^3+Sheet2!Z23*Z$224^4",fromreferencestyle:=xlA1, toreferencestyle:=xla1, toabsolute:=xlabsolute)
ActiveSheet.range("Z270")=application.ConvertFormula(formula:="=Sheet2!V23+Sheet2!W23*Z$224+Sheet2!X23*Z$224^2+Sheet2!Y23*Z$224^3",fromreferencestyle:=xlA1, toreferencestyle:=xla1, toabsolute:=xlabsolute)
ActiveSheet.range("Z270")=application.ConvertFormula(formula:="=Sheeet2!V23+Sheeet2!W23*Z$224+Sheeet2!X23*Z$224^2+Sheeet2!Y23*Z$224^3",fromreferencestyle:=xlA1, toreferencestyle:=xla1, toabsolute:=xlabsolute)
The first line returned #VALUE, the 2nd returned the formula correctly, and the 3rd returned #VALUE  again. Notice that in the 3rd I'd renamed the offsheet reference to Sheeet2, with an extra 'e' to add length to the formula.
I suspect that the behavior is related only to the formula string length and not the contents. The formula string lengths in my example are 85, 66, and 70 characters for the 1st, 2nd and 3rd lines respectively.
I'm not sure where the exact cut-off is, or whether the $ is excluded from the character count limit. Just wanted to put this out there to see if anyone can confirm my hypothesis, provide further insight, or provide an alternative explanation.
In response to the RFI about reference cell values, all referenced cells contained numeric values. 'Z$224' value was 65, formatted as 65.0. Sheet2 cells V23 thru Z23 had the following values (formatted in scientific notation):
1.0037243; -0.000062039288; 0.000000012983893; -0.00000000042225392

Ran into the same with varying character limits. Have not found a fix but there are workarounds.
Shorten the formulas using Custom user functions
Write your own conversion function. Which you can use Regular Expression to insert dollar signs to references and simple Replace() to remove them.

Related

Excel file contains invalid hidden characters that can't be removed

I have a peculiar problem with hidden characters in an Excel spreadsheet which uses VBA to create a text file. I've attached a link to a test version of the file, and I'll explain as best I can the issue.
The file creates a plain txt file that can be used to feed data into a System we use. It works well normally, however we've been supplied approximately 15,000 rows of data, and at random points throughout there are hidden characters.
In the test file, there's 1 row and it's cell B11 that has hidden characters at the beginning and end of the value. If you put your cursor at the end of it, and press the backspace key, it will look as if nothing has happened, but actually you've just deleted one of the characters.
As far as Excel is concerned, those hidden characters are question marks, but they're not, as text stream would parse those, but it doesn't, and instead throws up an invalid procedure call error.
I've tried using Excel's CLEAN formula, I've tried the VBA equivalent, tried using 'Replace', but nothing seems to recognise those characters. Excel is convinced they're just question marks, even an ASCII character call gives me the same answer (63), but replace doesn't replace them as question marks, it just omits them!
Any help on this, even if it's just a formula I could apply would be appreciated. In the interests of data protection the data in the file is fake by the way, it's nobody's real NI number.
The excel file with vba code is here
This VBA macro could be run on its own or in conjunction with the ClearFormatting macro. It did strip out the rogue unichars from the sample.
Sub strip_Rogue_Unichars()
Dim uc As Long
With Cells(11, 1).CurrentRegion
For uc = 8000 To 8390
.Replace what:=ChrW(uc), replacement:=vbNullString, lookat:=xlPart
DoEvents
Next uc
End With
End Sub
There's probably a better way to do this and being able to restrict the scope of the Unicode characters to search and replace would obviously speed things up. Turning off .EnableEvents, .ScreenUpdating, etc would likewise help. I believe the calculation was already at manual. I intentionally left a DoEvents in the loop as my first run was several thousand different unichars.

#Value! error with =(A1+A2)

I'm trying to add multiple cells with the formula =(A1+A2+A3...etc)
Which works, but if all the cells are empty then I get a #Value!
PLEASE NOTE:
Yes I am aware the proper way to add cell values is with =SUM(A1:A3)
However the cells I'm adding together each have their own functions to get their numbers, and the =SUM function won't add them together.
So! Is there a way I can make =(A1+A2+A3...etc) not give me a #Value! error in the cell that's supposed to total them if ALL the cells (A1,A2,etc) are empty? (as in, the cell with the total will just be blank)
Yes I know this is overly complicated. I'm working with that I've got.
EDIT
I might have figured out my problem. My 'false' statement in the function of the cells that were being added is "" in order to make the cell not have a 0 in it when empty. When it tries to add those cells together, if they all read "" and none are a number that's when I get the #Value! error. Not sure yet what I'm going to do about that...
EDIT 2
Yup. Problem was caused by having a non-numerical value as my false statement. Didn't want a bunch of zeros everywhere, but oh well I guess.
I tried both Excel 2007 and Calc 3.4.1, and neither one of these generated the #Value! that you mention. I am thinking that perhaps your source cells' equations are producing a value that is causing this to error out.
For example, if one of the cells has a String value, then this will be the result. This can be detected with the TYPE() function. for example:
=( IF(TYPE(A1)=1;A1;0) + IF(TYPE(A1)=1;B1;0) + ...)
this will make sure that you are actually adding numbers before the addition takes place.
edit
See: http://www.techonthenet.com/excel/formulas/type.php
for details on TYPE()

VBA texttocolumns ignores one column

I have data that was previously improted into Excel and want to have that data automatically put into the proper format.
Right now my dates look like this: 28122012 which should be 28.12.2012
My code, which I've put together with some sources I've found online, works well except for one snag... it ignores column V:
Dim rngS As Range
For Each rngS In .Range("F:F,U:W").Columns
rngS.TextToColumns Destination:=rngS.Cells(1, 1), _
DataType:=xlFixedWidth, FieldInfo:=Array(0, 4)
Next
I'm not certain why it does this. I've experimented by writing out each column, but no go. Column V remains as it is, while everything around it gets properly formatted.
Any ideas why this could be?
Thanks!
The problem is the field it pastes to is treating the value as numerical.
The only real remedy is to set the type to Text for that and other columns that might have such a date value. Then 01122012 will not lose its starting zero.
Alternatively you could let your follow up code or formula anticipate the missing zeroes and work back to date values using LEFT(), RIGHT() and like functions.

Is there a way in Excel to reference a cell range minus some cell? (As a function parameter)

Assume I have an Excel 2007 cell range of B3:B50. Is there any way to specify instead B3:B17+B19:B50, or maybe B3:B50-B18?
Several sources say that B3:B17,B19:B50 should work, but it doesn't work as a function parameter (for something like CORREL), since the comma is interpreted as separating parameters.
I tried with a named range (which can have multiple points in the definition) and for CORREL got #Value! returned, but SUM worked as expected.
you may have to test to see what functions will work

Write a formula in an Excel Cell using VBA

I'm trying to use VBA to write a formula into a cell in Excel.
My problem is that when I use a semicolon (;) in my formula, I get an error:
Run-time error 1004
My macro is the following :
Sub Jours_ouvres()
Dim Feuille_Document As String
Feuille_Document = "DOCUMENT"
Application.Worksheets(Feuille_Document).Range("F2").Formula = "=SUM(D2;E2)"
End Sub
You can try using FormulaLocal property instead of Formula. Then the semicolon should work.
The correct character to use in this case is a full colon (:), not a semicolon (;).
The correct character (comma or colon) depends on the purpose.
Comma (,) will sum only the two cells in question.
Colon (:) will sum all the cells within the range with corners defined by those two cells.
Treb, Matthieu's problem was caused by using Excel in a non-English language. In many language versions ";" is the correct separator. Even functions are translated (SUM can be SOMMA, SUMME or whatever depending on what language you work in). Excel will generally understand these differences and if a French-created workbook is opened by a Brazilian they will normally not have any problem.
But VBA speaks only US English so for those of us working in one (or more) foreign langauges, this can be a headache.
You and CharlesB both gave answers that would have been OK for a US user but Mikko understod the REAL problem and gave the correct answer (which was also the correct one for me too - I'm a Brit working in Italy for a German-speaking company).
I don't know why, but if you use
(...)Formula = "=SUM(D2,E2)"
(',' instead of ';'), it works.
If you step through your sub in the VB script editor (F8), you can add Range("F2").Formula to the watch window and see what the formular looks like from a VB point of view. It seems that the formular shown in Excel itself is sometimes different from the formular that VB sees...