VBA to automatically insert column, the inser formula in the column and finally, copy down the formula in the entire col.

Megha1484

New Member
Joined
Nov 10, 2018
Messages
17
Hi,

Need some help with the VBA code. I am trying to insert two columns, insert formulas in those two columns
and then eventually want to copy down the formula in the entire column. I am only half way the code and stuck at the "Thisworkbook.Worksheets("Transposed Data").


I got this code online, modified a bit to fit my needs, but can't seem to figure out this error.
Any help would be greatly appreciated.
Thanks!



Sub AddColumns()
'Inserts Two Columns at e and f
Worksheets(1).Range("e:f").EntireColumn.Insert
Worksheets(1).Range("e1").Formula = "Score%"
Worksheets(1).Range("f1").Formula = "Month"
'Inserts specific formulae to cells e2 and f2
Dim Formulas(1 To 2) As Variant
With ThisWorkbook.Worksheets("Transposed Data")???? SCRIPT OUT OF RANGE ERROR
Formulas(1) = "=IFERROR(c2/SUM(D:D), "")"
Formulas(2) = "=Month(G2)"
.Range("E2:F2").Formula = Formulas
'Changes number format in Columns E and F to general
.Range("E:F").NumberFormat = "General"
End With
End Sub
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
It means that you don't have a sheet called "Transposed Data" in the workbook containing the code.
 
Upvote 0
Oops! I did have a spreadsheet named that, but with wrong spelling. I did not catch it until you mentioned about it. Thanks a lot!
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0
Hello!

I am trying to accomplish kind of the same thing. Except I need to insert 5 columns. I created a button inside a UserForm that when clicked it inserts the columns.
I have 5 columns C-G. I need to insert 5 new columns, with the same headings, formats and formulas to be the new C-G, having the previous columns move over and now be H-L. The headings to each column begin on row 3. Row 2 above each group of columns is merged to input a date.

Any help would be appreciated.
 
Upvote 0
Can you please start a thread of your own.
Thanks
 
Upvote 0

Forum statistics

Threads
1,213,543
Messages
6,114,238
Members
448,555
Latest member
RobertJones1986

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