Calculating Hash Total 11 digits long

parry

MrExcel MVP
Joined
Aug 20, 2002
Messages
3,355
Hi, I have a workbook that creates an AFI file. A trailer record is required that needs a hash total being the first 11 digits of a number.

The numbers start as a string but I need to total the strings as a number so I have declared a variable sngHash to do this. Once I have the hash total it needs to be shortened to 11 digits by removing the highest digit of the number.

Trouble is, the Single data type is using scientific notation so when I want to place the number in the trailer it has 1.696781E+11.

OK, different tack, so I next declared a variable cstrHash to convert the number to a string but the number turns into 169781000000 ! which is definitely not correct (should be 149678133877 by my hand written arithmetic)

1) Why is single doing this instead of putting the actual number and
2) How can I add up all the strings to get a total

Any help would be gratefully received :)

Code (edited to remove unnecssary code)...
Code:
Dim sngHash As Single
Dim strHash As String
Dim Card As String
Dim Finish As Integer

n = 2
Finish = Selection.Rows.Count
Count = 0
For n = n To Finish
    Card = Range("b" & n).Value & Space(19 - Len(Range("b" & n).Value))

    Count = Count + 1
    sngHash = Val(Left(Card, 11)) + sngHash

Next n

strHash = CStr(Format(sngHash, "000000000000"))
If Len(strHash) > 11 Then
strHash = Right(strgHash, 11)
End If
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Hi all, cracked it. If you use the Currency data type it can handle the 12 digits so I have it working now.

However I would still like to know why Single was putting the number in scientific notation and how you can prevent it from doing so.
 
Upvote 0

Forum statistics

Threads
1,213,538
Messages
6,114,217
Members
448,554
Latest member
Gleisner2

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