+ Reply to Thread
Results 1 to 7 of 7

Separating Binary Numbers via "Text to Columns" Type Approach

Hybrid View

JP777 Separating Binary Numbers via... 04-21-2012, 10:04 PM
StevenM Re: Separating Binary Numbers... 04-21-2012, 10:32 PM
Trebor76 Re: Separating Binary Numbers... 04-21-2012, 10:44 PM
jindon Re: Separating Binary Numbers... 04-21-2012, 10:47 PM
JP777 Re: Separating Binary Numbers... 04-21-2012, 11:53 PM
Trebor76 Re: Separating Binary Numbers... 04-22-2012, 04:40 AM
lecxe Re: Separating Binary Numbers... 04-22-2012, 09:19 AM
  1. #1
    Registered User
    Join Date
    04-08-2012
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    76

    Separating Binary Numbers via "Text to Columns" Type Approach

    I have extremely long sets of binary 1s and 0s in cells in Column B of my spreadsheet, and I need to separate each digit into its own column, starting with Column B. I have looked at the Text to Columns function and have searched and searched Google, but I can't find anything that will separate after one digit/character. Can anyone provide me with VBA code to do this? Just a code that does what Text to Columns does but with each digit separated. Thanks in advance for your help! Please see the sample file for clarification, if needed.
    Attached Files Attached Files

  2. #2
    Valued Forum Contributor StevenM's Avatar
    Join Date
    03-23-2008
    Location
    New Lenox, IL USA
    MS-Off Ver
    2007
    Posts
    910

    Re: Separating Binary Numbers via "Text to Columns" Type Approach

    Perhaps there is a better way, but this should work.

    Sub SeparateBinary()
        Dim nRow As Long, nCol As Long, str As String
        'Find the last row in column A
        For nRow = 1 To Cells(Rows.Count, "A").End(xlUp).Row
            'Copy cell to string
            str = Cells(nRow, "A").Value
            For nCol = 1 To Len(str)
                'Separate Binary Digits into each Column
                Cells(nRow, nCol) = Mid$(str, nCol, 1)
            Next nCol
        Next nRow
    End Sub
    You wrote "column B" in your message, but your workbook showed column A. For column B, you would need to change "A" to "B" and the line "Cells(nRow, nCol) = ..." would need to be "Cells(nRow, nCol + 1) = ..."

  3. #3
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,568

    Re: Separating Binary Numbers via "Text to Columns" Type Approach

    Hi JP777,

    Try this:

    Option Explicit
    Sub Macro1()
    
        'http://www.excelforum.com/excel-programming/826513-separating-binary-numbers-via-text-to-columns-type-approach.html
    
        Dim rngCell As Range
        Dim intCellPos As Integer
        
        Application.ScreenUpdating = False
        
        For Each rngCell In Range("A2:A" & Range("A" & Rows.Count).End(xlUp).Row)
            For intCellPos = 1 To Len(rngCell)
                rngCell.Offset(0, intCellPos) = Mid(rngCell, intCellPos, 1)
            Next intCellPos
        Next rngCell
        
        Application.ScreenUpdating = True
    
    End Sub
    HTH

    Robert
    ____________________________________________
    Please ensure you mark your thread as Solved once it is. Click here to see how
    If this post helps, please don't forget to say thanks by clicking the star icon in the bottom left-hand corner of my post

  4. #4
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,836

    Re: Separating Binary Numbers via "Text to Columns" Type Approach

    TextToColumns
    Sub test()
        Dim i As Long, myArray()
        ReDim myArray(Len(Range("a2").Value) - 1)
        For i = 1 To Len(Range("A2").Value)
            myArray(i - 1) = Array(i - 1, 1)
        Next
        Range("a2", Range("a" & Rows.Count).End(xlUp)).TextToColumns _
            Destination:=Range("b2"), DataType:=xlFixedWidth, _
            FieldInfo:=myArray
    End Sub
    Last edited by jindon; 04-21-2012 at 10:50 PM.

  5. #5
    Registered User
    Join Date
    04-08-2012
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    76

    Re: Separating Binary Numbers via "Text to Columns" Type Approach

    Thanks to all 3 of you. All 3 worked like a charm!

  6. #6
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,568

    Re: Separating Binary Numbers via "Text to Columns" Type Approach

    Thanks for the feedback and we're glad to hear it

  7. #7
    Valued Forum Contributor
    Join Date
    10-15-2007
    Location
    Home
    MS-Off Ver
    Office 2010, W10
    Posts
    373

    Re: Separating Binary Numbers via "Text to Columns" Type Approach

    Hi

    A bit late, but another option


    Sub Test()
    Range("a2").Parse Replace(Replace(Range("a2").Value, "0", "[x]"), "1", "[x]"), Range("B2")
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1