Help with 'Spilt' function, please

kbishop94

Active Member
Joined
Dec 5, 2016
Messages
458
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
I've got this code all messed up and its not returning the correct parts of a string that I am attempting to spilt up.

Here is what I am starting with and what the original cells look like with the complete (un-spilt) data. There are 9 rows in my example. Ultimately there will be a varying number of rows so I need to make sure I have it look to see where the last row is.
$6.PNG



And here is what I am trying to get to... (except its not correct. its doubling up on some of the individual strings, I cant see where in my code that is making it do this.)


$8.PNG


Full disclosure, I found various parts on here and elsewhere when I googled what I need the code to do,and thats where I somehow got this all messed up.

my screwed up code:

Code:
Private Sub CommandButton2_Click()

    Dim LR As Long, i As Long
    Dim X As Variant
    Application.ScreenUpdating = False
    Dim iCell As Variant
    
    Dim rCol As Long
    rCol = ActiveSheet.UsedRange.Rows.Count
    For Each Cell In Range(Cells(1, rCol), Cells(1, rCol))
    Cell = Trim(Cell)

    LR = Range("A" & Rows.Count).End(xlUp).Row
    Columns("A").Insert
    MsgBox LR
    For i = LR To 1 Step -1
        With Range("B" & i)
            If InStr(.Value, " ") = 0 Then
                .Offset(, -1).Value = .Value
            Else
                X = Split(.Value, " ")
                .Offset(1).Resize(UBound(X)).EntireRow.Insert
                .Offset(, -1).Resize(UBound(X) - LBound(X) - 1).Value = Application.Transpose(X)
            End If
        End With
    Next i
    Columns("B").Delete

Next
   
    LR = Range("A" & Rows.Count).End(xlUp).Row
    With Range("A1:A" & LR)
        On Error Resume Next
        .SpecialCells(xlCellTypeBlanks).FormulaR1C1 = "=R[-1]C"
    On Error GoTo 0
        .Value = .Value
    End With

    Application.ScreenUpdating = True

End Sub

I had to put the bottom part of that code in there because it was inserting a blank row after every one of the original cells that contained the original string before getting split up.

Thanks in advance for setting me straight here. ;)
 

Attachments

  • $5.PNG
    $5.PNG
    11.3 KB · Views: 5
Try this

VBA Code:
Sub SplitData()
  Dim a As Variant, b As Variant, i As Long, j As Long, v As Variant
  a = Sheets("Sheet1").Range("A1", Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp)).Value
  ReDim b(1 To UBound(a) * 100, 1 To 1)
  For i = 1 To UBound(a)
    For Each v In Split(a(i, 1), Chr(10))
      If InStr(1, Trim(v), "ALL", vbTextCompare) = 0 And Trim(v) <> "" Then
        j = j + 1
        b(j, 1) = Trim(v)
      End If
    Next
    j = j + 1
  Next
  Sheets("Sheet2").Range("A1").Resize(j).Value = b
End Sub

Thanks for the reply. I'll give it a try first thing in the morning.
 
Upvote 0

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Of course, let me know if you have any doubt.



That fixed it. Many thanks, DanteAmor


I have another question; If I want to include other strings to exclude in addition to "ALL", how would I go about that?


Code:
If InStr(1, Trim(v), "ALL", vbTextCompare) = 0 And Trim(v) <> "" Then

like:

Code:
If InStr(1, Trim(v), "ALL", "SEA24X", vbTextCompare) = 0 And Trim(v) <> "" Then
(this doesn't work... I tried 'And' or 'Or' and that also didnt work.)
 
Upvote 0
If I want to include other strings to exclude in addition to "ALL"

Add those strings on this line within the macro.
strings = Array("ALL", "SEA24X", "Other")

VBA Code:
Sub SplitData()
  Dim a As Variant, b As Variant, i As Long, j As Long, v As Variant
  Dim strings As Variant, strg As Variant, n As Variant, k As Long
  
  strings = Array("ALL", "SEA24X", "Other")
  a = Sheets("Sheet1").Range("A1", Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp)).Value
  ReDim b(1 To UBound(a) * 100, 1 To 1)
  
  For i = 1 To UBound(a)
    For Each v In Split(a(i, 1), Chr(10))
      n = 0
      For Each strg In strings
        n = InStr(1, Trim(v), strg, vbTextCompare)
        If n > 0 Then Exit For
      Next
      If n = 0 And Trim(v) <> "" Then
        j = j + 1
        b(j, 1) = Trim(v)
      End If
    Next
    j = j + 1
  Next
  Sheets("Sheet2").Range("A1").Resize(j).Value = b
End Sub
 
Upvote 0
Add those strings on this line within the macro.
strings = Array("ALL", "SEA24X", "Other")

VBA Code:
Sub SplitData()
  Dim a As Variant, b As Variant, i As Long, j As Long, v As Variant
  Dim strings As Variant, strg As Variant, n As Variant, k As Long
 
  strings = Array("ALL", "SEA24X", "Other")
  a = Sheets("Sheet1").Range("A1", Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp)).Value
  ReDim b(1 To UBound(a) * 100, 1 To 1)
 
  For i = 1 To UBound(a)
    For Each v In Split(a(i, 1), Chr(10))
      n = 0
      For Each strg In strings
        n = InStr(1, Trim(v), strg, vbTextCompare)
        If n > 0 Then Exit For
      Next
      If n = 0 And Trim(v) <> "" Then
        j = j + 1
        b(j, 1) = Trim(v)
      End If
    Next
    j = j + 1
  Next
  Sheets("Sheet2").Range("A1").Resize(j).Value = b
End Sub

Hello DanteAmor

I'm back to playing around with this bit of code (its working great, btw) but now I'm attempting to change it up a bit, (in order to use it in a different way) but I'm not having much luck.

The part I am trying to revise deals with this line:

Code:
  strings = Array("ALL", "SEA24X", "Other")

Where as I previously requested to exclude those specific strings within the quotes, now I'm wanting to only include those listed strings instead.

In other words, instead of not including "ALL, "SEA24X" and "Other" in the output of what the code returns, I would now like to have it where it only includes those strings and ignores all others.

Can this be done with the code you supplied or would it require a completely different type of approach?

Thank you
 
Upvote 0

Forum statistics

Threads
1,214,591
Messages
6,120,431
Members
448,961
Latest member
nzskater

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top