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
 

Some videos you may like

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

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
46,077
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
46,077
Office Version
  1. 365
Platform
  1. Windows
You're welcome & thanks for the feedback
 

harveya915

Board Regular
Joined
Sep 4, 2015
Messages
95

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
46,077
Office Version
  1. 365
Platform
  1. Windows
Can you please start a thread of your own.
Thanks
 

Watch MrExcel Video

Forum statistics

Threads
1,108,630
Messages
5,523,991
Members
409,554
Latest member
denistrevisan

This Week's Hot Topics

Top