* Urgent VBA formula help

storer

New Member
Joined
Jul 21, 2010
Messages
15
Hi guys

I am doing a programme at work and I am struggling to finish the macro.

I am running a macro in a cell (j8), but I want to offset to the cell to the right and insert the following formula:

=J8-((three worksheets to the left)J8)

The formula i have is below

Function addValue(number) 'Add value into the value box
x = ActiveSheet.Name 'active worksheet denotated as x
If (number >= 0) Then
Sheets(x).Activate
Worksheets(x).Cells(8, 10) = number
addValue = True
Else
addValue = False
End If
Range("K8").Select
ActiveCell.FormulaR1C1 = "=RC[-1]-sheets(sheetcount-2)RC[-1]"
End Function

If anyone can help it would be massively appreciated.

Kind regards

Storer
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Code:
Worksheets(Activesheet.Index - 3).Cells(8, 11).formula = Activesheet.Cells(8, 10).formula
 
Upvote 0
From: ActiveCell.FormulaR1C1 = "=RC[-1]-sheets(sheetcount-2)RC[-1]"



To: ActiveCell.Formula = "=j8-'Sheet" & Sheets.Count - 2 & "'!j8"
 
Upvote 0
From: ActiveCell.FormulaR1C1 = "=RC[-1]-sheets(sheetcount-2)RC[-1]"



To: ActiveCell.Formula = "=j8-'Sheet" & Sheets.Count - 2 & "'!j8"

JLGWhiz, this is the best of the two posted. I can get it to put everything in the speech marks in when i don't have the = sign. When it runs with the = sign it comes up with an error (runtime error 1004) application defined or object defined error.

Its really close i just need this last step.

AS
 
Upvote 0
Hi storer,

Try this:

Code:
If InStr(Sheets(ActiveSheet.Index - 3).Name, " ") > 0 Then
        ActiveCell.Formula = "=J8-'" & Sheets(ActiveSheet.Index - 3).Name & "'!J8"
    Else
        ActiveCell.Formula = "=J8-" & Sheets(ActiveSheet.Index - 3).Name & "!J8"
    End If

HTH

Robert
 
Upvote 0
JLGWhiz, this is the best of the two posted. I can get it to put everything in the speech marks in when i don't have the = sign. When it runs with the = sign it comes up with an error (runtime error 1004) application defined or object defined error.

Its really close i just need this last step.

AS

I suspect that if you typed the line of code in instead of copy and paste, that there might be a typo involving the ( "" ) marks and the ( ' ) mark. It has to end up as =j8 - 'Sheet#'!j8 so the placement of the double quotes and the apotrophes are critical.

Or you could just do it with code:

Range("K8") = Range("J8").Value - Sheets(Sheets.Count-2).Range("J8").Value
 
Upvote 0

Forum statistics

Threads
1,214,636
Messages
6,120,668
Members
448,977
Latest member
moonlight6

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