If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > PC based Database Applications > Microsoft Excel > Back to basics anyone?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-06-08, 12:41
JAustin JAustin is offline
Registered User
 
Join Date: Aug 2008
Posts: 2
Back to basics anyone?

I'm working on programming my first VB userform / function. I openly admit, I barely know what I'm doing. But I'm working on a project and thought perhaps I could resolve an issue by writing an excel function in visual basic.

Can anyone explain how to write the code for a program that would help me seperate a column of alpha numeric data? (i.e. 123AP value is in column A. The program would generate value "123" into Column B and "AP" into Column C.

Any information is Greatly Appreciated!

Thanks,

Jesse
Reply With Quote
  #2 (permalink)  
Old 08-20-08, 15:51
weejas weejas is offline
Registered User
 
Join Date: Sep 2006
Location: Surrey, UK
Posts: 448
If you're certain that the string will always be numeric on one side and text on the other, it's fairly simple to write a custom function to do this.

Code:
Function SplitString(strStart As String, booSide As Boolean) As Variant
'Function to split a string composed of numeric and non-numeric characters
'booSide determines if the return value will be the numeric side (left, true) or the
'non-numeric side (right, false).

Dim strWorking as String   'Working variable
Dim strTemp as String    'Holder for return value

strWorking = strStart

If booSide Then

   'Parse strWorking from the left until you run out of numeric characters
   Do While IsNumeric(Left(strWorking, 1))

      'Add the character to the return variable
      strTemp = strTemp & Left(strWorking, 1)

      'Lop off that character from strWorking
      strWorking = Right(strWorking, Len(strWorking)-1)

   Loop

Else

   'Parse strWorking from the right until you reach numeric characters
   Do Until IsNumeric(Right(strWorking, 1))

      'Add the character to the return variable
      strTemp = Right(strWorking, 1) & strTemp

      'Lop that character from strWorking
      strWorking = Left(strWorking, Len(strWorking)-1)

   Loop

End If

'Populate the function with the return value

SplitString = IIf(booSide, CLng(strTemp), strTemp)

End Function
NB
I haven't tested this, but it looks about right. To use it, enter your value in A1, "=SplitString(A1, True)" into B1 and "=SplitString(A1, False)" into C1.

Good luck!
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On