Consulting

Results 1 to 7 of 7

Thread: Solved: Separating between each two digits

  1. #1

    Solved: Separating between each two digits

    Hi guys
    I have a range of numbers which differs in the number of digits
    I want to separate between each two digits with a space..
    Note that the number of digits is unknown and changeable
    for example the number 15426854645 should be like that
    15 42 68 56 65 5
    Last edited by YasserKhalil; 07-31-2010 at 05:18 AM.

  2. #2
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    [VBA]=TRIM(TEXT(A2,CHOOSE(MOD(LEN(A2),2)+1,"## ## ## ## ## ## ## ## ## ## ##","## ## ## ## ## ## ## ## ## ## ## #")))[/VBA]
    It's a formula, not vba, you may need to increase the number of # characters if your numbers are longer.
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  3. #3
    Thank you Mr. p45cal
    Your great formula works just with numbers..
    What if the number was a mixture of text and numbers?

  4. #4
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    Quote Originally Posted by YasserKhalil
    Thank you Mr. p45cal
    Your great formula works just with numbers..
    What if the number was a mixture of text and numbers?
    Ah, the old moving-the-goalposts gambit..

    some examples of before and after perhaps?
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  5. #5
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Quote Originally Posted by p45cal
    Ah, the old moving-the-goalposts gambit..

    some examples of before and after perhaps?
    A quiet rainy day off started with laughter; nice!

    Well, here was my shot, modified as guessing...

    As a UDF, array UDF or called in vba...
    [vba]
    Option Explicit

    Sub exa()
    Dim rng As Range

    Set rng = Range("A2:A20")
    rng.Value = SplitNumString(rng)
    End Sub

    Function SplitNumString(CellRange As Range) As Variant
    Static REX As Object
    Dim vntCellVal As Variant, _
    aryVals As Variant, _
    x As Long, _
    y As Long

    If REX Is Nothing Then
    Set REX = CreateObject("VBScript.RegExp")
    REX.Global = True
    REX.Pattern = "([0-9]{2}|[a-zA-Z]{2}|[0-9][a-zA-Z]|[a-zA-Z][0-9])(?=.)"
    End If

    aryVals = CellRange.Value

    If IsArray(aryVals) Then
    For x = 1 To UBound(aryVals)
    For y = 1 To UBound(aryVals, 2)
    aryVals(x, y) = REX.Replace(aryVals(x, y), "$1 ")
    Next
    Next
    SplitNumString = aryVals
    Else
    SplitNumString = REX.Replace(aryVals, "$1 ")
    End If
    End Function[/vba]

    Hope that helps,

    Mark

  6. #6
    VBAX Expert Tinbendr's Avatar
    Joined
    Jun 2005
    Location
    North Central Mississippi (The Pines)
    Posts
    993
    Location
    [vba]Sub AddASpace()

    Dim aCell As Range

    For Each aCell In Range("A1:A19")
    aCell = WorkHorse(aCell.Text)
    Next

    End Sub
    Function WorkHorse(aCell As String) As String

    Dim TempCell As String
    Dim A As Long

    For A = 1 To Len(aCell) Step 2
    TempCell = TempCell & Mid(aCell, A, 2) & " "
    Next

    WorkHorse = Trim(TempCell)

    End Function[/vba]

    David


  7. #7
    Mr. Mark
    Mr. Tinbendr
    Your solutions are perfect ... They are both wonderful
    Thank you very much for your help

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •