+ Reply to Thread
Results 1 to 12 of 12

Change cell.value quicker

  1. #1
    Forum Contributor
    Join Date
    04-07-2009
    Location
    Tasmania, Australia
    MS-Off Ver
    Excel 2003
    Posts
    158

    Change cell.value quicker

    I need to change the cell values in a column if they are more than 7 or less than -7. I've got three lots of code which work, but are very slow. Two take more than 5 minutes to run and the following takes 1.5 minutes. There is 1300 rows in my test worksheet, but in practice there will be 4-5 times that.

    Is there a quicker way please?

    Please Login or Register  to view this content.
    Steve W.
    Vba is my hobby, racing the means.

  2. #2
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,168

    Re: Change cell.value quicker

    Hi swallis,

    The median function in excel is what you seek. No VBA needed.

    =MEDIAN(-7,A2,7)

    See the attached

    Median betwean 7s.xlsx
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  3. #3
    Forum Expert dflak's Avatar
    Join Date
    11-24-2015
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    7,920

    Re: Change cell.value quicker

    Looping takes a lot of time. Copying takes a lot less time. This code assumes that there is data in columns A-C and that there is a filter on the range.

    Please Login or Register  to view this content.
    Attached Files Attached Files
    One spreadsheet to rule them all. One spreadsheet to find them. One spreadsheet to bring them all and at corporate, bind them.

    A picture is worth a thousand words, but a sample spreadsheet is more likely to be worked on.

  4. #4
    Forum Contributor
    Join Date
    04-07-2009
    Location
    Tasmania, Australia
    MS-Off Ver
    Excel 2003
    Posts
    158

    Re: Change cell.value quicker

    Quote Originally Posted by MarvinP View Post
    The median function in excel is what you seek. No VBA needed.
    Thanks Marvin. I need vba as it's part of a large procedure. I've managed to put the following code together, which works in 2 seconds.

    A couple of questions:

    1 - I've had to use a helper column, but would prefer to avoid that if possible - is it and if so, how?

    2 - The formula works perfectly, but I have no clue why. If you have time, could you explain it please?

    Please Login or Register  to view this content.
    Thanks very much for your help

    Steve

  5. #5
    Forum Contributor
    Join Date
    04-07-2009
    Location
    Tasmania, Australia
    MS-Off Ver
    Excel 2003
    Posts
    158

    Re: Change cell.value quicker

    Quote Originally Posted by dflak View Post
    Looping takes a lot of time. Copying takes a lot less time.
    Thanks Dflak. Your code is a lot quicker at 10 seconds, but I've gone with Marvin's.

    Steve

  6. #6
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,168

    Re: Change cell.value quicker

    Hi Steve,

    The Median() function returns the Value of the Number in the middle. So the formula should be =Median(-7,D2,7). I needed to think hard about this a few years ago. If you are given Median(-7,20,7) then 20>7 so 7 is in the middle and it returns 7. If you are given Median(-7,5,7) then the 5 is the middle value and is returned. If you are given Median(-7,-12,7) then the -7 is in the middle and returned.

    Also the formula could be written as Median(D2,7,-7) or Median(7,-7,D2) as it uses the values of the arguments and not their position in the function.

    I hope that explains Median a bit better.

  7. #7
    Forum Contributor
    Join Date
    04-07-2009
    Location
    Tasmania, Australia
    MS-Off Ver
    Excel 2003
    Posts
    158

    Re: Change cell.value quicker

    Quote Originally Posted by MarvinP View Post
    I hope that explains Median a bit better.
    It certainly does. Thank you.

    Do you know if I can avoid using the helper column? or should I make that a new post?

    Steve

  8. #8
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,168

    Re: Change cell.value quicker

    If you don't use a helper column you will need to do the problem using VBA and overwrite each value. That would take longer than a helper, using the formula and then copy and paste the answer using Values Only.

  9. #9
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,612

    Re: Change cell.value quicker

    Please Login or Register  to view this content.
    Ben Van Johnson

  10. #10
    Forum Contributor
    Join Date
    04-07-2009
    Location
    Tasmania, Australia
    MS-Off Ver
    Excel 2003
    Posts
    158

    Re: Change cell.value quicker

    Thank Ben. That works perfectly and only takes 2 seconds.

    Much appreciated,

    Steve

  11. #11
    Forum Contributor
    Join Date
    04-07-2009
    Location
    Tasmania, Australia
    MS-Off Ver
    Excel 2003
    Posts
    158

    Re: Change cell.value quicker

    Quote Originally Posted by MarvinP View Post
    If you don't use a helper column you will need to do the problem using VBA and overwrite each value.
    Thanks Marvin. That's what I want to do. Protonleah's solution does it nicely and still only 2 seconds.

    Thanks a lot once again for you help. I'll mark it solved.

    Regards,

    Steve

  12. #12
    Forum Expert dflak's Avatar
    Join Date
    11-24-2015
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    7,920

    Re: Change cell.value quicker

    I like the MEDIAN approach. It's a completely different box than I was thinking in.

+ 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 backspace a text in a cell quicker
    By SimonLee in forum Excel General
    Replies: 39
    Last Post: 09-11-2016, 06:29 AM
  2. Can the following Vba's be improved to run quicker.
    By Toonies in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 10-11-2012, 06:54 AM
  3. Is there a quicker way to do this
    By jonn in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-26-2008, 11:12 AM
  4. Quicker way?
    By Craiig in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 06-22-2008, 07:32 AM
  5. Quicker VBA
    By sparx in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-10-2007, 06:47 AM
  6. Cell problems - need a quicker way.
    By lara5555 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 01-13-2006, 09:00 AM
  7. Check if cell contains certain text-Is sumproduct or sum quicker
    By jhockstr in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 08-25-2005, 04:32 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