Results 1 to 5 of 5

Macro which sorts and matches entries not working but not erroring

Threaded View

Nick_G Macro which sorts and matches... 01-22-2020, 04:24 PM
JLGWhiz Re: Macro which sorts and... 01-22-2020, 05:27 PM
Nick_G Re: Macro which sorts and... 01-22-2020, 06:51 PM
JLGWhiz Re: Macro which sorts and... 01-23-2020, 09:26 AM
Nick_G Re: Macro which sorts and... 01-23-2020, 02:57 PM
  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):

    Sub Sort_And_Match()
    
    Application.ScreenUpdating = False
    
    
    counter = 0
    
    SortMultipleColumns
    Matching
    
    If counter = 1 Then
        SortMultipleColumns
        Matching
        
    End If
    
    
        
    Application.ScreenUpdating = True
    
    End Sub
    Sub Matching()
    
    Application.ScreenUpdating = False
    
    Numrows = Range("A1", Range("A1").End(xlDown)).Rows.Count
    
    Set Rng = Range("A2:A" & Numrows)
    
    
    For Each Cell In Rng
        RowNumber = Cell.Row
        RowNumberPlus = RowNumber + 1
        
        If Range("B" & RowNumber) = -Range("B" & RowNumberPlus) Then
        
            If Range("A" & RowNumber) = Range("A" & RowNumberPlus) Then
                Range("A" & RowNumber).Resize(2).EntireRow.Delete
                counter = 1
                
                
                
            End If
        End If
        
        Next Cell
    
    
    End Sub
    Sub SortMultipleColumns()
    
    Numrows = Range("A1", Range("A1").End(xlDown)).Rows.Count
    Set Rng = Range("A2:B" & Numrows)
    
    Columns("C").EntireColumn.Insert
    
    
    For Each Cell In Rng
        RowNumber = Cell.Row
        
        Range("C" & RowNumber).Value = Abs(Range("B" & RowNumber))
        
           
        Next Cell
    
    
    With ActiveSheet.Sort
        .SortFields.Clear
        .SortFields.Add Key:=Range("A1"), Order:=xlAscending
        .SortFields.Add Key:=Range("C1"), Order:=xlAscending
        .SetRange Range("A1:C" & Numrows)
        .Header = xlYes
        .Apply
        
        Range("C" & RowNumber).EntireColumn.Delete
    
    End With
    End Sub
    Last edited by Nick_G; 02-03-2020 at 03:35 PM.

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