+ Reply to Thread
Results 1 to 14 of 14

Faster Way to Substract 2 Columns?

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    02-04-2014
    Location
    Florida, United States
    MS-Off Ver
    Excel 2010, Excel 2013
    Posts
    366

    Faster Way to Substract 2 Columns?

    Range("M2").Select
        Range(Selection, Selection.End(xlDown)).Select
        
        Dim CEL As Range
         
        For Each CEL In Selection
        If CEL.Value <> "" Then
            If Range("O" & CEL.Row).Value <> "" Then
            CEL.Value = CEL.Value - Range("O" & CEL.Row).Value
            End If
        End If
        Next CEL
        
        MsgBox ("Done")
    Is there a faster, better way?
    Thank You,

    EnigmaMatter

  2. #2
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,088

    Re: Faster Way to Substract 2 Columns?

    Perhaps

    Sub TestPCI()
    Dim WkRg As Range
    Dim WkTb1, WkTb2
    Dim LastRow As Long
    Dim I  As Long
    
        LastRow = Range("M" & Rows.Count).End(xlUp).Row
        Set WkRg = Range("M2:M" & LastRow)
        WkTb1 = WkRg
        Set WkRg = Range("O2:O" & LastRow)
        WkTb2 = WkRg
        For I = 1 To UBound(WkTb1)
            If ((WkTb1(I, 1) <> 0) And (WkTb2(I, 1) <> 0)) Then WkTb1(I, 1) = WkTb1(I, 1) - WkTb2(I, 1)
        Next
        Range("M2").Resize(UBound(WkTb1, 1), 1) = WkTb1
    
    End Sub
    - Battle without fear gives no glory - Just try

  3. #3
    Forum Contributor
    Join Date
    02-04-2014
    Location
    Florida, United States
    MS-Off Ver
    Excel 2010, Excel 2013
    Posts
    366

    Re: Faster Way to Substract 2 Columns?

    Curiosity, why does this work so much better than my script?

  4. #4
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,088

    Re: Faster Way to Substract 2 Columns?

    Because access to the spreadsheet is limited to the minimum:
    All data is read and put in memory in one shot: Wktb1 = WkRg, WkTb2 = WkRg
    Treatment is done in memory
    Complete result is written in one shot in the cells
    Range("M2").Resize(UBound(WkTb1, 1), 1) = WkTb1

  5. #5
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Faster Way to Substract 2 Columns?

    try
    Sub test()
        With Range("m2", Range("m" & Rows.Count).End(xlUp))
            .Value = Evaluate(.Address & "-o2:o" & .Cells(.Count).Row)
        End With
    End Sub

  6. #6
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Faster Way to Substract 2 Columns?

    Mr. Jindon What if I want the results to be in Q2:Q & LastRow?

  7. #7
    Forum Contributor
    Join Date
    02-04-2014
    Location
    Florida, United States
    MS-Off Ver
    Excel 2010, Excel 2013
    Posts
    366

    Re: Faster Way to Substract 2 Columns?

    PCI,

    It bugs on:

    
    WkTb1(I, 1) = WkTb1(I, 1) - WkTb2(I, 1)
    in line:

    If ((WkTb1(I, 1) <> 0) And (WkTb2(I, 1) <> 0)) Then WkTb1(I, 1) = WkTb1(I, 1) - WkTb2(I, 1)

  8. #8
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,088

    Re: Faster Way to Substract 2 Columns?

    Can you send a short sample of your data, with mine it was OK
    Perhaps try depending of your data
    If ((WkTb1(I, 1) <> "") And (WkTb2(I, 1) <> "")) Then WkTb1(I, 1) = WkTb1(I, 1) - WkTb2(I, 1)
    Attached Files Attached Files
    Last edited by PCI; 01-21-2015 at 04:58 PM.

  9. #9
    Forum Contributor
    Join Date
    02-04-2014
    Location
    Florida, United States
    MS-Off Ver
    Excel 2010, Excel 2013
    Posts
    366

    Re: Faster Way to Substract 2 Columns?

    It looks identical except it is in a table...but yours works in a table too...

  10. #10
    Forum Contributor
    Join Date
    02-04-2014
    Location
    Florida, United States
    MS-Off Ver
    Excel 2010, Excel 2013
    Posts
    366

    Re: Faster Way to Substract 2 Columns?

    PCI,

    Your substitute line worked! Thanks!

    Why did it work?

  11. #11
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,088

    Re: Faster Way to Substract 2 Columns?

    I have the "feeling" there is not only numbers in your data, that's why it is always better to have a sample ...!

  12. #12
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Faster Way to Substract 2 Columns?

    Mr. Jindon What if I want the results to be in Q2:Q & LastRow?
    Thanks advanced

  13. #13
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Faster Way to Substract 2 Columns?

        With Range("m2", Range("m" & Rows.Count).End(xlUp))
            Range("q2:q" & .Cells(.Count).Row).Value = Evaluate(.Address & "-o2:o" & .Cells(.Count).Row)
        End With

  14. #14
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Faster Way to Substract 2 Columns?

    Thanks a lot Mr. Jindon

+ 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. Faster way to COUNTIF across multiple columns for datesets over 100,000 rows
    By JP Romano in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-15-2014, 05:44 PM
  2. Replies: 12
    Last Post: 01-30-2014, 01:33 AM
  3. [SOLVED] Faster way to copy and paste multiple columns in Sheet1 to corresponding columns in Sheet2
    By babbeleme1 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 06-19-2013, 12:55 AM
  4. [SOLVED] How do I substract numbers in range like this: Substract(a1..a10)
    By Paulo in forum Excel - New Users/Basics
    Replies: 3
    Last Post: 11-17-2005, 12:50 AM
  5. [SOLVED] Can faster CPU+larger/faster RAM significantly speed up recalulati
    By jmk_li in forum Excel General
    Replies: 2
    Last Post: 09-28-2005, 06:05 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