Multiple Apps

Functions for the Sum, Minimum, and Maximum of ANSI Characters Values in a String

Ease of Use

Easy

Version tested with

2000, 2002 

Submitted by:

matthewspatrick

Description:

These function evaluate the ANSI values of each character in a string, and return the sum (AscSum), minimum (AscMin), or maximum (AscMax) of these values. A zero-length string returns -1 (to avoid confusion with ANSI character 0) 

Discussion:

I once had to help someone with a game where the puzzles involved turning numbers into letters, and evaluating words and sentences for the values of their characters. These functions came in handy. In another instance, a client wanted passwords for various users of a certain application to be determined by summing the ANSI values of the letters in their geographic codes, plus a random number. The functions will work in any VB-supported application. 

Code:

instructions for use

			

Option Explicit Public Function AscSum(CheckString As String) As Integer ' Calculates the sum of the ANSI codes for each character in a text string ' (-1 for zero-length strings)" Dim Counter As Integer If CheckString = "" Then AscSum = -1 Exit Function End If AscSum=0 For Counter = 1 To Len(CheckString) AscSum = AscSum + Asc(Mid(CheckString, Counter, 1)) Next End Function Public Function AscMin(CheckString As String) As Integer ' Evaluates each character in a text string and returns the lowest ' ANSI character code found. Empty strings evaluate to -1 (because ' there is an ANSI character 0) Dim Counter As Integer If CheckString = "" Then AscMin = -1 Exit Function End If For Counter = 1 To Len(CheckString) If Counter = 1 Then AscMin = Asc(Left(CheckString, 1)) ElseIf Asc(Mid(CheckString, Counter, 1)) < AscMin Then AscMin = Asc(Mid(CheckString, Counter, 1)) End If Next End Function Public Function AscMax(CheckString As String) As Integer ' Evaluates each character in a text string and returns the highest ' ANSI character code found. Empty strings evaluate to -1 (because ' there is an ANSI character 0) Dim Counter As Integer If CheckString = "" Then AscMax = -1 Exit Function End If For Counter = 1 To Len(CheckString) If Counter = 1 Then AscMax = Asc(Left(CheckString, 1)) ElseIf Asc(Mid(CheckString, Counter, 1)) > AscMax Then AscMax = Asc(Mid(CheckString, Counter, 1)) End If Next End Function

How to use:

  1. Paste the code above into your project
  2. Use the functions AscSum(), AscMin(), and AscMax() in your code, or in your application's UI as appropriate (e.g., in Excel worksheet formulas--please see attached file for examples, or in Access queries)
 

Test the code:

  1. Test various strings in the functions.
  2. Please see the attached sample file for a few examples of how to use this function.
 

Sample File:

AscSumMinMaxExamples.zip 9.22KB 

Approved by mdmackillop


This entry has been viewed 69 times.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2020 VBA Express