Results 1 to 5 of 5

Thread: Extend the macro to a selection of cells...

  1. #1
    Member
    Join Date
    Jul 2013
    Posts
    40
    Rep Power
    0

    Question Extend the macro to a selection of cells...

    I have the macro in the code below which runs successfully for the selected cell.
    I want to extend it so as to work on a selection of cells.
    I have tried with the Do Loop statement but I have not managed it.
    Any help would be appreciated.

    Code:
    Sub Splitt()
        Dim splitVals As Variant
        Dim totalVals, y As Long
        
        y = InputBox("Enter the delimiter character used (e.g., comma, semicolon, space...)")
        If y = 0 Then Exit Sub
    
        splitVals = Split(ActiveCell.Value, Chr(Asc(y)))
        totalVals = UBound(splitVals)
    
        Range(Cells(ActiveCell.Row, ActiveCell.Column + 1), Cells(ActiveCell.Row, ActiveCell.Column + 1 + totalVals)).Value = splitVals
    
    End Sub

  2. #2
    Administrator Admin's Avatar
    Join Date
    Mar 2011
    Posts
    1,123
    Rep Power
    10
    Hi

    Try

    Code:
    Sub Splitt()
        
        Dim splitVals As Variant
        Dim totalVals, y As String
        Dim i   As Long, x
        
        y = InputBox("Enter the delimiter character used (e.g., comma, semicolon, space...)")
        If y = vbNullString Then Exit Sub
        
        If Selection.Rows.Count > 1 Then
            splitVals = Application.Transpose(Selection.Value2)
            For i = 1 To UBound(splitVals)
                x = Split(splitVals(i), y)
                If UBound(x) > -1 Then
                    Selection.Cells(i, 1 + 1).Resize(, UBound(x) + 1) = x
                End If
            Next
        ElseIf Selection.Cells.Count = 1 Then
            x = Split(Selection.Value2, y)
            If UBound(x) > -1 Then
                Selection.Offset(, 1).Resize(, UBound(x) + 1) = x
            End If
        End If
        
    End Sub
    Last edited by Admin; 09-10-2013 at 01:31 PM. Reason: code edited
    Cheers !

    Excel Range to BBCode Table
    Use Social Networking Tools If You Like the Answers !

    Message to Cross Posters

    @ Home - Office 2010/2013/2016 on Win 10 (64 bit); @ Work - Office 2016 on Win 10 (64 bit)

  3. #3
    Member
    Join Date
    Jul 2013
    Posts
    40
    Rep Power
    0
    Thanks for that fast reply...

    It works fine on a selection of cells but if I select only 1 cell it gets stuck on
    Code:
    x = Split(Selection.Value2(i), y)
    .

    Also why to replace the column that the initial data exists and not place the split data in the subsequent columns?

  4. #4
    Administrator Admin's Avatar
    Join Date
    Mar 2011
    Posts
    1,123
    Rep Power
    10
    Hi

    Sorry, I should have tested the code before post. Anyway, I have edited the above code.
    Cheers !

    Excel Range to BBCode Table
    Use Social Networking Tools If You Like the Answers !

    Message to Cross Posters

    @ Home - Office 2010/2013/2016 on Win 10 (64 bit); @ Work - Office 2016 on Win 10 (64 bit)

  5. #5
    Member
    Join Date
    Jul 2013
    Posts
    40
    Rep Power
    0
    No need to apologize, you are providing valuable help. Thanks it works as intended now!!!

Similar Threads

  1. Replies: 9
    Last Post: 08-23-2013, 04:25 PM
  2. Replies: 4
    Last Post: 08-20-2013, 06:28 PM
  3. Replies: 13
    Last Post: 06-10-2013, 09:05 AM
  4. Replies: 3
    Last Post: 04-08-2012, 08:05 AM
  5. Using Selection.Address
    By Rasm in forum Excel Help
    Replies: 1
    Last Post: 11-28-2011, 05:20 AM

Posting Permissions

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