PDA

View Full Version : [SOLVED:] loop sum



kared
05-19-2022, 03:11 PM
Hi, I want to sum the values of the same cell from different workbooks. Why is this code not working?


Sub Dosomething5() Dim dodawanie
Dim ileluk

ileluk = 0
dodawanie = 0

Dim xSh As Worksheet
Application.ScreenUpdating = False
For Each xSh In Worksheets
xSh.Select
Call RunCode5
Next
Application.ScreenUpdating = True
End Sub

Sub RunCode5()


dodawanie = Range("N10").Value
ileluk = ileluk + dodawanie
Range("M11") = ileluk




End Sub

Dave
05-19-2022, 03:48 PM
Hi Kared. Remove these variables from your sub and put them at the top of your code sheet. HTH. Dave

Dim dodawanie
Dim ileluk

kared
05-19-2022, 03:59 PM
Hi Kared. Remove these variables from your sub and put them at the top of your code sheet. HTH. Dave

Dim dodawanie
Dim ileluk

Thank you it worked. Funny I thought to do that but didn't. I thought dim couldn't be outside of sub.

Dave
05-19-2022, 04:29 PM
If you leave the variable declaration (dim) inside a sub then it is only available to that sub and it's value is gone after the sub is done. If you put it at the top of a code sheet then it is available to all subs in the code sheet and it value persists until it is changed. If you want to make a variable available to the whole project (and persist until changed) then declare it as Public at the top of module code. You are welcome. Thanks for posting your outcome. Dave