PDA

View Full Version : Solved: Length of negative number



mdmackillop
09-27-2006, 01:22 PM
In debugging my posted code here, http://vbaexpress.com/forum/showthread.php?t=9663 I've a problem with these lines

Num = CInt(Mid(F, Pos1 + 2, Pos2 - Pos1 - 2))
Mid(F, Pos1 + 2, Len(Num)) = Num + Rw

When Num has a value of -2, Len(Num) = 4, which is throwing out the text replacement.

Ken Puls
09-27-2006, 03:14 PM
Hey Malcolm,

Can you provide a sample of a formula that would cause this issue? I tried it with a simple one, and it seems nice...

mdmackillop
09-27-2006, 04:03 PM
Hi Ken
I've resolved the problem, but here's the original code.

Ken Puls
09-27-2006, 04:06 PM
Are you submitting this one to the KB, Malcolm? Seems a nice entry to me. :)

mdmackillop
09-27-2006, 04:12 PM
I'll give it a day or two for some ********* to come up with a method solution!
Seriously though, I'll probably do that. I should be able to build in options for absolute row, colum or both.

Ken Puls
09-27-2006, 04:14 PM
LOL!

Bob Phillips
09-27-2006, 04:33 PM
Sub xxConvertAbsolute()
Dim cell As Range

For Each cell In Selection
If cell.HasFormula Then
cell.Formula = Application.ConvertFormula( _
cell.Formula, xlA1, xlA1, xlAbsolute)
End If

Next cell

End Sub

mdmackillop
09-27-2006, 04:42 PM
The ********* has posted! "ConvertFormula". I should have guessed!

Ken Puls
09-27-2006, 04:44 PM
:rotflmao:

Bob Phillips
09-27-2006, 04:53 PM
I told you, those Scots love us men of Wessex.

For Arthur and country!

mdmackillop
09-27-2006, 06:16 PM
I'll steal the solution and KB it anyway!

Bob Phillips
09-28-2006, 04:24 AM
You could wrap my name around it, I don't get many KBs :-(

mdmackillop
09-28-2006, 07:12 AM
but my original question remains. Why does -2 have a length of 4?

mvidas
09-28-2006, 08:09 AM
Malcolm,

I'm guessing 'Num' is declared as type Long. Len() returns different things, depending on what you pass it.

If you pass it a string, it returns the length of that string.
If you pass it a variant, it converts to a string first then returns the length of that string.
If you pass it anything else, it returns how much memory is used by the variable. (LenB)
See it in action:Sub WatchLen()
Dim lNum As Long, vNum, stNum As String, iNum As Integer, siNum As Single, dNum As Double
lNum = -2
vNum = -2
stNum = -2
iNum = -2
siNum = -2
dNum = -2
Debug.Print "Long: " & Len(lNum)
Debug.Print "Variant: " & Len(vNum)
Debug.Print "String: " & Len(stNum)
Debug.Print "Integer: " & Len(iNum)
Debug.Print "Single: " & Len(siNum)
Debug.Print "Double: " & Len(dNum)
End Sub

Long: 4
Variant: 2
String: 2
Integer: 2
Single: 4
Double: 8Matt

mdmackillop
09-28-2006, 08:43 AM
Thanks Matt,
That makes sense.