+ Reply to Thread
Results 1 to 15 of 15

Finding the same value from a variable and selecting all these rows within a range

  1. #1
    Registered User
    Join Date
    08-04-2022
    Location
    Blackburn
    MS-Off Ver
    Excel 2013
    Posts
    39

    Finding the same value from a variable and selecting all these rows within a range

    Hey so I'm looking for a generic code. Basically, I need it to find the value in a column using a calculation already named on the code so basically it would be something like .Value = Calc. I need it to find all cells in a column with this value (should only be between 1 and 20 cells in total) and then I need it to select the cells and delete them and shift up. Cheers.

    I've managed to get it to delete one row but not all of them, the code I used is

    Please Login or Register  to view this content.

  2. #2
    Forum Expert torachan's Avatar
    Join Date
    12-27-2012
    Location
    market harborough, england
    MS-Off Ver
    Excel 2010
    Posts
    4,307

    Re: Finding the same value from a variable and selecting all these rows within a range

    have you tried looping from the bottom upwards (step -1)
    Torachan,

    Mission statement; Promote the use of Tables, Outlaw the use of 'merged cells' and 'RowSource'.

  3. #3
    Registered User
    Join Date
    08-04-2022
    Location
    Blackburn
    MS-Off Ver
    Excel 2013
    Posts
    39

    Re: Finding the same value from a variable and selecting all these rows within a range

    Quote Originally Posted by torachan View Post
    have you tried looping from the bottom upwards (step -1)
    Cheers for your reply. No luck though. It doesn't delete any rows. I'll leave code incase I've written it wrong.

    Please Login or Register  to view this content.
    I just used delete to test first and see if cell is deleted. I'll add range once it's deleted multiple values from the column.

  4. #4
    Forum Expert torachan's Avatar
    Join Date
    12-27-2012
    Location
    market harborough, england
    MS-Off Ver
    Excel 2010
    Posts
    4,307

    Re: Finding the same value from a variable and selecting all these rows within a range

    Please Login or Register  to view this content.
    This does not make sense - what is it finding.

    put the word Stop in the code immediately after the above code and see what the value of nextTrade is.

  5. #5
    Valued Forum Contributor
    Join Date
    08-08-2022
    Location
    Buenos Aires
    MS-Off Ver
    Excel 2019
    Posts
    1,777

    Re: Finding the same value from a variable and selecting all these rows within a range

    Hi. I'll show you one way to do it:

    Please Login or Register  to view this content.
    Of course you will replace Select with Delete.
    Last edited by beyond Excel; 08-22-2022 at 10:08 AM.

  6. #6
    Valued Forum Contributor
    Join Date
    05-03-2022
    Location
    Halifax,Canada
    MS-Off Ver
    365
    Posts
    326

    Re: Finding the same value from a variable and selecting all these rows within a range

    This does not make sense - what is it finding.

    put the word Stop in the code immediately after the above code and see what the value of nextTrade is.
    Hi Torachan:
    I did ... and it makes sense to me ... it provides the "nextTrade" number from looking at the incremented trade numbers in the list .
    (IE. Get the last trade number in list and increment it by 1 )

    ... I kind of like it ... think I will use this in the future .
    I also like the name Ija gave the variable as it clearly indicates the "intent" of the variable as it is literally the "Next Trade" in the list.
    So many of us give variables names that don't follow the practice of variable names that provide intent. I know i'm guilty of it on occasion .
    (boy will I feel foolish if that's not what Ija was doing LOL)

    ScrShot16.gif

    ScrShot17.gif

    Please Login or Register  to view this content.
    Last edited by nimrod1313; 08-22-2022 at 11:23 AM.

  7. #7
    Registered User
    Join Date
    08-04-2022
    Location
    Blackburn
    MS-Off Ver
    Excel 2013
    Posts
    39

    Re: Finding the same value from a variable and selecting all these rows within a range

    Quote Originally Posted by nimrod1313 View Post
    Hi ToraChan:
    I did ... and it makes sense to me ... it provides the "nextTrade" number from looking at the incremented trade numbers in the list .
    (IE. Get the last trade number in list and increment it by 1 )

    ... I kind of like it ... think I will use this in the future .
    I also like the name Ija gave the variable as it clearly indicates the "intent" of the variable as it is literally the "Next Trade" in the list.
    So many of us give variables names don't follow the practice of variable names that provide intent. I know i'm guilty of it on occasion .
    (boy will I feel foolish if that's not what Ija was doing LOL)

    Attachment 792822

    Attachment 792823

    Please Login or Register  to view this content.
    Thanks nimrod. That's exactly what it is for and yeah I like to try and keep it simple especially since the full vba code for this button is lengthy so I need to be able to clearly see what each section of code is doing.

  8. #8
    Valued Forum Contributor
    Join Date
    05-03-2022
    Location
    Halifax,Canada
    MS-Off Ver
    365
    Posts
    326

    Re: Finding the same value from a variable and selecting all these rows within a range

    Thanks nimrod. That's exactly what it is for
    Hi Ija34
    Well I like it !
    Off the top of my head I can't think of a cleaner/clearer way to accomplish this .

    Though , from a "style" standpoint I would have done it this way ..

    Please Login or Register  to view this content.
    Last edited by nimrod1313; 08-22-2022 at 11:14 AM.

  9. #9
    Valued Forum Contributor
    Join Date
    05-03-2022
    Location
    Halifax,Canada
    MS-Off Ver
    365
    Posts
    326

    Re: Finding the same value from a variable and selecting all these rows within a range

    since the full vba code for this button is lengthy so I need to be able to clearly see what each section of code is doing
    Hi Ija
    Well if you ever want some "peer review" on cleaning up code I would be interested .

  10. #10
    Registered User
    Join Date
    08-04-2022
    Location
    Blackburn
    MS-Off Ver
    Excel 2013
    Posts
    39

    Re: Finding the same value from a variable and selecting all these rows within a range

    Quote Originally Posted by beyond Excel View Post
    Hi. I'll show you one way to do it:

    Please Login or Register  to view this content.
    Of course you will replace Select with Delete.
    Hi Beyond Excel. This works perfectly, thank you. Massive help.

  11. #11
    Valued Forum Contributor
    Join Date
    05-03-2022
    Location
    Halifax,Canada
    MS-Off Ver
    365
    Posts
    326

    Re: Finding the same value from a variable and selecting all these rows within a range

    Hello Ija:

    Your original code was very close.
    If you're going to delete row by row you need to start from bottom and work up the sheet.
    (you'll notice torachan mentioned this in their first post)

    Reason :
    If you start from top you will miss rows .
    For example let's say you loop from top and have just deleted row 4 before going onto row 5.
    Well if you delete row 4 then row 5 becomes row 4 , but since you've already done row 4 the new row 4 is not evaluated.
    This issue goes away if you delete from bottom.
    NOTE : There are better ways to delete rows ... but I think you should know this process


    Starting from bottom row and working up (demo , not tested)
    Please Login or Register  to view this content.
    Last edited by nimrod1313; 08-22-2022 at 11:42 AM.

  12. #12
    Registered User
    Join Date
    08-04-2022
    Location
    Blackburn
    MS-Off Ver
    Excel 2013
    Posts
    39

    Re: Finding the same value from a variable and selecting all these rows within a range

    Quote Originally Posted by nimrod1313 View Post
    Hello Ija:

    Your original code was very close.
    If you're going to delete row by row you need to start from bottom and work up the sheet.
    (you'll notice torachan mentioned this in their first post)

    Reason :
    If you start from top you will miss rows .
    For example let's say you loop from top and have just deleted row 4 before going onto row 5.
    Well if you delete row 4 then row 5 becomes row 4 , but since you've already done row 4 the new row 4 is not evaluated.
    This issue goes away if you delete from bottom.
    NOTE : There are better ways to delete rows ... but I think you should know this process


    Starting from bottom row and working up (demo , not tested)
    Please Login or Register  to view this content.
    Ahhhh I see, that's where it went wrong. I tried this formula earlier but I put for i = 4 to lr step -1 so obviously I should have been going from last row to 4.

  13. #13
    Valued Forum Contributor
    Join Date
    05-03-2022
    Location
    Halifax,Canada
    MS-Off Ver
    365
    Posts
    326

    Re: Finding the same value from a variable and selecting all these rows within a range

    Ahhhh I see, that's where it went wrong. I tried this formula earlier but I put for i = 4 to lr step -1 so obviously I should have been going from last row to 4.
    Yes.
    But let's be clear, updating a sheet row by row (eg. deleting , inserting , clearing) has it's pitfalls.
    Primarily there is excel overhead to each change. This overhead takes time for each event/change. (ie. this approach is not "scalable" )
    There are many approaches to diminish this affect , these approaches fall primarily into two strategies.
    1) Update many rows/ranges at one time vs single row/range at a time
    2) turn off some of the applications overhead generating events during the running of the macro


    Lessen Application Overhead during macro run

    Please Login or Register  to view this content.
    WARNING : If your code fails/stops before getting to the lines that re-activate the application events then they will remain off.
    (ie. Have all your errors in code workout prior to this OR have some good error handling code in place.
    Last edited by nimrod1313; 08-22-2022 at 12:10 PM.

  14. #14
    Registered User
    Join Date
    08-04-2022
    Location
    Blackburn
    MS-Off Ver
    Excel 2013
    Posts
    39

    Re: Finding the same value from a variable and selecting all these rows within a range

    Quote Originally Posted by nimrod1313 View Post
    Yes.
    But let's be clear, updating a sheet row by row (eg. deleting , inserting , clearing) has it's pitfalls.
    Primarily there is excel overhead to each change. This overhead takes time for each event/change. (ie. this approach is not "scalable" )
    There are many approaches to diminish this affect , these approaches fall primarily into two strategies.
    1) Update many rows/ranges at one time vs single row/range at a time
    2) turn off some of the applications overhead generating events during the running of the macro


    Lessen Application Overhead during macro run

    Please Login or Register  to view this content.
    WARNING : If your code fails/stops before getting to the lines that re-activate the application events then they will remain off.
    (ie. Have all your errors in code workout prior to this OR have some good error handling code in place.
    So what does each of this bit of code do? My goal is to have a fully automated workbook that send info between accounts and for trades. I only need to enter event info then the rest of it is automated then at the end I'll have a full statement list of trades and an analyser that will calculate P & L and have graphs for profit over time. So the only manual things I need to do is data entry and the set up at the beginning. At the moment I'm trying to get each section working but since I'm not an expert at vba and this is my first time, I don't know how to write to make it run faster and efficiently so I'm just aiming to get it working then see if you guys can help me rewrite the code in a more efficient way.

  15. #15
    Valued Forum Contributor
    Join Date
    05-03-2022
    Location
    Halifax,Canada
    MS-Off Ver
    365
    Posts
    326

    Re: Finding the same value from a variable and selecting all these rows within a range

    I'm just aiming to get it working then see if you guys can help me rewrite the code in a more efficient way.
    Cool , good approach.
    I just trying to keep you up enough in understanding so that you could make informed decisions.
    Just having you understand that there are ways to make faster is enough for now , since you will revisit later.


    Please Login or Register  to view this content.
    Explaination:
    Each update of a cell, range , row excel recalculates all formulas and refreshes the screen.
    This all takes time... so many people turn these things off while looping and changing alot of cells, range, rows
    and then turn it back on after loop.

    ... but your right ... worry about this after you get everything working
    Last edited by nimrod1313; 08-22-2022 at 01:21 PM.

+ 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. VBA Selecting a variable range
    By JustinHook in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-28-2016, 02:26 PM
  2. Selecting A Variable Range
    By LSM1604 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-12-2010, 07:31 AM
  3. [SOLVED] Selecting a variable range
    By Bill in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-16-2006, 12:45 PM
  4. selecting Variable range
    By katmando in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-18-2006, 04:51 AM
  5. selecting rows using a variable.
    By cherrynich in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-02-2006, 03:30 PM
  6. selecting range of cells - variable # of rows
    By [email protected] in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 04-15-2005, 10:06 AM
  7. selecting a variable row range
    By Dan in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-05-2005, 08:06 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