+ Reply to Thread
Results 1 to 3 of 3

Problem using VBA code for text to column (the value is repeated after separate cell)

Hybrid View

Jasa P Problem using VBA code for... 03-18-2012, 05:25 AM
Kelshaer Re: Problem using VBA code... 03-19-2012, 09:06 AM
Jasa P Re: Problem using VBA code... 04-07-2012, 09:17 AM
  1. #1
    Registered User
    Join Date
    03-18-2012
    Location
    ID
    MS-Off Ver
    Excel 2007
    Posts
    14

    Question Problem using VBA code for text to column (the value is repeated after separate cell)

    I used text to column by selected Tab character as delimiter. So, if I paste data to this workbook, that will be separated in two column.

    B03=08^t1174.8364.9474.6340.2381.8794x3.10.10/47.46x10/18.64.87.78.894.640x7/49.08.04.40.54x7
    becomes
    in Cell A -->B03=08
    in Cell B --> 1174.8364.9474.6340.2381.8794x3.10.10/47.46x10/18.64.87.78.894.640x7/49.08.04.40.54x7
    Everything was fine after I got this code that can separate cell (like Text to Column function) and do some other functions (change "X" to "x", etc).

    Option Explicit
    Option Compare Text
    
    Private Sub Worksheet_Change(ByVal Target As Range)
    
    Dim rng             As Range
    Dim rngC            As Range 'Single cell for loop
    Dim arrstr()        As String
    Dim strSplit        As String
    Dim findPos         As Long
    
    Dim strFinal        As String
    
    Application.EnableEvents = False
    Application.ScreenUpdating = False
    
    Set rng = Intersect(Columns(1), Target)
    
    If Not rng Is Nothing Then
    
        For Each rngC In Target.Cells
        
            If Not rngC.Value = "" Then
            
                arrstr = Split(rngC.Value, "/")
                
                Dim i As Long
                For i = LBound(arrstr) To UBound(arrstr)
                
                    strSplit = arrstr(i)
                    findPos = InStr(strSplit, "x")
                    
                    If findPos > 0 Then
                        strSplit = Left(strSplit, findPos - 1) & "x" & Replace(Right(strSplit, Len(strSplit) - findPos), "x", ".")
                    End If
                    
                    strFinal = strFinal & strSplit & "/"
                
                Next i
                
                If Not strFinal = "" Then
                    strFinal = Left(strFinal, Len(strFinal) - 1)
                    rngC.Value = strFinal
                End If
                
            End If
            
        Next rngC
        
        If Application.CountA(rng) > 0 Then
            rng.TextToColumns DataType:=xlDelimited, Other:=True, OtherChar:="#"
        End If
        
    End If
    
    Application.ScreenUpdating = True
    Application.EnableEvents = True
    
    End Sub
    But, after pasted the data above, it becomes like this
    in cell A --> B03=08
    in cell B --> B03=081174.8364.9474.6340.2381.8794x3.10.10/47.46x10/18.64.87.78.894.640x7/49.08.04.40.54x7
    This happens if I paste data in A2, but in A1, it works properly

    cats2.jpg
    I'm using Excel 2007
    I hope there's someone can help me
    Any help would be great, thanks a lot

    Regards,



    Jasa

  2. #2
    Valued Forum Contributor
    Join Date
    08-29-2011
    Location
    Mississauga, CANADA
    MS-Off Ver
    Excel 2010
    Posts
    503

    Re: Problem using VBA code for text to column (the value is repeated after separate cell)

    1. Do you want to split on every TAB character?
    2. What are the other functions? for example replacing x with a period and what else?
    3. do you copy from the text file and paste in the excel or how do you import the data?
    Regards,
    Khaled Elshaer
    www.BIMcentre.com

    Remember To Do the Following....
    1. Thank those who have helped you by clicking the Star below their post.
    2. Mark your post SOLVED if it has been answered satisfactorily:
    • Select Thread Tools (on top of your 1st post)
    • Select Mark this thread as Solved

  3. #3
    Registered User
    Join Date
    03-18-2012
    Location
    ID
    MS-Off Ver
    Excel 2007
    Posts
    14

    Re: Problem using VBA code for text to column (the value is repeated after separate cell)

    I'm sorry for replying too late

    1. Yes, I want to split on every TAB character
    2. Yes,
    - "X" becomes "x" for example: 2438X5X5X5 will be changed to 2438x5x5x5
    - after the first "x", becomes "." for example: 2438X5X5X5 will be changed to 2438x5.5.5
    3. Yes, I import the data from Word and use paste special: paste text to Excel

    Thank you for helping me


    Regards

+ 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