Code Snippets


Testing for Integers

You could use any of the following to test if the number is an Integer.

Dim number As Variant 
If (INT(number) = number) Then
End

If (INT(number)/number = 1) Then
End

If (TypeName(number) = "Integer") Then
End

??

Option Explicit 

Public Sub testing()
   Debug.Print AddTwoNumberStrings("2352362", "74747")
End Sub

Public Function AddTwoNumberStrings( _
   ByVal sString1 As String, _
   ByVal sString2 As String) As String

Dim aDigits1 As Variant
Dim aDigits2 As Variant
Dim lupper As Long
Dim sResult As String

   sString1 = VBA.StrReverse(sString1)
   sString2 = VBA.StrReverse(sString2)

   If (Len(sString2) > Len(sString1)) Then
      sString1 = sString1 & String(Len(sString2) - Len(sString1), "0")
   Else
      sString2 = sString2 & String(Len(sString1) - Len(sString2), "0")
   End If
      
   aDigits1 = SplitTextNumberToArray(sString1)
   aDigits2 = SplitTextNumberToArray(sString2)
   
   AddTwoNumberStrings = AddTwoDigitArrays(aDigits1, aDigits2)
End Function

Public Function AddTwoDigitArrays( _
   ByVal aDigits1 As Variant, _
   ByVal aDigits2 As Variant) As String

Dim sResult As String
Dim aResults As Variant
Dim lupper As Long
Dim ipos As Integer
Dim iaddone As Integer
   
   lupper = UBound(aDigits1)
   ReDim aResults(lupper)
   
   iaddone = 0
   For ipos = 0 To lupper
      sResult = VBA.Val(aDigits1(ipos)) + VBA.Val(aDigits2(ipos)) + iaddone
      If (Len(sResult) > 1) Then
         iaddone = 1
      Else
         iaddone = 0
      End If
      aResults(ipos) = Right(sResult, 1)
   Next ipos
      
   If (iaddone = 1) Then
      ReDim Preserve aResults(lupper + 1)
      aResults(lupper + 1) = 1
   End If
         
   AddTwoDigitArrays = VBA.StrReverse(VBA.Join(aResults, ""))
End Function

Public Function SplitTextNumberToArray( _
   ByVal sNumberText As String) As Variant
   
Dim aDigits As Variant

   aDigits = VBA.Split(VBA.StrConv(sNumberText, 64), Chr(0))
   ReDim Preserve aDigits(Len(sNumberText) - 1)

   SplitTextNumberToArray = aDigits
End Function


© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrev