Alex Campbell
asked on
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(xlToLef t).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)).Inser t
.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
Sub Commas2Rows()
' hiker95, 05/18/2017, ME1006027
Dim lr As Long, lc As Long, r As Long, s, i As Long
Application.ScreenUpdating
With ActiveSheet
lr = .Cells(Rows.Count, 1).End(xlUp).Row
lc = .Cells(2, Columns.Count).End(xlToLef
For r = lr To 2 Step -1
If InStr(.Range("G" & r), ", ") Then
s = Split(.Range("G" & r), ", ")
.Rows(r + 1).Resize(UBound(s)).Inser
.Range("G" & r).Resize(UBound(s) + 1) = Application.Transpose(s)
.Range("A" & r + 1 & ":F" & r + 1).Resize(UBound(s)).Value
' .Range("H" & r + 1 & ":J" & r + 1).Resize(UBound(s)).Value
.Range("H" & r + 1 & ":" & Chr(64 + lc) & r + 1).Resize(UBound(s)).Value
End If
Next r
End With
Application.ScreenUpdating
End Sub
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
»bp
ASKER
That works, but it would be great if you would stop and ask which column to work with it you would.
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
Great!! Thanks!
Open in new window
»bp