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

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
73,043
Office Version
  1. 365
Platform
  1. Windows
It means that you don't have a sheet called "Transposed Data" in the workbook containing the code.
 

Megha1484

New Member
Joined
Nov 10, 2018
Messages
17
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!
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
73,043
Office Version
  1. 365
Platform
  1. Windows
You're welcome & thanks for the feedback
 

harveya915

Board Regular
Joined
Sep 4, 2015
Messages
134

ADVERTISEMENT

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.
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
73,043
Office Version
  1. 365
Platform
  1. Windows
Can you please start a thread of your own.
Thanks
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,163,598
Messages
5,832,645
Members
430,150
Latest member
amitk1

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