+ Reply to Thread
Results 1 to 5 of 5

Macro which sorts and matches entries not working but not erroring

  1. #1
    Registered User
    Join Date
    01-13-2020
    Location
    Christchurch, New Zealand
    MS-Off Ver
    2019
    Posts
    8

    Macro which sorts and matches entries not working but not erroring

    Hi all,

    I've done some programming in Python and R but this is my first foray into VBA so my apologies if any of this turns out to be an absurdly stupid question!

    I'm trying to write some VBA code which will do the following two tasks.

    1: Sorts a range of data by Name and then by Absolute Value

    2: Looks through the sorted data, and if two rows have the same Name and Values which sum to zero, deletes both rows.

    As it stands, the code I've written works some of the time, but not all of the time. I've attached a spreadsheet with examples of where it works and where it fails. The code behaves as expected for "Anna" and "Tom" but fails for Jack. It should delete both entries as the names are the same and the values are the same.

    Any advice would be greatly appreciated.

    The code is as follows (a text box is assigned to the first piece of code (Sort_And_Match):

    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    Last edited by Nick_G; 02-03-2020 at 03:35 PM.

  2. #2
    Forum Expert JLGWhiz's Avatar
    Join Date
    02-20-2011
    Location
    Florida, USA
    MS-Off Ver
    Windows 10, Excel 2013
    Posts
    2,070

    Re: Macro which sorts and matches entries not working but not erroring

    The parameters for the Rng variable were set up incorrectly for proper use in the For Each statement.

    Please Login or Register  to view this content.
    Also, your 'Matching' procedure needed some work.
    Please Login or Register  to view this content.
    To troubleshoot a process using multiple macros, open the vb editor to the primary code module (starting macro) and then use the F8 key to step through the procedure and see where your problems are.
    Last edited by JLGWhiz; 01-22-2020 at 05:30 PM.
    Any code provided by me should be tested on a copy or a mock up of your original data before applying it to the original. Some events in VBA cannot be reversed with the undo facility in Excel. If your original post is satisfied, please mark the thread as "Solved". To upload a file, see the banner at top of this page.
    Just when I think I am smart, I learn something new!

  3. #3
    Registered User
    Join Date
    01-13-2020
    Location
    Christchurch, New Zealand
    MS-Off Ver
    2019
    Posts
    8

    Re: Macro which sorts and matches entries not working but not erroring

    Firstly - thank you very much. That works perfectly and is exactly what I expected.

    I have a couple of questions about the changes you made to the Matching procedure.

    Please Login or Register  to view this content.
    This starts at the end and works backwards until it stops at Row 3 (2 if you consider RowNumberPlus, which is really RowNumberMinus with working backwards)

    Why do you do it this way?

    Also why the manual update of
    Please Login or Register  to view this content.
    ? Given
    Please Login or Register  to view this content.
    , wouldn't this happen automatically?

    Once again, it works perfectly and I'm very grateful! Just trying to figure out "why"

  4. #4
    Forum Expert JLGWhiz's Avatar
    Join Date
    02-20-2011
    Location
    Florida, USA
    MS-Off Ver
    Windows 10, Excel 2013
    Posts
    2,070

    Re: Macro which sorts and matches entries not working but not erroring

    When deleting rows, starting from the bottom assures that rows will not be skipped because the loop automatically increments to the next row on each iteration. Since Excel shifts the rows as rows are deleted, If the loop begins at the top, then the shifted row will be skipped because the automatic increment of the loop looks at the next row and ignores the row that was shifted into the deleted position. By starting at the bottom, that part of the problem goes away. The deleted row will be below the next increment.

    The' i = i - 1 ' statement is to compensate for the two row deletion and give i a value so the autm increment will be for the correct row.

    The Step -1 simply tells VBA how to increment the loop upward.
    Regards, JLG

  5. #5
    Registered User
    Join Date
    01-13-2020
    Location
    Christchurch, New Zealand
    MS-Off Ver
    2019
    Posts
    8

    Re: Macro which sorts and matches entries not working but not erroring

    Many thanks! Will mark thread as solved and keep this in my pocket if I'm asked to dip my toes into VBA again.

+ 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. Using a macro to filter text data with a wildcard in a pivot table erroring out
    By Oy-a-Billy-Bumbler in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-30-2019, 08:22 AM
  2. [SOLVED] Macro erroring out on worksheet name
    By RSa in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-10-2016, 05:03 PM
  3. [SOLVED] VBA Macro erroring out with example code / document
    By Hyflex in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-25-2014, 05:21 AM
  4. Password and case statements codes not working, but not erroring either
    By pjbassdc in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-09-2013, 09:32 AM
  5. Designing a macro that takes data, sorts it and then identifies unique entries
    By gcXL in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-27-2012, 02:42 PM
  6. Print macro erroring if there are hidden sheets
    By davidjoe in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-03-2012, 03:51 PM
  7. Basic Macro (Jump to Cell) erroring out
    By kethyar in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-12-2008, 06:54 PM

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