Button to add colums on a spreadsheet?

cloudforgiven

New Member
Joined
Nov 21, 2016
Messages
21
Hi I am new to excel 2013 vba programming so bare with me.

This code is suppose to generate a column at the location the user specified, and the column is suppose to end where the table ends, meaning if I have a 10 row table the last column box would be at 10, not line 11.
Now the code blow is suppose to do that but I keep getting an error where I put the ' ' marks. The error is a type mismatch and I dont know why? Any ideas?

The line I get the error is suppose to copy the A column, clear the contents of the copied version then paste that into the new location the user inputted.

Now I tried using macros to do this but the problem is that I cannot specify a Range because the table's rows and columns sizes are constantly changing, so I would have to manually edit the macro's range which I dont want to do.

The code works meaning it generates the column where the user wants it to, however
the lines or boxes around the cells so that you are aware that its an extension of the table are not generated which means its not copying correctly. here is img: Imgur: The most awesome images on the Internet

here is code:

Dim x AsVariant
Dim ColumnNum%
x
= InputBox("Enter a column that you want to add: ","What column?")
If x =""ThenExitSub
ColumnNum
= x
ThisWorkbook
.Sheets("Sheet1").Columns(ColumnNum).Insert shift:=xlRight

ThisWorkbook.Sheets("Sheet1").Columns(ColumnNum - 1).Range("A" & ColumnNum).Copy 'error occurs here'


ThisWorkbook
.Sheets("Sheet1").Columns(ColumnNum).ClearContents
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.

Forum statistics

Threads
1,195,623
Messages
6,010,748
Members
441,567
Latest member
Flitbee

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
Top