bearcub
Well-known Member
- Joined
- May 18, 2005
- Messages
- 734
- Office Version
- 365
- 2013
- 2010
- 2007
- Platform
- Windows
I have this macro that I just recorded using the macro recorder and it is way too long and messy. I would like a more efficient way of how to do this (so I could write something like this on my own).
I have 6 columns in a report where i want to insert a blank column, rename each one of them and then add the Len function to the column to the left of it to count the number of characters.
The range of columns that will be inserted is from Column K:R. After all the columns have been inserted the new range extends from K:Z (including the 8 additional columns)
The first column that will be inserted will be in column K (note that the Len function points to K2).
Here is a snippet of the code I got from the macro recorder:
I was thinking that i could use a For Next statement to add the columns but I'm not sure how to go about doing this.
I have the sheet as an Excel Table so the formulas will fill down automatically (there are presently over 67,000 rows on the spreadsheet).
Thank you for your help in Advance,
Michael
I have 6 columns in a report where i want to insert a blank column, rename each one of them and then add the Len function to the column to the left of it to count the number of characters.
The range of columns that will be inserted is from Column K:R. After all the columns have been inserted the new range extends from K:Z (including the 8 additional columns)
The first column that will be inserted will be in column K (note that the Len function points to K2).
Here is a snippet of the code I got from the macro recorder:
Code:
Sub InsertColumn_PhoneCount()
'
' InsertColumn_PhoneCount Macro
'
On Error Resume Next
Columns("L:L").Select
Selection.Insert Shift:=xlToRight
Range("Table1[[#Headers],[Column1]]").Select
ActiveCell.FormulaR1C1 = "Primary Count"
Columns("N:N").Select
Selection.Insert Shift:=xlToRight
Range("Table1[[#Headers],[Phone Count]]").Select
ActiveCell.FormulaR1C1 = "Home Count"
Selection.Copy
Range("Table1[[#Headers],[Column1]]").Select
ActiveSheet.Paste
Columns("N:N").Select
Application.CutCopyMode = False
Selection.Copy
Columns("P:P").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "=LEN(K2)"
Range("L2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Range("N2").Select
ActiveSheet.Paste
Range("P2").Select
ActiveSheet.Paste
Range("R2").Select
ActiveSheet.Paste
Range("T2").Select
ActiveSheet.Paste
Range("V2").Select
ActiveSheet.Paste
Range("X2").Select
ActiveSheet.Paste
Range("Z2").Select
ActiveSheet.Paste
I was thinking that i could use a For Next statement to add the columns but I'm not sure how to go about doing this.
I have the sheet as an Excel Table so the formulas will fill down automatically (there are presently over 67,000 rows on the spreadsheet).
Thank you for your help in Advance,
Michael