How to make this macro create a new sheet and leave the original sheet alone?

Alex CampbellAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bill PrewIT / Software Engineering ConsultantCommented:
Sure, can you provide a sample workbook for testing.

Bill PrewIT / Software Engineering ConsultantCommented:
Give this a try, should insert a new sheet at the beginning of the workbook and do the spilts there...

Sub Commas2Rows()
  Dim lr As Long, lc As Long, r As Long, s, i As Long, SplitCol As String, SplitBefore As String, SplitAfter As String, NewSheet As Worksheet

  SplitCol = Application.InputBox("Please specify column letter to split on:", "Split Column", "G")
  SplitBefore = Chr(Asc(SplitCol) - 1)
  SplitAfter = Chr(Asc(SplitCol) + 1)

  With ActiveWorkbook
    .ActiveSheet.Copy Before:=.Worksheets(1)
    Set NewSheet = .Worksheets(1)
  End With
  Application.ScreenUpdating = False

  With NewSheet
    lr = .Cells(Rows.Count, 1).End(xlUp).Row
    lc = .Cells(2, Columns.Count).End(xlToLeft).Column

    For r = lr To 2 Step -1
      If InStr(.Cells(r, SplitCol), ", ") Then
        s = Split(.Cells(r, SplitCol), ", ")
        .Rows(r + 1).Resize(UBound(s)).Insert
        .Cells(r, SplitCol).Resize(UBound(s) + 1) = Application.Transpose(s)
        .Range(.Cells(r + 1, 1), .Cells(r + 1, SplitBefore)).Resize(UBound(s)).Value = .Range(.Cells(r, 1), .Cells(r, SplitBefore)).Value
        .Range(.Cells(r + 1, SplitAfter), .Cells(r + 1, lc)).Resize(UBound(s)).Value = .Range(.Cells(r, SplitAfter), .Cells(r, lc)).Value
      End If
    Next r
  End With

  Application.ScreenUpdating = True
End Sub

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Alex CampbellAuthor Commented:
Thanks, works great.
Bill PrewIT / Software Engineering ConsultantCommented:
Great, glad that helped, thanks for the feedback.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.