+ Reply to Thread
Results 1 to 7 of 7

VBA in change event format to auto remove rows having zero value and auto sort

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    02-19-2013
    Location
    UK
    MS-Off Ver
    Excel 2019
    Posts
    677

    Question VBA in change event format to auto remove rows having zero value and auto sort

    Hi,
    I have sheet2 which is taking a direct linked data from sheet1

    What I want to achieve is as follows:

    First delete rows based on values in certain range of cells.
    The VBA i use is as follows with a form-control button:

    Option Explicit
    Sub DeleteRows()
    Dim ChkRange As Range
    Set ChkRange = Range("C4:C188")
    Dim cell As Range
    For Each cell In ChkRange
        If cell = "0" Then
            cell.EntireRow.Delete
        End If
    Next
    End Sub
    secondly I also want to sort all rows based on values ( time) in cells F4:F188 ( lowest value first .....)
    However the first action has removed certain number of rows having 'Zero' value.
    I want it also to ignore any blank cells while sorting so that they do not interfere.
    I can manually do this as well.

    I need to combine both above in kind of change event, where any updation of data on sheet2 ( which is linked) will trigger change event to delete rows having Zero value and sort them.

    I have tried various methods, but no success.

    Any help please..
    Many thanks
    regards

  2. #2
    Forum Contributor
    Join Date
    02-19-2013
    Location
    UK
    MS-Off Ver
    Excel 2019
    Posts
    677

    Re: VBA in change event format to auto remove rows having zero value and auto sort

    Hi,
    PS: Want to sort out first and then delete rows having Zeros.
    If I reverse the order the zeros return.

    Many thanks

  3. #3
    Forum Contributor
    Join Date
    02-19-2013
    Location
    UK
    MS-Off Ver
    Excel 2019
    Posts
    677

    Question Re: VBA in change event format to auto remove rows having zero value and auto sort

    Hi,
    PS: Want to sort out first and then delete rows having Zeros.
    If I reverse the order the zeros return.

    Many thanks

  4. #4
    Forum Contributor
    Join Date
    02-19-2013
    Location
    UK
    MS-Off Ver
    Excel 2019
    Posts
    677

    Re: VBA in change event format to auto remove rows having zero value and auto sort

    Hi Experts,
    Any Help yet??
    Thanks Regards

  5. #5
    Forum Contributor
    Join Date
    02-19-2013
    Location
    UK
    MS-Off Ver
    Excel 2019
    Posts
    677

    Re: VBA in change event format to auto remove rows having zero value and auto sort

    Hi Experts,

    I have figured it out and have codes for Sorting and to hide rows having zero value.

    I now need help in combining both codes please..... tried a lot but it will not work....

    First auto sort..... It works fine....

    ActiveWorkbook.Worksheets("Sheet2").Sort.SortFields.Add Key:=Range("C4:C20") _
            , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        
        With ActiveWorkbook.Worksheets("Sheet2").Sort
            
            .SetRange Range("A3:K20")
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        
        End With
    
    
    End Sub
    To hide rows having Zero value..... It works fine...
    Sub Hide_rows()
    Dim LastRow As Long
    Dim Rng As Range
    LastRow = Range("A3:K190").End(xlUp).Row '
    Set Rng = Range("A3:K190" & LastRow) '
    Application.ScreenUpdating = False
    For Each cell In Rng
    If cell.Value = "0" Then
    cell.EntireRow.Hidden = True
    End If
    Next cell
    Application.ScreenUpdating = True
    End Sub

    Many thanks in advance.....

    Regards

  6. #6
    Forum Contributor
    Join Date
    02-19-2013
    Location
    UK
    MS-Off Ver
    Excel 2019
    Posts
    677

    Re: VBA in change event format to auto remove rows having zero value and auto sort

    Hi all,
    Apology. The first code was incomplete. Here is the complete one.

    Any help yet please from any one to incorporate hide code in change event . Preferably if that can delete the rows instead of hiding please..

    Regards

    Private Sub Worksheet_Change(ByVal Target As Range)
        ActiveWorkbook.Worksheets("Sheet2").Sort.SortFields.Clear
        ActiveWorkbook.Worksheets("Sheet2").Sort.SortFields.Add Key:=Range("C4:C20") _
            , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
            With ActiveWorkbook.Worksheets("Sheet2").Sort
            
            .SetRange Range("A3:K20")
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
            End With
    End Sub

  7. #7
    Forum Contributor
    Join Date
    02-19-2013
    Location
    UK
    MS-Off Ver
    Excel 2019
    Posts
    677

    Re: VBA in change event format to auto remove rows having zero value and auto sort

    Hi
    I am now closing this one as had no luck on it. I have adapted a different approach to it now.
    Many thanks friends

+ 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. Auto sort macro for ScrollBar event
    By taimaishu001 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-19-2012, 01:54 AM
  2. Auto sort macro for spin button event
    By mantooth29 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-05-2011, 02:58 PM
  3. Remove Duplicates from 2 columns then auto sort
    By Dsnellin in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-24-2011, 03:09 PM
  4. Auto Sort, Auto Lock, Auto Date & Time Stamp
    By suehatesyou in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-23-2010, 02:57 PM
  5. auto sort by date event macro
    By SMUGGLINGSTONES in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-13-2008, 01:04 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