Results 1 to 7 of 7

Thread: Subtraction Of Series Of Cells' / Array Values

  1. #1
    Senior Member
    Join Date
    Oct 2011
    Posts
    135
    Rep Power
    13

    Subtraction Of Series Of Cells' / Array Values

    Hi,

    I have a column with the following values​​:
    94,80
    94,10
    95,60
    91,90
    93,00
    92,50
    94,00
    95,60
    99,50
    98,50
    100,10
    100,70

    I'm using the following code
    Code:
    Option Explicit
    
    Sub CTest()
        Dim r   As Long
        Dim ka, k()
        Dim i   As Long
        
        r = Range("C" & Rows.Count).End(3).Row + 1
        
        ka = Range("C3:C" & r)
        
        ReDim k(1 To UBound(ka, 1), 1 To 1)
        
        For i = 1 To UBound(ka, 1) - 1
            k(i, 1) = ka(i + 1, 1) - ka(i, 1)
        Next
    
        Range("O3").Resize(UBound(k, 1)) = k
    End Sub
    The data obtained are correct.

    1 - Wonder if the procedure is properly optimized.

    2 - If I use: r = Range("C" & Rows.Count).End(3).Row
    Wonder how it must be interpreted

    Thanks in advance

  2. #2
    Administrator Admin's Avatar
    Join Date
    Mar 2011
    Posts
    1,122
    Rep Power
    10
    Hi

    Working on array is always faster. The End(3) is equivalent to End(xlUp)



    https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA
    https://www.youtube.com/watch?v=UywjKEMjSp0&lc=UgxIySxHPqM1RxtVqoR4AaABAg. 9edGvmwOLq99eekDyfS0CD
    https://www.youtube.com/watch?v=UywjKEMjSp0&lc=UgxIySxHPqM1RxtVqoR4AaABAg. 9edGvmwOLq99eevG7txd2c
    https://www.youtube.com/watch?v=SIDLFRkUEIo&lc=UgzTF5vvB67Zbfs9qvx4AaABAg
    https://www.youtube.com/watch?v=9P6r7DLS77Q&lc=UgzytUUVRyw9U55-6M54AaABAg
    https://www.youtube.com/watch?v=9P6r7DLS77Q&lc=UgzCoa6tOVIBxRDDDbN4AaABAg
    https://www.youtube.com/watch?v=9P6r7DLS77Q&lc=UgyriWOelbVnw4FHWT54AaABAg. 9dPo-OdLmZ09dc21kigjmr
    https://www.youtube.com/watch?v=363wd2EtQZ0&lc=UgzDQfo5rJqyVwvv2r54AaABAg
    https://www.youtube.com/watch?v=363wd2EtQZ0&lc=UgzHTSka7YppBdmUooV4AaABAg. 9cXui6zzkz09cZttH_-2Gf
    https://www.youtube.com/watch?v=tPRv-ATUBe4&lc=UgxhXnQ-mWYhrHWuM354AaABAg.9bepnegjnRu9iMmBDtf4m1
    https://www.youtube.com/watch?v=tPRv-ATUBe4&lc=UgxFIZ858qf7w_uA9bd4AaABAg.9dKpEpUk3YT9d VEGnka6yj
    https://www.youtube.com/watch?v=tPRv-ATUBe4&lc=Ugz8oC8iGd6-SPhpaQZ4AaABAg.9bhRt-kPXri9brzh_99JF9
    https://www.youtube.com/watch?v=tPRv-ATUBe4&lc=Ugz8oC8iGd6-SPhpaQZ4AaABAg.9bhRt-kPXri9bsrQIgXb3L
    https://www.youtube.com/watch?v=tPRv-ATUBe4&lc=UgzFkoI0n_BxwnwVMcZ4AaABAg
    https://www.youtube.com/watch?v=tPRv-ATUBe4&lc=UgxwJDkFskrMW8EpcXt4AaABAg.9bmKMz5-Z1g9bmx0REIz41
    https://www.youtube.com/watch?v=tPRv-ATUBe4&lc=UgxhXnQ-mWYhrHWuM354AaABAg.9bepnegjnRu9bmyko2YUvQ
    https://www.youtube.com/watch?v=tPRv-ATUBe4&lc=UgxwJDkFskrMW8EpcXt4AaABAg.9bmKMz5-Z1g9bmzpPqfLRD
    https://www.youtube.com/watch?v=tPRv-ATUBe4&lc=UgzZwbV_Y_7UFzHwNBh4AaABAg.9dKb0Vc7MOB9d VK8si3ont
    https://www.youtube.com/watch?v=tPRv-ATUBe4&lc=Ugx6Ec_r4kb9EYOVgIt4AaABAg.9dOW613fb8V9d VIJECZIdC
    https://www.youtube.com/watch?v=tPRv-ATUBe4&lc=UgwBho9tBLQ4nPVdYqd4AaABAg.9fWvoBWY3Da9g 9cLjhPiaz
    https://www.youtube.com/watch?v=tPRv-ATUBe4&lc=UgzZy1NAMBx5Uv4U2cJ4AaABAg.9f0XX-_JaGp9g9bYLMZiIy
    https://www.youtube.com/watch?v=tPRv-ATUBe4&lc=UgyL-xp8IiiahmQ12kJ4AaABAg.9f7xHCpAEx29g9asFhVFfT
    https://www.youtube.com/watch?v=tPRv-ATUBe4&lc=UgxRxyFNNp3WHTzuiJJ4AaABAg.9fFR6ECmXk69g 9afNBcS4Z
    https://www.youtube.com/watch?v=tPRv-ATUBe4&lc=UgwsdMh0FGDfvA249_B4AaABAg.9fLR6FHCIVI9g 9aLlUyzog
    https://www.youtube.com/watch?v=tPRv-ATUBe4&lc=UgwBho9tBLQ4nPVdYqd4AaABAg.9fWvoBWY3Da9g 9_4422NzK
    https://www.youtube.com/watch?v=tPRv-ATUBe4&lc=Ugwyy8JXr56HJ8m_od94AaABAg.9gSFgqqJQNV9g TXco41b5l
    https://www.youtube.com/watch?v=tPRv-ATUBe4&lc=UgyS8stMz5B9NrpPrbR4AaABAg.9gOjiS0rs8l9g TYl6RldpA
    https://www.youtube.com/watch?v=tPRv-ATUBe4&lc=UgyS8stMz5B9NrpPrbR4AaABAg.9gOjiS0rs8l9g TfhAWU9ju
    https://www.youtube.com/watch?v=tPRv-ATUBe4&lc=UgyS8stMz5B9NrpPrbR4AaABAg.9gOjiS0rs8l9g TfuYQGmUa
    https://www.youtube.com/watch?v=tPRv-ATUBe4&lc=UgyS8stMz5B9NrpPrbR4AaABAg.9gOjiS0rs8l9g Tg3AmMPUc
    https://www.youtube.com/watch?v=tPRv-ATUBe4&lc=UgyS8stMz5B9NrpPrbR4AaABAg.9gOjiS0rs8l9g TgEqh5wdo
    https://www.youtube.com/watch?v=tPRv-ATUBe4&lc=UgxmUK0S_aZVZWz8-gt4AaABAg.9gLc3DfWfHl9gTZ3y6fL1H
    https://www.youtube.com/watch?v=tPRv-ATUBe4&lc=UgzZloYeY2wQr7-xTOh4AaABAg.9gB2bbbs9mB9gTZUkNYI8e
    https://www.youtube.com/watch?v=tPRv-ATUBe4&lc=UgzlM96nGEhW9J1Gpgd4AaABAg.9fmOFVcXZh49g T_8CYeQgz
    https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA
    Last edited by DocAElstein; 09-22-2023 at 05:29 PM.
    Cheers !

    Excel Range to BBCode Table
    Use Social Networking Tools If You Like the Answers !

    Message to Cross Posters

    @ Home - Office 2010/2013/2016 on Win 10 (64 bit); @ Work - Office 2016 on Win 10 (64 bit)

  3. #3
    Forum Guru Rick Rothstein's Avatar
    Join Date
    Feb 2012
    Posts
    659
    Rep Power
    13
    Quote Originally Posted by PcMax View Post
    I'm using the following code
    Code:
    Option Explicit
    
    Sub CTest()
        Dim r   As Long
        Dim ka, k()
     Dim i   As Long
        
        r = Range("C" & Rows.Count).End(3).Row + 1
        
        ka = Range("C3:C" & r)
        
        ReDim k(1 To UBound(ka, 1), 1 To 1)
     
        For i = 1 To UBound(ka, 1) - 1
            k(i, 1) = ka(i + 1, 1) - ka(i, 1)
        Next
    
        Range("O3").Resize(UBound(k, 1)) = k
    End Sub
    I doubt if the time difference is measurable, but given what you are doing, you do not need the k array... just do your calculations back into the ka array itself...
    Code:
    Sub CTest()
        Dim r   As Long
        Dim ka
        Dim i   As Long
        
        r = Range("C" & Rows.Count).End(3).Row + 1
        
        ka = Range("C3:C" & r)
        
        For i = 1 To UBound(ka, 1) - 1
            ka(i, 1) = ka(i + 1, 1) - ka(i, 1)
        Next
        Range("O3").Resize(UBound(ka, 1)) = ka
    End Sub
    Last edited by Rick Rothstein; 10-26-2012 at 11:46 PM.

  4. #4
    Senior Member
    Join Date
    Oct 2011
    Posts
    135
    Rep Power
    13
    Hi,

    Thanks to the alternative code, useful to reduce the memory used in the cycle.

    To avoid errors when there is a text, I added in the cycle:

    For i = 1 To UBound(ka, 1) - 1
    ka(i, 1) = Val(ka(i + 1, 1)) - Val(ka(i, 1))
    Next

    Is there an alternative to load the array: ka = Range("C3:C" & r) with only numeric values ?

    I hope my request makes sense

  5. #5
    Forum Guru Rick Rothstein's Avatar
    Join Date
    Feb 2012
    Posts
    659
    Rep Power
    13
    Quote Originally Posted by PcMax View Post
    To avoid errors when there is a text, I added in the cycle:

    For i = 1 To UBound(ka, 1) - 1
    ka(i, 1) = Val(ka(i + 1, 1)) - Val(ka(i, 1))
    Next

    Is there an alternative to load the array: ka = Range("C3:C" & r) with only numeric values ?
    Just double-checking... you are performing a mathematical operation on a column of cells that contain both numbers and non-numbers??? If so, can you show us an example of data in Column C that is like that and also show us the results you want to see from it?

  6. #6
    Senior Member
    Join Date
    Oct 2011
    Posts
    135
    Rep Power
    13
    Hi,

    I have an Excel spreadsheet in the column are expected values ​​only.
    There is however the possibility that the macro is interrupted due to a single value text.
    Which solution is indicated I enter a:
    Code:
    On Error Resume Next

  7. #7
    Forum Guru Rick Rothstein's Avatar
    Join Date
    Feb 2012
    Posts
    659
    Rep Power
    13
    Quote Originally Posted by PcMax View Post
    I have an Excel spreadsheet in the column are expected values ​​only.
    There is however the possibility that the macro is interrupted due to a single value text.
    Which solution is indicated I enter a:
    Code:
    On Error Resume Next
    Straightening out your data so that non-numeric text values do not appear in Column C would be the best course of action but, assuming that is not possible for some reason, then I would go with the On Error method instead of the Val method only because the text values are retained and should stand out (they will be left-justified in the cell) in and among the numerical output (which is right-justified)... the Val method would produce incorrect numerical results for the text and be hard to spot given normal looking numbers would be output with it. One thing to note with either method... the value in the cell located in the row above the text cell's output will be incorrect.
    Last edited by Rick Rothstein; 10-26-2012 at 11:57 PM.

Similar Threads

  1. Replies: 4
    Last Post: 06-10-2013, 01:27 PM
  2. Replies: 1
    Last Post: 12-04-2012, 08:56 AM
  3. counting consecutive values in an array
    By 5ko in forum Excel Help
    Replies: 3
    Last Post: 12-04-2012, 03:49 AM
  4. Replies: 3
    Last Post: 08-05-2012, 09:16 PM
  5. Replies: 3
    Last Post: 04-08-2012, 09:44 AM

Posting Permissions

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