Forum Discussion
help making a loop (to automate table formatting)
- Sep 05, 2019
This should do what you are trying to achieve....
Sub FormatTables() Dim c As Long Dim lr As Long Dim i As Long Application.ScreenUpdating = False c = 1 For i = 1 To 24 lr = Cells(Rows.Count, c + 1).End(xlUp).Row If lr > 2 Then Cells(3, c).AutoFill Destination:=Range(Cells(3, c), Cells(lr, c)) Cells(3, c + 4).AutoFill Destination:=Range(Cells(3, c + 4), Cells(lr, c + 4)) Cells(3, c + 5).AutoFill Destination:=Range(Cells(3, c + 5), Cells(lr, c + 5)) End If c = c + 18 Next i Application.ScreenUpdating = True End Sub
This should do what you are trying to achieve....
Sub FormatTables()
Dim c As Long
Dim lr As Long
Dim i As Long
Application.ScreenUpdating = False
c = 1
For i = 1 To 24
lr = Cells(Rows.Count, c + 1).End(xlUp).Row
If lr > 2 Then
Cells(3, c).AutoFill Destination:=Range(Cells(3, c), Cells(lr, c))
Cells(3, c + 4).AutoFill Destination:=Range(Cells(3, c + 4), Cells(lr, c + 4))
Cells(3, c + 5).AutoFill Destination:=Range(Cells(3, c + 5), Cells(lr, c + 5))
End If
c = c + 18
Next i
Application.ScreenUpdating = True
End Sub
- gms4bSep 05, 2019Brass Contributor
AMAZING!!! I spent, like, hours yesterday entering all of the cells names in manually...and this does it perfectly! Even better, there are other actions that I need to automate on the same data set as well. I'm hoping that I can just modify this sub and use it in other ways as well.
Thank you so much!!!!
Greg
- Subodh_Tiwari_sktneerSep 05, 2019Silver Contributor
You're welcome Greg! Glad it worked as desired.
Thanks for the feedback.
Please take a minute to accept my post with the proposed solution as an Answer and hit the like button. 🙂
Subodh
- gms4bSep 05, 2019Brass Contributor
Haha! I used your sub as a template and this new sub goes back and merges those cells that you did the autofill on. Worked great!
Actually, instead of 2 subs, I could probably just merge the two subs together...
Greg
Sub mergecells()
Dim c As Long
Dim lr As Long
Dim i As LongApplication.ScreenUpdating = False
Application.DisplayAlerts = Falsec = 1
For i = 1 To 26
lr = Cells(Rows.Count, c + 1).End(xlUp).Row
If lr > 2 Then
With Sheets("QC Chart 2 Prep")
.Range(.Cells(3, c), .Cells(lr, c)).Merge
.Range(.Cells(3, c + 4), .Cells(lr, c + 4)).Merge
.Range(.Cells(3, c + 5), .Cells(lr, c + 5)).Merge
End With
End If
c = c + 18
Next iApplication.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub