VBA: The returned length (len) of variable is not proper

syzer

New Member
Joined
May 11, 2024
Messages
9
Office Version
  1. 2021
Platform
  1. Windows
Hello guys,

I would like to determine the length of the cell to create special "ID" based on the length of number value. Example: Number 1 : Length = 1 / Number 10 : Length = 2 / Number 122 : Length = 3

Unfortunately, when I trying to determine with LEN function the count of characters of the variable (in my case characters of the numbers), then excel determine wrong number. (Number 9: Length = 2)

1716502433900.png


Could somebody support me, what can be the root-cause and how I can repair it?

Dim datasheet As Worksheet 'where is the data copied from
Dim reportsheet As Worksheet 'where is the data pasted to
Dim LastRow As Integer 'the last row of the data set
Dim maxValue As Integer 'Create variable to store max value

Set datasheet = Sheet7

datasheet.Select
LastRow = Cells(Rows.Count, 1).End(xlUp).Row

'Calculate max value in range
maxValue = Application.WorksheetFunction.Max(Range("A1:A" & LastRow))

maxValue = maxValue + 1
LastRow = LastRow + 1

ActiveSheet.Range("A" & LastRow).Select
ActiveSheet.Range("A" & LastRow).value = maxValue

MsgBox ("MaxValue:" & maxValue & vbCrLf & "Len of MaxValue:" & Len(maxValue))

Thank you in advance for any idea!
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
The VBA Len() function returns the number of characters in a string or the number of bytes required to store a variable.

Hence, for example:

VBA Code:
Sub Demo()

    Dim x As Integer
    Dim y As Long
    x = 123
    y = 123
    
    MsgBox Len(x)   'returns 2
    MsgBox Len(y)   'returns 4

End Sub
 
Upvote 1
Here is my solution to your problem. This usually generates some discussion from the A+ students.

VBA Code:
Sub Prog1()

Dim datasheet As Worksheet 'where is the data copied from
Dim reportsheet As Worksheet 'where is the data pasted to
Dim LastRow As Integer 'the last row of the data set
Dim maxValue As Integer 'Create variable to store max value

Set datasheet = Sheets("Sheet7")

datasheet.Select
LastRow = Cells(Rows.Count, 1).End(xlUp).Row

'Calculate max value in range
maxValue = Application.WorksheetFunction.Max(Range("A1:A" & LastRow))

maxValue = maxValue + 1
LastRow = LastRow + 1

ActiveSheet.Range("A" & LastRow).Select
ActiveSheet.Range("A" & LastRow).Value = maxValue

MsgBox ("MaxValue:" & maxValue & vbCrLf & "Len of MaxValue:" & Len(CStr(maxValue)))

End Sub
 
Upvote 1
Solution
Another way ....

VBA Code:
With Sheet7
    MsgBox Evaluate("Max(Len('" & .Name & "'!A1:A" & .Range("A" & Rows.Count).End(xlUp).Row & "))")
End With
 
Upvote 1
Thank you guys for everyone, the second solution solved my issue! Thanks
 
Upvote 0

Forum statistics

Threads
1,217,382
Messages
6,136,234
Members
450,000
Latest member
jgp19

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