length of 1,000,000 is 8

yky

Well-known Member
Joined
Jun 7, 2011
Messages
1,881
Office Version
  1. 2010
Platform
  1. Windows
I passed a numeric value of 1,000,000 to a sub and checked the length of it. This is what I got in the Immediate window.

VBA Code:
Sub income_RG(num As Double)
Debug.Print num              <-gives 1000000 with a trailing space
num = Trim(num)
Debug.Print num              <-gives 1000000 with a trailing space
Debug.Print Len(num)         <- gives 8
Debug.Print Len(Trim(num))   <- gives 7
Debug.Print Len(num)         <-give 8
......

It seems the effect of TRIM is short-lived. Somehow Excel appends a space after num right away. What's going on here? How do I prevent Excel from adding the trailing space?

Further test shows this:

VBA Code:
Debug.Print num&a         <gives 1000000a

There doesn't seem to be a trailing space (though it does have one in the Immediate window). So, why is the length 8, not 7?
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
For numbers, len() returns the storage size of the variable type.

Dante Amor
GH
1TypeStorage
2Byte1 byte
3Integer2 bytes
4Long4 bytes
5Single4 bytes
6Double8 bytes
Sheet4


Try this
VBA Code:
Sub test2()
  Dim numB As Byte
  Dim numI As Integer
  Dim numL As Long
  Dim numS As Single
  Dim numD As Double
  numB = 100
  numI = 100
  numL = 100
  numS = 100
  numD = 100
  Debug.Print Len(numB) 'gives 1
  Debug.Print Len(numI) 'gives 2
  Debug.Print Len(numL) 'gives 4
  Debug.Print Len(numS) 'gives 4
  Debug.Print Len(numD) 'gives 8
  
  Debug.Print Len(Trim(numD)) 'gives 3 Text length "100"
End Sub

----------------------------------
There doesn't seem to be a trailing space (though it does have one in the Immediate window). So, why is the length 8, not 7?
There are no spaces, it is the storage size of the variable type.
If you convert it to text, for example using the Trim() function, then you can see the number of digits of the number.
 
Upvote 0
Solution

Forum statistics

Threads
1,223,239
Messages
6,170,947
Members
452,368
Latest member
jayp2104

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top