+ Reply to Thread
Results 1 to 3 of 3

Calculation formula on Change by Val

Hybrid View

  1. #1
    Registered User
    Join Date
    08-04-2014
    Location
    Stockholm
    MS-Off Ver
    2013
    Posts
    58

    Calculation formula on Change by Val

    Hi,

    I use the code below push a calculation formula to U11:U.
    I want to execute it with a change by val event. If a cell in the range R11:R511 is changed I want
    to execute
     "=T11/S11"
    .
    However I only want the macro to set the formula on the same row as the cell that has been changed in R11:R511.

    But I can't get it to work. Any help ?: )



    
    Sub CalcTg()
    
    Dim LastRow As Long
         
        LastRow = Cells(Rows.count, "O").End(xlUp).Row
        
            If WorksheetFunction.CountA(Range("O11:O511")) >= 1 Then
        
            sheets("Kalkyl").Range("U11:U" & LastRow).Formula = "=T11/S11"
        
            End If
    
    End Sub

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    49,834

    Re: Calculation formula on Change by Val

    You need a worksheet change event handler in the sheet where you are making changes:

    Option Explicit
    
    Private Sub Worksheet_Change(ByVal Target As Range)
    
    Dim lLR As Long
    Dim rInterest As Range, cell As Range
    
    lLR = Range("R" & Rows.Count).End(xlUp).Row
    lLR = IIf(lLR < 11, 11, lLR)         ' minimum range R11
    Set rInterest = Range("R11:R" & lLR) ' minimum range R11
    
    ' ignore any changes outside the "range of interest"
    If Intersect(Target, rInterest) Is Nothing Then Exit Sub
    
    ' for every cell in the range, add the formula
    Application.EnableEvents = False
    For Each cell In Target
        Range("U" & cell.Row).Formula = _
            "=IFERROR(T" & cell.Row & "/S" & cell.Row & ","""")"
    Next 'cell
    Application.EnableEvents = True
    
    End Sub

    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    49,834

    Re: Calculation formula on Change by Val

    If you are satisfied with the solution(s) provided, please mark your thread as Solved.


    New quick method:
    Select Thread Tools-> Mark thread as Solved. To undo, select Thread Tools-> Mark thread as Unsolved.

    Or you can use this way:

    How to mark a thread Solved
    Go to the first post
    Click edit
    Click Go Advanced
    Just below the word Title you will see a dropdown with the word No prefix.
    Change to Solved
    Click Save


    You may also want to consider thanking those people who helped you by clicking on the little star at the bottom left of their reply to your question.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. How to write the Formula for calculation day of inventory when month has change
    By Akatecho in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 05-20-2014, 02:28 PM
  2. How to make a formula post to a different place after each change in calculation
    By frenkile in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-25-2011, 10:15 AM
  3. Calculation Source Change
    By casdaq in forum Excel General
    Replies: 3
    Last Post: 03-21-2011, 03:58 PM
  4. [SOLVED] Percentage of change calculation
    By maddog9486 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 01-20-2009, 05:28 PM
  5. % Change calculation
    By al in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-08-2005, 01:55 AM

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