Consulting

Results 1 to 4 of 4

Thread: loop sum

  1. #1
    VBAX Regular
    Joined
    Apr 2022
    Posts
    9
    Location

    loop sum

    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

  2. #2
    VBAX Expert Dave's Avatar
    Joined
    Mar 2005
    Posts
    832
    Location
    Hi Kared. Remove these variables from your sub and put them at the top of your code sheet. HTH. Dave
    Dim dodawanie
    Dim ileluk

  3. #3
    VBAX Regular
    Joined
    Apr 2022
    Posts
    9
    Location
    Quote Originally Posted by Dave View Post
    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.

  4. #4
    VBAX Expert Dave's Avatar
    Joined
    Mar 2005
    Posts
    832
    Location
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •