3

enter image description here

I have the data in the image I would like to separate. The code in the image doesn't separate once it gets to the semicolon and indentation. I would like to separate the words onto a new sheet so I'm about to search through the array for certain words.

Thanks for the help!

5
  • 1
    It is always good to provide some test data. Commented Jun 22, 2016 at 15:24
  • Instead of splitting each line it stops after the first one and if there were underlines those would disappear as well Commented Jun 22, 2016 at 15:32
  • 2
    Because you are not looping the rows. Commented Jun 22, 2016 at 15:32
  • What should I change to get the rows looping?\ Commented Jun 22, 2016 at 16:47
  • So you have data in column A and you want to split it and put it in column B, C, etc...? Commented Jun 22, 2016 at 17:49

3 Answers 3

1

The following code will not only split the data, but also copy over the formatting, which it seems you also wanted. Assumes data is in Column A

Option Explicit
Sub SplitWithFormat()
    Dim R As Range, C As Range
    Dim i As Long, V As Variant

Set R = Range("a1", Cells(Rows.Count, "A").End(xlUp))
For Each C In R
    With C
        .TextToColumns Destination:=.Offset(0, 1), DataType:=xlDelimited, _
        consecutivedelimiter:=True, Tab:=False, semicolon:=False, comma:=False, _
        Space:=True, other:=False

        .Copy
        Range(.Offset(0, 1), Cells(.Row, Columns.Count).End(xlToLeft)).PasteSpecial xlPasteFormats
    End With
Next C
Application.CutCopyMode = False

End Sub

enter image description here

Sign up to request clarification or add additional context in comments.

8 Comments

Is the outcome of the data considered an array?
@johndoe253 What definition of array are you considering? And why might you think the outcome to not be an array?
well I would like to be able to search through the outcome for certain words and Im not sure what search function to use
@johndoe253 Your response doesn't really indicate what you mean by array and why you think the results might not be considered an array. With regard to searching for words, there are a number of different ways to do this, making it quite a broad question. I suggest you formulate a new question, and follow the guidelines laid out in HELP for How do I Ask a Good Question, and also How to create a Minimal, Complete, and Verifiable example
A question I had about your code: When I run the code if there are and places where theres an indentation it stops and moves to the next cell. A semicolon separates the beginning of the indentation so I changed it to true but it doesn't seem to work
|
1

This following code will assume you have data in column A. It will put values in column B, C, etc...

Sub find()

    Dim s As String
    Dim Data As Variant
    Dim i As Integer

    NumRows = ActiveSheet.Range("A1048576").End(xlUp).Row
    s = ActiveCell.Value
    Data = Split(s, " ")

    For i = 0 To NumRows
        Data = Split(Cells(i + 1, 1), " ")
        x = 2
        For j = 0 To UBound(Data)
            Cells(i + 1, x).Value = Data(j)
            x = x + 1
        Next j
    Next i

End Sub

1 Comment

Thanks for the help! It's much appreciated!
1

The following code will work for an array of size 100, you can modify it to a higher value, or use a dynamic array with REDIM.

Sub find()

Dim s As String
Dim Data(100) As Variant
Dim i As Integer

For i = 1 To Range("A1").End(xlDown).Row
    Cells(i, 1).Font.Underline = True
    Cells(i, 2).Value = FindWord(Cells(i, 1), 2) ' change 2 to whatever word position in the original string you want to copy to the right column
Next i

End Sub

' And I added this function

Function FindWord(Source As String, Position As Integer)

Dim cell_strarr() As String
cell_strarr = Split(Source, " ")
strWordCount = UBound(cell_strarr)
If strWordCount < 1 Or (Position - 1) > strWordCount Or Position < 0 Then
    FindWord = ""
Else
    FindWord = cell_strarr(Position - 1)
End If

End Function

5 Comments

The code works but only splits the first word and move it over one cell. It also removes underlines. How can you fix it?
@johndoe253, I've added a row for the underline. What do you want to have in the original column ? The whole string ? What do you want to have in the second column ?
I would like to keep the original string in the left and create the splits next to the original column
@johndoe253, try the updated code now. You can adjust the parameters you want to retrieve from the original string by modifying the value 2 when calling the Function FindWord(Source As String, Position As Integer)
Thank you for the help! It was much appreciated!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.