Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Excel VBA

Posted on 2015-01-01
9
Medium Priority
?
126 Views
Last Modified: 2015-01-04
How to write a Excel VBA code to break a field "18DFB0.23" such that

fld1 = 18
fld2 = DFB
fld3 = 0.23
0
Comment
Question by:AXISHK
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 48

Expert Comment

by:Wayne Taylor (webtubbs)
ID: 40527196
Will each field always be the same length? If so, you can use something like this....

    Dim s As String
    s = "18DFB0.23"
    
    Dim fld1 As Integer, fld2 As String, fld3 As Double
    
    fld1 = CInt(Left(s, 2))
    fld2 = Mid(s, 3, 3)
    fld3 = Right(s, 4)

Open in new window

0
 
LVL 11

Expert Comment

by:Wilder1626
ID: 40527197
Try this if the field will always be the same length.

=LEFT(A1,2)
=MID(A1,3,3)
=RIGHT(A1,4)
0
 

Author Comment

by:AXISHK
ID: 40527216
Tks,  but the size of the each portion are difference, only the middle is a char field. Tks
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 27

Accepted Solution

by:
MacroShadow earned 2000 total points
ID: 40527257
Put this code in a regular module (found it on Tek-Tips):
Public Function getPart(theString As String, intPartNumber As Integer) As String
    Dim aVarArray() As Variant
    aVarArray = SplitAlphaNumeric(theString)
    If Not intPartNumber > UBound(aVarArray()) + 1 Then
        getPart = aVarArray(intPartNumber - 1)
    End If
End Function
Public Function SplitAlphaNumeric(theString As String) As Variant
    Dim aVarArray() As Variant
    Dim intSplits As Integer
    Dim intIndex As Integer
    Dim intSwitchStart As Integer
    Dim intSwitchPos As Integer
    ReDim aVarArray(countParts(theString) - 1)
    intSwitchStart = 1
    Do
        intSplits = intSplits + 1
        intSwitchStart = intSwitchStart + intSwitchPos
        intSwitchPos = getPartLength(theString, intSwitchStart)
        aVarArray(intIndex) = Mid(theString, intSwitchStart, intSwitchPos)
        intIndex = intIndex + 1
    Loop Until intSwitchStart + intSwitchPos > Len(theString)
    SplitAlphaNumeric = aVarArray
End Function

Public Function countParts(theString As String) As Integer
    Dim isChrNumeric As Boolean
    Dim intcounter As Integer
    countParts = 1
    isChrNumeric = IsNumeric(Left(theString, 1))
    For intcounter = 2 To Len(theString)
        If Not (IsNumeric(Mid(theString, intcounter, 1)) = isChrNumeric) Then
            countParts = countParts + 1
            isChrNumeric = Not (isChrNumeric)
        End If
    Next intcounter
End Function

Public Function getPartLength(theString As String, intStart As Integer) As Integer
'Find the length of the alphabetic or numeric part
    Dim intCount As Integer
    intCount = 1
    If Not (IsNumeric(Mid(theString, intStart, 1)) = IsNumeric(Mid(theString, intStart + 1, 1))) Then
        intCount = 1
    Else
        Do
            intCount = intCount + 1
        Loop While IsNumeric(Mid(theString, intStart, 1)) = IsNumeric(Mid(theString, intStart + intCount, 1)) And Not ((intStart + intCount) > Len(theString))
    End If
    getPartLength = intCount
End Function

Open in new window

Then use it as follows:
fld1 = getPart("18DFB0.23",1)
fld2 = getPart("18DFB0.23",2)
fld3 = getPart("18DFB0.23",3)

Open in new window

0
 

Author Comment

by:AXISHK
ID: 40527265
fld3 = getPart("18DFB0.23",3) --> return 0 rather than 0.23... ?
0
 

Author Closing Comment

by:AXISHK
ID: 40527295
Modification the code slightly and it works perfect, Tks
0
 
LVL 48

Expert Comment

by:Wayne Taylor (webtubbs)
ID: 40530816
While this question has already been answered, another option is to use Regular Expressions. While somewhat difficult to understand, they can make your job much easier.

Use this function....


Function GetParts(inputString As String) As Variant

    Dim regex, matches
    Set regex = CreateObject("VBScript.RegExp")
    regex.Pattern = "(\d+)([A-Z]+)(\d+.\d+)"
    If regex.Test(inputString) Then
        Set matches = regex.Execute(inputString)
        Dim arr(0 To 2) As Variant
        arr(0) = matches.Item(0).SubMatches(0)
        arr(1) = matches.Item(0).SubMatches(1)
        arr(2) = matches.Item(0).SubMatches(2)
        GetParts = arr
    End If
    
End Function

Open in new window


....like this....

    Dim fld1 As Integer, fld2 As String, fld3 As Double
    Dim arr() As Variant
    arr = GetParts("18DFB0.23")
    fld1 = arr(0)
    fld2 = arr(1)
    fld3 = arr(2)

Open in new window

0
 

Author Comment

by:AXISHK
ID: 40530828
What does regex.Pattern = "(\d+)([A-Z]+)(\d+.\d+)" in the expression mean ?

Tks
0
 
LVL 48

Expert Comment

by:Wayne Taylor (webtubbs)
ID: 40530832
It's matching a pattern of 3 groups....

(\d+) = match a numeral character that occurs one or more times
([A-Z]+) = Match any character from A to Z that occurs 1 or more times
(\d+.\d+) = Match a one or more numerals, followed by a decimal place, followed by one or more numerals
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question