Formula to Cell issue

Ampleford

Active Member
Joined
Mar 26, 2002
Messages
380
I'm trying to add a formula to a cell using VB and it keeps stopping, but I can't see where I'm going wrong - can someone maybe help me? I'd like the following formula in Cell BA2 on the tab "Theo"

=IF(SUM(AC2:AD2)=0,"XX",AZ2&"~"&SUM(AC2:AD2)&"-"&Y2) so I'm using the following:-

Sub CalcsToData()
Sheets("Theo").Select
Range(BA2).Value = "=IF(SUM(AC2:AD2)=0,Chr34XXChr34,AZ2&Chr34~Chr34&SUM(AC2:AD2)&Chr34-Chr34&Y2)"
End Sub

But I'm getting the following:-

Run-time error '1004':
Method 'Range' of object '_Global' failed

Can anyone assist?
1618476270140.png
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
If you want See formula Try Method 1 AND if you want only Result Try Method 2.
Method 1:
VBA Code:
Sub Macro1()
Sheets("Theo").Select
Range(BA2).FormulaR1C1 = "=IF(SUM(RC[-24]:RC[-23])=0,""XX"",RC[-1]&""~""&SUM(RC[-24]:RC[-23])&""-""&RC[-28])"
End Sub
Method 2
VBA Code:
Sub Macro2()
Sheets("Theo").Select
If Application.WorksheetFunction.Sum(Range("AC2:AD2")) = 0 Then
Range(BA2).Value = "XX"
Else
Range(BA2).Value = Range("AZ2").Value & "~" & Application.WorksheetFunction.Sum(Range("AC2:AD2")) & "-" & Range("Y2").Value
End If
End Sub
 
Upvote 0
Hi, another option.

Sheets("Theo").Range("BA2").Formula= "=IF(SUM(AC2:AD2)=0,""XX"",AZ2&""~""&SUM(AC2:AD2)&""-""&Y2)"
 
Upvote 0
Solution
If you want See formula Try Method 1 AND if you want only Result Try Method 2.
Method 1:
VBA Code:
Sub Macro1()
Sheets("Theo").Select
Range(BA2).FormulaR1C1 = "=IF(SUM(RC[-24]:RC[-23])=0,""XX"",RC[-1]&""~""&SUM(RC[-24]:RC[-23])&""-""&RC[-28])"
End Sub
Method 2
VBA Code:
Sub Macro2()
Sheets("Theo").Select
If Application.WorksheetFunction.Sum(Range("AC2:AD2")) = 0 Then
Range(BA2).Value = "XX"
Else
Range(BA2).Value = Range("AZ2").Value & "~" & Application.WorksheetFunction.Sum(Range("AC2:AD2")) & "-" & Range("Y2").Value
End If
End Sub


I'm going to copy the formula down, so using Method 1, but I'm still getting the same message:-
1618481537820.png
 
Upvote 0
Sorry my fault:
Method1
VBA Code:
 Sub Macro1() 
Sheets("Theo").Select 
Range("BA2").FormulaR1C1 = "=IF(SUM(RC[-24]:RC[-23])=0,""XX"",RC[-1]&""~""&SUM(RC[-24]:RC[-23])&""-""&RC[-28])"
 End Sub

Method 2
VBA Code:
 Sub Macro2() 
Sheets("Theo").Select
 If Application.WorksheetFunction.Sum(Range("AC2:AD2")) = 0 Then
 Range("BA2").Value = "XX" 
Else
 Range("BA2").Value = Range("AZ2").Value & "~" & Application.WorksheetFunction.Sum(Range("AC2:AD2")) & "-" & Range("Y2").Value
 End If 
End Sub
 
Upvote 0
I'm going to copy the formula down

HI, if you intend to do this programatically, then instead of entering the formula in a single cell and copying it down, you can do it all in one step. For example.

Sheets("Theo").Range("BA2:BA10").Formula = "=IF(SUM(AC2:AD2)=0,""XX"",AZ2&""~""&SUM(AC2:AD2)&""-""&Y2)"
 
Upvote 0
HI, if you intend to do this programatically, then instead of entering the formula in a single cell and copying it down, you can do it all in one step. For example.

Sheets("Theo").Range("BA2:BA10").Formula = "=IF(SUM(AC2:AD2)=0,""XX"",AZ2&""~""&SUM(AC2:AD2)&""-""&Y2)"
That would be even gooder, but the range is going to be dynamic, in that there will be an unknown number of rows in each iteration of the sheet as we're reconciling a set of journals which can vary in quantity each month, but thank you very much for that , and I'm sure I will be able to use that in future
 
Upvote 0
Sorry my fault:
Method1
VBA Code:
 Sub Macro1()
Sheets("Theo").Select
Range("BA2").FormulaR1C1 = "=IF(SUM(RC[-24]:RC[-23])=0,""XX"",RC[-1]&""~""&SUM(RC[-24]:RC[-23])&""-""&RC[-28])"
End Sub

Method 2
VBA Code:
 Sub Macro2()
Sheets("Theo").Select
If Application.WorksheetFunction.Sum(Range("AC2:AD2")) = 0 Then
Range("BA2").Value = "XX"
Else
Range("BA2").Value = Range("AZ2").Value & "~" & Application.WorksheetFunction.Sum(Range("AC2:AD2")) & "-" & Range("Y2").Value
End If
End Sub

Thanks very much for this. It's appreciated...
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,749
Members
448,989
Latest member
mariah3

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