How to Tell Macro to Create New Rows Based on a Particular Column Rather Than Hard Coded Column G

I had this question after viewing How to change this macro to find last column instead of A to J?.

Sub Commas2Rows()
  ' hiker95, 05/18/2017, ME1006027
  Dim lr As Long, lc As Long, r As Long, s, i As Long
  Application.ScreenUpdating = False
  With ActiveSheet
    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(.Range("G" & r), ", ") Then
        s = Split(.Range("G" & r), ", ")
        .Rows(r + 1).Resize(UBound(s)).Insert
        .Range("G" & r).Resize(UBound(s) + 1) = Application.Transpose(s)
        .Range("A" & r + 1 & ":F" & r + 1).Resize(UBound(s)).Value = .Range("A" & r & ":F" & r).Value
'        .Range("H" & r + 1 & ":J" & r + 1).Resize(UBound(s)).Value = .Range("H" & r & ":J" & r).Value
        .Range("H" & r + 1 & ":" & Chr(64 + lc) & r + 1).Resize(UBound(s)).Value = .Range("H" & r & ":" & Chr(64 + lc) & r).Value
      End If
    Next r
  End With
  Application.ScreenUpdating = True
End Sub
LVL 1
Alex CampbellAsked:
Who is Participating?

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

x
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 PrewCommented:
I think this should handle that.

Sub Commas2Rows()
  ' hiker95, 05/18/2017, ME1006027
  Dim lr As Long, lc As Long, r As Long, s, i As Long, SplitCol As String, SplitBefore As String, SplitAfter As String

  SplitCol = "G"
  SplitBefore =  Chr(Asc(SplitCol)-1)
  SplitAfter = Chr(Asc(SplitCol)+1)

  Application.ScreenUpdating = False

  With ActiveSheet
    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(.Range(SplitCol & r), ", ") Then
        s = Split(.Range(SplitCol & r), ", ")
        .Rows(r + 1).Resize(UBound(s)).Insert
        .Range(SplitCol & r).Resize(UBound(s) + 1) = Application.Transpose(s)
        .Range("A" & r + 1 & ":" & SplitBefore & r + 1).Resize(UBound(s)).Value = .Range("A" & r & ":" & SplitBefore & r).Value
        .Range(SplitAfter & r + 1 & ":" & Chr(64 + lc) & r + 1).Resize(UBound(s)).Value = .Range(SplitAfter & r & ":" & Chr(64 + lc) & r).Value
      End If
    Next r
  End With

  Application.ScreenUpdating = True
End Sub

Open in new window


»bp
0
Bill PrewCommented:
And if it were me I would probably switch from .Range() referencing to .Cells() referencing for this, to avoid the concatenations and character conversions, etc.  Would be a bit simpler and easier to follow I suspect.


»bp
0
Alex CampbellAuthor Commented:
That works, but it would be great if you would stop and ask which column to work with it you would.
0
Bill PrewCommented:
Sub Commas2Rows()
  ' hiker95, 05/18/2017, ME1006027
  Dim lr As Long, lc As Long, r As Long, s, i As Long, SplitCol As String, SplitBefore As String, SplitAfter As String

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

  Application.ScreenUpdating = False

  With ActiveSheet
    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(.Range(SplitCol & r), ", ") Then
        s = Split(.Range(SplitCol & r), ", ")
        .Rows(r + 1).Resize(UBound(s)).Insert
        .Range(SplitCol & r).Resize(UBound(s) + 1) = Application.Transpose(s)
        .Range("A" & r + 1 & ":" & SplitBefore & r + 1).Resize(UBound(s)).Value = .Range("A" & r & ":" & SplitBefore & r).Value
        .Range(SplitAfter & r + 1 & ":" & Chr(64 + lc) & r + 1).Resize(UBound(s)).Value = .Range(SplitAfter & r & ":" & Chr(64 + lc) & r).Value
      End If
    Next r
  End With

  Application.ScreenUpdating = True
End Sub

Open in new window

0

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:
Great!! Thanks!
0
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
R

From novice to tech pro — start learning today.