Macro to insert column with a formula

fintansmith

New Member
Joined
Dec 20, 2021
Messages
3
Office Version
  1. 2013
Platform
  1. Windows
Hi,

Im not very competent on VBA but really need a macro to insert a column at GB with the below formula -

=IF((IF(AJ4="CLOSED",BA4,IF(BE4<BP4,0,IF((AY4+AV4)>BQ4,BA4,BA4-(BQ4-(AY4+AV4)))))-BR4)>=0,(IF(AJ4="CLOSED",BA4,IF(BE4<BP4,0,IF((AY4+AV4)>BQ4,BA4,BA4-(BQ4-(AY4+AV4)))))-BR4),IF(AJ4="CLOSED",0,IF((AY4+AV4)>BR4,BA4+(0-BR4),0)))

Could anyone please give me a hand? Really struggling.

P.s the formula is above is row 4 but on row 5 all cells should be 5 etc.

Many thanks
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Hi,

Please check below code:

VBA Code:
Sub insertFormula()
    Dim lastRow As Integer
    Sheets("Sheet1").Range("GB1").Formula = "=IF((IF(AJ1='CLOSED',BA1,IF(BE1<BP1,0,IF((AY1+AV1)>BQ1,BA1,BA1-(BQ1-(AY1+AV1)))))-BR1)>=0,(IF(AJ1='CLOSED',BA1,IF(BE1<BP1,0,IF((AY1+AV1)>BQ1,BA1,BA1-(BQ1-(AY1+AV1)))))-BR1),IF(AJ1='CLOSED',0,IF((AY1+AV1)>BR1,BA1+(0-BR1),0)))"
    Range("GB1").Select
    lastRow = Sheets("Sheet1").Cells(Rows.Count, "AJ").End(xlUp).Row
    Selection.AutoFill Destination:=Range("GB1:GB" & lastRow)
End Sub
 
Upvote 0
Solution
Hi,

Please check below code:

VBA Code:
Sub insertFormula()
    Dim lastRow As Integer
    Sheets("Sheet1").Range("GB1").Formula = "=IF((IF(AJ1='CLOSED',BA1,IF(BE1<BP1,0,IF((AY1+AV1)>BQ1,BA1,BA1-(BQ1-(AY1+AV1)))))-BR1)>=0,(IF(AJ1='CLOSED',BA1,IF(BE1<BP1,0,IF((AY1+AV1)>BQ1,BA1,BA1-(BQ1-(AY1+AV1)))))-BR1),IF(AJ1='CLOSED',0,IF((AY1+AV1)>BR1,BA1+(0-BR1),0)))"
    Range("GB1").Select
    lastRow = Sheets("Sheet1").Cells(Rows.Count, "AJ").End(xlUp).Row
    Selection.AutoFill Destination:=Range("GB1:GB" & lastRow)
End Sub
Hi,
Apologies, the column i need to insert is at BG not GB as initially stated. Sheet1 is also called "Lloyds Bdx".

I have copied your code and replaced all 'GB' with BG and Sheet1 with "Lloyds Bdx" but am still getting Rune-time error below and cant figure it out

1640021296589.png


1640021427438.png


Thanks so much for your help.
 
Upvote 0
I managed to get it thanks to your help @Saurabhj. The problem was the 'closed' in the code. When changed to ""closed"" it worked perfectly. Code I ended up using was;

VBA Code:
Sub insertFormula()
    Dim lastRow As Integer
    Sheets("Lloyds Bdx").Range("BG1").EntireColumn.Insert
    Sheets("Lloyds Bdx").Range("BG4").Formula = "=IF((IF(AJ4=""CLOSED"",BA4,IF(BE4<BP4,0,IF((AY4+AV4)>BQ4,BA4,BA4-(BQ4-(AY4+AV4)))))-BR4)>=0,(IF(AJ4=""CLOSED"",BA4,IF(BE4<BP4,0,IF((AY4+AV4)>BQ4,BA4,BA4-(BQ4-(AY4+AV4)))))-BR4),IF(AJ4=""CLOSED"",0,IF((AY4+AV4)>BR4,BA4+(0-BR4),0)))"
    Range("BG4").Select
    lastRow = Sheets("Lloyds Bdx").Cells(Rows.Count, "AJ").End(xlUp).Row
    Selection.AutoFill Destination:=Range("BG4:BG" & lastRow)
End Sub

Thanks a million for your help!
 
Upvote 0
You can also get rid of the Select & Selection (which can slow down the code) like
VBA Code:
Sub insertFormula()
    Dim lastRow As Integer
    Sheets("Lloyds Bdx").Range("BG1").EntireColumn.Insert
    Sheets("Lloyds Bdx").Range("BG4:BG" & lastRow).Formula = "=IF((IF(AJ4=""CLOSED"",BA4,IF(BE4<BP4,0,IF((AY4+AV4)>BQ4,BA4,BA4-(BQ4-(AY4+AV4)))))-BR4)>=0,(IF(AJ4=""CLOSED"",BA4,IF(BE4<BP4,0,IF((AY4+AV4)>BQ4,BA4,BA4-(BQ4-(AY4+AV4)))))-BR4),IF(AJ4=""CLOSED"",0,IF((AY4+AV4)>BR4,BA4+(0-BR4),0)))"
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,918
Messages
6,122,257
Members
449,075
Latest member
staticfluids

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