1

I am trying to split cells if contain "," to next row i am using this formula to get data from sheet1 (=Sheet1!B3) and so on and data is in B3:AF50. i use this vba code

Sub splitcells()
Dim InxSplit As Long
Dim SplitCell() As String
Dim RowCrnt As Long

 With Worksheets("Sheet2")

 RowCrnt = 3

 Do While True


  If .Cells(RowCrnt, "b").Value = "" Then
    Exit Do
  End If

  SplitCell = Split(.Cells(RowCrnt, "b").Value, ",")

  If UBound(SplitCell) > 0 Then

    .Cells(RowCrnt, "b").Value = SplitCell(0)


    For InxSplit = 1 To UBound(SplitCell)
      RowCrnt = RowCrnt + 1

      .Cells(RowCrnt, "b").Value = SplitCell(InxSplit)

      .Cells(RowCrnt, "B").Value = .Cells(RowCrnt - 1, "B").Value
    Next
  End If

  RowCrnt = RowCrnt + 1

  Loop

End With

End Sub

Problem is

  • I don't know how to give range(B3:Af50) in this code
  • This code is not keeping my code (=Sheet1!)
  • it is just copying the value before ","

my cell values are like that

B3 ABC,XYZ,KKK,LLL i want it to split as B3 = ABC and B4 = XYZ B5 = KKK b6 = LLL

and if value in B3 is changed it should clear the cells (B4,B5,B6)splitted early and update if split required criteria ","

Have sheet like that

enter image description here

after split should look like that with formula intact

enter image description here

1 Answer 1

3

No VBA is needed.

With data in Sheet1 cell B3, put this in any cell of any sheet:

=TRIM(MID(SUBSTITUTE(Sheet1!$B3,",",REPT(" ",999)),COLUMNS($A:A)*999-998,999))

and copy across.

enter image description here

EDIT#1:

To copy downwards, use this formula instead:

=TRIM(MID(SUBSTITUTE(Sheet1!B$3,",",REPT(" ",999)),ROWS($1:1)*999-998,999))
Sign up to request clarification or add additional context in comments.

4 Comments

it is splitting it into Columns and also if value in Sheet 1 is change it is not updating sheet 2
@MuhammadKhurrumButt See my EDIT#1:
Edit#1 is working good but only in first 4 rows not covering full range B3:AF50
@MuhammadKhurrumButt i think you need to change address eg =Sheet1!B4 and so on

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.