How to "operate" with a cell in a previous worksheet?

Michele317

New Member
Joined
Apr 29, 2024
Messages
20
Office Version
  1. 365
Platform
  1. Windows
Hello,
I'm trying to find a way to operate only with the previous sheet.
I explain better what I mean with an example.

I have 3 sheets: A, B e C.
In sheet A I have a value in a cell, for example 1.
In sheet B I have 2.
In sheet C I have 3.

What I want to be able to do is to have a cell in sheet B that, for example, sum the value in the same sheet (sheet B) with the value in the previous sheet (sheet A). So, in sheet B i would have in a cell the value 1+2=3
In sheet C I want to do the same. So, in a cell in sheet C I would have 3+2=5

I have tried to play with the .Count() function but I think it's a little to vulnerable and easy to break. This because, if I create a sheet and then i delete it, the whole macro broke.
I post down what I have done and that has the problem I just explained:
VBA Code:
Sub Calcolo_tot()

    Dim i As Integer
    i = Worksheets.Count - 1

    Sheets(Worksheets.Count).Range("J35").Value = Sheets(Worksheets.Count).Range("J34").Value + Worksheets(i).Range("J35")
 
End Sub

Thank you all in advance
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Hi,
I suspect you want show result in the same cell is already existed value!
based on what I understand above you can try this
VBA Code:
Sub Calcolo_tot()
Dim sh1, sh2, sh3 As Worksheet
Set sh1 = Sheets("A")
Set sh2 = Sheets("B")
Set sh3 = Sheets("C")
sh2.Range("J35").Value = sh1.Range("J34").Value + sh2.Range("J34").Value
sh3.Range("J35").Value = sh2.Range("J34").Value + sh3.Range("J34").Value
End Sub
the values should be in J34 for sheets the result will be in J35.
 
Upvote 0
Hi,
I suspect you want show result in the same cell is already existed value!
based on what I understand above you can try this
VBA Code:
Sub Calcolo_tot()
Dim sh1, sh2, sh3 As Worksheet
Set sh1 = Sheets("A")
Set sh2 = Sheets("B")
Set sh3 = Sheets("C")
sh2.Range("J35").Value = sh1.Range("J34").Value + sh2.Range("J34").Value
sh3.Range("J35").Value = sh2.Range("J34").Value + sh3.Range("J34").Value
End Sub
the values should be in J34 for sheets the result will be in J35.
Hi and thank you for the answer. The problem is that every month I make a new sheet, with a different name ("month year"). And so each month I would have to make a new variable and a new line. That's why i tried to use the counter.
 
Upvote 0
I suggest use XL2BB tool and show data before and after it until members help you.
good luck.
 
Upvote 0

Forum statistics

Threads
1,223,099
Messages
6,170,114
Members
452,302
Latest member
TaMere

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