Macro to Copy Column

Michael151

Board Regular
Joined
Sep 20, 2010
Messages
247
Hello all,

I’m trying to write a macro that will copy a column, then change the header of the new column in row 1:

In the column labeled “New_Deal”, make a copy of this column by inserting a new column to the right, then change the header of the new column to “New_Deal_2”

Before sheet:

Row1 Col1 New_Deal
Row2 Col1 Col2
Row3 Col1 Col2
Row4 Col1 Col2
Row5 Col1 Col2

After Macro:

Row1 Col1 New_Deal New_Deal_2
Row2 Col1 Col2 Col2
Row3 Col1 Col2 Col2
Row4 Col1 Col2 Col2
Row5 Col1 Col2 Col2

The New_Deal column can be in any column letter, which is why I’d like to use the header in row 1 to identify.

Any help is most appreciated – thanks!
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Two separate functions actually, but similar.

I'm trying to write a macro that will make a copy of a column, then change the header of that new column.

The second function is similar, but instead of copying a column, I'm trying to simply fill the column with one specific word.
 
Upvote 0
Try :
Code:
Option Explicit
Sub IWillNeverDoublePostAgain()
Dim c As Range
Dim Count As Integer
Count = 2
For Each c In Sheets("Sheet1").Range("1:1")
    If c.Value = "New_Deal" Then
        c.EntireColumn.Copy
        c.Offset(0, 1).Insert Shift:=xlToRight
        c.Offset(0, 1).Value = "New_Deal" & Count
        Count = Count + 1
    End If
Next c
Application.CutCopyMode = False
End Sub
 
Upvote 0
Ha ha clever sub name stnkynts...

This almost works perfectly. The only thing is that the macro changes the new column name to New_Deal2. Even if I try changing the name to something else, it adds a 2 at the end of the name. Any ideas?
 
Upvote 0
Ahh you wanted it to be New_Deal_2 not New_Deal2 correct? Also the only reason i through the count in there was incase you had multiple instances of New_Deal in the columns. If this never happens then just remove all the counts and change the parenthesis to be exactly what u want.

Try changing this line:
Code:
        c.Offset(0, 1).Value = "New_Deal" & Count

To this:
Code:
        c.Offset(0, 1).Value = "New_Deal_" & Count

I am pretty sure you still want the new column to be shifted to the right and the title changed if this is incorrect let me know.
 
Upvote 0
I don't want it to have a 2 at the end (sorry for the confusion, I just read my original post).

Perhaps changing to a different name would work better. How about the new column simply be "market_right_id" without a 2 at the end.

Sorry for the back and forth - thanks for the help
 
Upvote 0

Forum statistics

Threads
1,224,600
Messages
6,179,836
Members
452,947
Latest member
Gerry_F

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top