Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #3  
Old 11-26-2020, 04:37 PM
Dave T Dave T is offline Subtract contiguous values from top cell, continuously in column, till end Windows 7 64bit Subtract contiguous values from top cell, continuously in column, till end Office 2013
Advanced Beginner
Subtract contiguous values from top cell, continuously in column, till end
 
Join Date: Nov 2014
Location: Australia
Posts: 66
Dave T is on a distinguished road
Default

Hello Purfleet,

I really appreciate your response and it has given me something to play with.

Whilst your solution works well, I have noted some problems with how I plan to use it.
I have been working on modifying what you have written (what I have done may not be the best way, so feel free to comment):
  • I only wanted it to work with the range B2:X21 as I have other formulas below, so how do I restrict it to only apply to cells from Row 2 to Row 21 and Columns B to X?
  • In my example I had data from Column B to Column I, but the number of columns from B across varies. It appears my additions may have solved this part.
  • Sometimes the values within the contiguous range may be formulas i.e. =2440/2.
    Your macro overwrites any formulas in these cells.

Here is what I have been playing with so feel free to comment:
Code:
Sub AutoSubtract_v2()
  'https://stackoverflow.com/questions/24047824/vba-excel-loop-assistance
  'https://www.msofficeforums.com/excel-programming/46058-subtract-contiguous-values-top-cell-continuously-column.html
  
  Dim Area As Range
  Dim startNum As String
  Dim SumAddr As String
  Dim i As Long
  
  On Error GoTo NoBlanks
  
  Dim N As Long
  'N = Sheet1.Cells(2, Columns.Count).End(xlToLeft).Column
  N = Cells(2, Columns.Count).End(xlToLeft).Column
  For i = 2 To N
    
NoBlanks:
    Resume Next
    
    For Each Area In Columns(i).SpecialCells(xlConstants, xlNumbers).Areas
      If Area.Count <= 1 Then GoTo Skip
      
      startNum = Area.Resize(1, 1).Address(False, False)
      SumAddr = Area.Offset(1, 0).Resize(Area.Count - 1, 1).Address(False, False)
      
      With Area.Offset(Area.Count, 0).Resize(1, 1)
        .Formula = "=" & startNum & "-SUM(" & SumAddr & ")"
        .Font.Color = vbRed
        .Font.Bold = True
        .Interior.Color = RGB(255, 255, 204)
      End With
Skip:
    Next Area
  Next i
End Sub
Regards, Dave T
Reply With Quote
 



Similar Threads
Thread Thread Starter Forum Replies Last Post
Subtract contiguous values from top cell, continuously in column, till end Novice needs: Complicated running totals adding substracting non contiguous cells contiguous columns innkeeper9 Excel 5 08-30-2016 04:43 PM
Subtract value from one cell into another otuatail Excel 3 02-01-2016 03:21 AM
Mouseover cell to indicate mouse pointer location based on Specific Row/Column values bolandk Excel 1 05-15-2014 08:22 AM
Summing Non Contiguous Cells in a Row or Column Joe Ottenhof Excel 4 12-19-2012 04:13 AM
From a particular cell till the end of the same column tinfanide Excel 3 08-10-2011 05:09 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 06:23 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft