+ Reply to Thread
Results 1 to 5 of 5

Need to subtract G4-H4 when J4 cell is clicked through VBA, this will for entire range

  1. #1
    Registered User
    Join Date
    06-10-2023
    Location
    Mumbai India
    MS-Off Ver
    2019
    Posts
    21

    Need to subtract G4-H4 when J4 cell is clicked through VBA, this will for entire range

    Need to subtract values from Column H from Column G when Column J is clicked through VBA, this will happen for the entire range i.e G4:G76, H4:H76 & J4:J76

    if only J4 cell is clicked it will subtract (G4-H4), if J5 cell is clicked it will subtract (G5-H5), if J6 cell is clicked it will subtract (G6-H6) so on and so forth and show the value in single cell "M3"

    I tried below code, this macro can be called when any cell in column J is clicked however this displays it in the entire column. I dont need that, only when respective cell in J column is clicked it should display the calculation in cell M3.

    Sub FormulasNoLoops()

    Dim LastRow As Long
    LastRow = Cells(Rows.Count, "G").End(xlUp).Row
    Range("M4:M" & LastRow) = Evaluate("G4:G" & LastRow & "-H4:H" & LastRow)

    End Sub
    Attached Files Attached Files
    Last edited by mahmad558; 06-13-2023 at 04:17 PM. Reason: some changes to explain clearly

  2. #2
    Forum Expert bebo021999's Avatar
    Join Date
    07-22-2011
    Location
    Vietnam
    MS-Off Ver
    Excel 2016
    Posts
    9,466

    Re: Need to subtract G4-H4 when J4 cell is clicked through VBA, this will for entire range

    Replace worksheet_selectChange in sheet NIFTY (sheet module) with:
    PHP Code: 
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If 
    Intersect(TargetRange("J4:J76")) Is Nothing Or Target.Count 1 Then Exit Sub
    Target
    .Offset(03).Value Target.Offset(0, -3).Value Target.Offset(0, -2).Value
    End Sub 
    Quang PT

  3. #3
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Need to subtract G4-H4 when J4 cell is clicked through VBA, this will for entire range

    Put this in the worksheet code module...

    Please Login or Register  to view this content.
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  4. #4
    Registered User
    Join Date
    06-10-2023
    Location
    Mumbai India
    MS-Off Ver
    2019
    Posts
    21

    Re: Need to subtract G4-H4 when J4 cell is clicked through VBA, this will for entire range

    This is saving values in Column M, I only want to store it in cell M3 and overide when another J cell is clicked.

  5. #5
    Forum Expert bebo021999's Avatar
    Join Date
    07-22-2011
    Location
    Vietnam
    MS-Off Ver
    Excel 2016
    Posts
    9,466

    Re: Need to subtract G4-H4 when J4 cell is clicked through VBA, this will for entire range

    May be:

    PHP Code: 
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If 
    Intersect(TargetRange("J4:J76")) Is Nothing Or Target.Count 1 Then Exit Sub
    Range
    ("M3").Value Target.Offset(0, -3).Value Target.Offset(0, -2).Value
    End Sub 

+ 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. [SOLVED] Selecting the last cell clicked in a specified range.
    By CDandVinyl in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-01-2020, 10:41 PM
  2. Click macro and then subtract from/add to the next cell clicked
    By dvs in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-02-2015, 05:52 PM
  3. [SOLVED] Load web image when cell holding URL is clicked/double-clicked
    By jchamber00 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-20-2013, 09:13 PM
  4. select entire row of clicked cell in a table
    By hb316 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-19-2013, 11:29 AM
  5. [SOLVED] code so that when a cell is clicked, a tick mark goes in the cell that was clicked
    By behnam in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 08-08-2013, 01:17 PM
  6. run macros on entire network of computers once clicked
    By myjebay1 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-19-2012, 05:55 PM
  7. [SOLVED] Load UserForm if cell is clicked in range A2:A200
    By stojko89 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-06-2011, 06:15 AM

Tags for this Thread

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