+ Reply to Thread
Results 1 to 7 of 7

Re-calculate cell everytime it is changed

Hybrid View

  1. #1
    Registered User
    Join Date
    08-11-2008
    Location
    Edinburgh
    Posts
    35

    Re-calculate cell everytime it is changed

    I have a spreadsheet where sheet("summary").Cells("K4") = sheet("component").Cells("G7").

    This is written as a formula, e.g. =SUM('Component'!G7).

    I am trying to achieve the same through VBA. I can get the summary cell to copy the others. but it just copies it the once when it is created. How can I make this re-calculate everytime it is changed?

    Sub PopulateTestsExecuted()
        
        ' Populates the Summary sheet with the tests executed per component
        Dim i As Integer, component As String, goHere As Range, fromHere As Range
        
        With frmGetSetUpData.lstComponents
            Set goHere = Sheets("Summary").Range("K4")
            For i = 0 To .ListCount - 1
            component = .List(i)
        
            Set fromHere = Sheets(component).Range("G7")
            goHere = fromHere
            
            Set goHere = goHere.Offset(i, 0)
            
            Next i
        End With
        
    End Sub
    Last edited by NBVC; 10-23-2008 at 04:30 AM.

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464
    I'm not clear why K4 isn't changing automatically when you change G7. Have you got recalculation set to manual?

    I'm also not clear what you mean by 'when it is changed'. What's it refer to? cells K4, G7 or any cell on the sheet?

    If it's a particular cell you can use the Worksheet Selection_Change event, and use the cell in question in an:

    If Target=Range(yourcell) Then
    'your code
    
    End If
    HTH

  3. #3
    Registered User
    Join Date
    08-11-2008
    Location
    Edinburgh
    Posts
    35
    What I mean by when it is changed is when the number in cell G7 changes (as there is a formula in there which adds up cells on that sheet), then I need the cell in K4 to change also!

    And calculation is set to automatic....

  4. #4
    Forum Contributor
    Join Date
    02-23-2006
    Location
    Near London, England
    MS-Off Ver
    Office 2003
    Posts
    770
    I might be wrong, but I think it is because you have linked your cells only at the time the vba macro runs?

    Sub PopulateTestsExecuted()
        
        ' Populates the Summary sheet with the tests executed per component
        Dim i As Integer, component As String, goHere As Range, fromHere As Range
        
        With frmGetSetUpData.lstComponents
            Set goHere = Sheets("Summary").Range("K4")
            For i = 0 To .ListCount - 1
            component = .List(i)
        
            Set fromHere = Sheets(component).Range("G7")
            goHere = fromHere
            
            Set goHere = goHere.Offset(i, 0)
            
            Next i
        End With
        
    End Sub
    My evaluation of what your code is doing it that you are basically starting at K4 on your summary sheet, and copying to that the value from G7 of the 'component' sheet.
    On the next loop the value is copied into K5 from G7 of THAT component, and so on, moving row by row down the summary sheet?

    If this is the case then the reason it only works once is because you are assigning the VALUE at the moment the macro is run, where as if you want to update all the time you need to make the cell contain a formuala.
    I would adjust your code as follows using the R1C1 format of addressing Rows and Columns. (I have commented your lines of code so that you can hopefully see which lines my new code is related to)

    Sub PopulateTestsExecuted()
        
        ' Populates the Summary sheet with the tests executed per component
        Dim index As Integer
        Dim component As String
        Dim goHere_row as Long
    
        'Set goHere = Sheets("Summary").Range("K4")
        goHere_row = 4 ' Row is 4, column will be 11, which is column K
        
        With frmGetSetUpData.lstComponents
            For index = 0 To .ListCount - 1
            component = .List(index)
        
            'Set fromHere = Sheets(component).Range("G7")
            'goHere = fromHere
            Worksheets("Summary").Cells(goHere_row, 11).formula= "='" & component & "'!G7" ' Remember the single quotes!
            
            'Set goHere = goHere.Offset(i, 0)
            goHere_row = goHere_row + 1  ' NOTE I don't think your line above would have done this, as the first time it would have offset by 1 row from it's current position, the next time 2 rows, then 3 etc as 'i' increased.
            
            Next index
        End With
        
    End Sub
    Note the single quotes included in the 'formula' line. These will cause the name of the worksheet to be enclosed in single quotes which allows for the fact that component might include an embedded space character.

    EDITED I got my rows and columns the wrong way around. Now corrected
    Last edited by Phil_V; 08-14-2008 at 07:34 AM.

  5. #5
    Registered User
    Join Date
    08-11-2008
    Location
    Edinburgh
    Posts
    35
    Absolutely brilliant!

    Works perfectly, thanks Phil much appreciated.

  6. #6
    Forum Contributor
    Join Date
    02-23-2006
    Location
    Near London, England
    MS-Off Ver
    Office 2003
    Posts
    770
    No problem, glad I could help. It's nice to be able to give something back to this forum

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1