Adding a new column to end of a named range

gaudrco

Board Regular
Joined
Aug 16, 2019
Messages
203
I have a named range on sheet Competitor Comparison Data called “CCData”. I would like to add a new column to the end of the table every time I press a button. With each button press, a new column is added to the end of the range and is then included in the named range. The new column should retain the formatting of the previously column entirely. Here is a bit of code that I’ve tried to use but it’s not right.

Code:
Private Sub CommandButton1_Click()

With Sheets("Competitor Comparison Data").Range("CCData)+1 .EntireColumn.Insert
        .Range("CCData)-1 .EntireColumn.Copy
        .Range("CCData).EntireColumn.PasteSpecial Paste:=xlPasteFormats
End With
End Sub
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
And just to clarify, "CCData" is not an actually table, its a range of cells that I defined with Name Manager
 
Last edited:
Upvote 0
maybe like this
Code:
Private Sub CommandButton1_Click()
    With Sheets("Competitor Comparison Data").Range("CCData")
        .Offset(, .Range("CCData").Columns.Count).Resize(, 1).EntireColumn.Insert
        .Offset(, .Range("CCData").Columns.Count - 1).Resize(, 1).EntireColumn.Copy
        .Offset(, .Range("CCData").Columns.Count).Resize(, 1).EntireColumn.PasteSpecial Paste:=xlPasteFormats
    End With
End Sub
 
Upvote 0
This did everything except resize the defined range 'CCData'. The reason I need the defined range to include the newcolumns added is because I need each added column to be at the end of the definedrange. With the code you put together, the new columns are always added to thesame column instead of expanding to the right. I think this code is great but just needs onemore piece that says include the new column in the defined range.


 
Upvote 0
Oops, missed that part
Rich (BB code):
Private Sub CommandButton1_Click()
    With Sheets("Competitor Comparison Data").Range("CCData")
        .Offset(, .Range("CCData").Columns.Count).Resize(, 1).EntireColumn.Insert
        .Offset(, .Range("CCData").Columns.Count - 1).Resize(, 1).EntireColumn.Copy
        .Offset(, .Range("CCData").Columns.Count).Resize(, 1).EntireColumn.PasteSpecial Paste:=xlPasteFormats
        .Name.RefersTo = .Name.RefersToRange.Resize(, .Columns.Count + 1)
    End With
End Sub
 
Upvote 0
Great! And lastly, where do I rename this new column? I want the name to be = "C2" on the active sheet.
 
Upvote 0
What you're asking would be under Name Manager on the formulas ribbon.

If what you mean is to put a header in the last column of the new "CCData" range then try this immediately before the red line
Code:
        .Offset(, .Range("CCData").Columns.Count).Resize(1, 1).Value = Range("C2").Value
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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