+ Reply to Thread
Results 1 to 10 of 10

How can i delete or clear cells that exists before zero cell ?

Hybrid View

  1. #1
    Registered User
    Join Date
    01-17-2021
    Location
    Australia
    MS-Off Ver
    2019
    Posts
    25

    How can i delete or clear cells that exists before zero cell ?

    I need to delete cells that found before a cell that contains zero
    the attached excel contains an example that I have and I need it like this

    Screenshot from 2021-01-25 01-54-30.png

    is there any formula that I can use it by drag it down because I have large dataset
    Attached Files Attached Files

  2. #2
    Valued Forum Contributor
    Join Date
    01-16-2012
    Location
    England
    MS-Off Ver
    MS 365 Version 2501 64-bit
    Posts
    1,465

    Re: How can i delete or clear cells that exists bfore zero cell ?

    hassan,

    Excel formulae will not allow you to "tell" another cell to change. It can only change the cell containing the formula. You will have to use a Macro.

    Option Explicit
    Dim c As Long, f As Long, m As Long, r As Long
    
    Sub NOZEROES()
    
    'Find last row.
     
        With Sheet1
        f = .Cells(.Rows.Count, "A").End(xlUp).Row
        
        If f = 1 Then Exit Sub
        
    'Find last column
        r = .Cells(1, .Columns.Count).End(xlToLeft).Column
    
    Look in each row        
         For m = 2 To f
    
    Look in each column
            For c = 1 To r
    
    If the cell is Zero, then clear it and all cells above it in that column
                If .Cells(m, c) = 0 Then
                .Range(.Cells(1, c), .Cells(m, c)).ClearContents
                End If
                
    Move to next column and repeat
            Next
    
    Move to next row and repeat
        Next
        
        End With
        
        End Sub
    Ochimus
    Attached Files Attached Files
    Last edited by Ochimus; 01-24-2021 at 08:37 PM.

  3. #3
    Registered User
    Join Date
    01-17-2021
    Location
    Australia
    MS-Off Ver
    2019
    Posts
    25

    Re: How can i delete or clear cells that exists bfore zero cell ?

    Quote Originally Posted by Ochimus View Post
    hassan,

    Excel formulae will not allow you to "tell" another cell to change. It can only change the cell containing the formula. You will have to use a Macro.

    Option Explicit
    Dim c As Long, f As Long, m As Long, r As Long
    
    Sub NOZEROES()
    
    'Find last row.
     
        With Sheet1
        f = .Cells(.Rows.Count, "A").End(xlUp).Row
        
        If f = 1 Then Exit Sub
        
    'Find last column
        r = .Cells(1, .Columns.Count).End(xlToLeft).Column
    
    Look in each row        
         For m = 2 To f
    
    Look in each column
            For c = 1 To r
    
    If the cell is Zero, then clear it and all cells above it in that column
                If .Cells(m, c) = 0 Then
                .Range(.Cells(1, c), .Cells(m, c)).ClearContents
                End If
                
    Move to next column and repeat
            Next
    
    Move to next row and repeat
        Next
        
        End With
        
        End Sub
    Ochimus
    i tried it for my example and worked but when I tried it in the real data I have it didn't work .. should I press button because I got [ Cannot run the macro… the macro may not be available in this workbook ] and when I run the code from VBA i got no reaction

  4. #4
    Registered User
    Join Date
    01-17-2021
    Location
    Australia
    MS-Off Ver
    2019
    Posts
    25

    Re: How can i delete or clear cells that exists before zero cell ?

    Thanks for helping .. I'm trying it now but it will take much time to finish because I have 2000 column and row .. can I do it with a formula using if condition and drag it down to all cells for the check to be faster

  5. #5
    Forum Expert bebo021999's Avatar
    Join Date
    07-22-2011
    Location
    Vietnam
    MS-Off Ver
    Excel 2016
    Posts
    9,651

    Re: How can i delete or clear cells that exists before zero cell ?

    Try to list in other available range, then copy/paste value back to the original range.

    =IFERROR(IF(ROW($A1)<=MATCH(0,A:A,0),"",A1),A1)

    Drag down and accross
    Attached Files Attached Files
    Quang PT

  6. #6
    Registered User
    Join Date
    01-17-2021
    Location
    Australia
    MS-Off Ver
    2019
    Posts
    25

    Re: How can i delete or clear cells that exists before zero cell ?

    thanks but i tried it and didn't work

  7. #7
    Forum Expert bebo021999's Avatar
    Join Date
    07-22-2011
    Location
    Vietnam
    MS-Off Ver
    Excel 2016
    Posts
    9,651

    Re: How can i delete or clear cells that exists before zero cell ?

    Does it work for sample file?
    With your actual file, could you attach a small piece of file (few rows / columns) showing the error?

  8. #8
    Registered User
    Join Date
    01-17-2021
    Location
    Australia
    MS-Off Ver
    2019
    Posts
    25

    Re: How can i delete or clear cells that exists before zero cell ?

    Quote Originally Posted by bebo021999 View Post
    Does it work for sample file?
    With your actual file, could you attach a small piece of file (few rows / columns) showing the error?
    Many thanks for your helping i tried the above answer and worked .. Thanks again

  9. #9
    Valued Forum Contributor
    Join Date
    01-16-2012
    Location
    England
    MS-Off Ver
    MS 365 Version 2501 64-bit
    Posts
    1,465

    Re: How can i delete or clear cells that exists before zero cell ?

    Morning Hassan,

    The Macro will not take "much time". It will cycle through two thousand rows and however many columns extremely quickly.

    If you moved the Macro to a different file that has your "real" data, can you check which sheet number it is on? (not the worksheet name) and change the 'With Sheet1' to whatever. Or replace 'With Sheet1' to 'With Active sheet' and it will work.

    Ochimus

  10. #10
    Registered User
    Join Date
    01-17-2021
    Location
    Australia
    MS-Off Ver
    2019
    Posts
    25

    Re: How can i delete or clear cells that exists before zero cell ?

    Quote Originally Posted by Ochimus View Post
    Morning Hassan,

    The Macro will not take "much time". It will cycle through two thousand rows and however many columns extremely quickly.

    If you moved the Macro to a different file that has your "real" data, can you check which sheet number it is on? (not the worksheet name) and change the 'With Sheet1' to whatever. Or replace 'With Sheet1' to 'With Active sheet' and it will work.

    Ochimus
    yes I did it already and worked .. thanks

+ 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. Delete row if value exists in cell
    By taylorsm in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 12-21-2018, 09:57 PM
  2. Can cell.Clear be made to delete down to next value
    By taylorsm in forum Excel Programming / VBA / Macros
    Replies: 16
    Last Post: 12-06-2017, 01:08 PM
  3. VBA to Clear 1 Cell and Clear other cells Formula only
    By oneblondebrow in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-17-2017, 12:56 PM
  4. [SOLVED] Clear cells to the left of active cell when it is delete/cleared
    By nickmax1 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 09-21-2015, 05:20 AM
  5. [SOLVED] if sheet 9 exists, clear it otherwise create one
    By asdzxc in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 08-31-2014, 06:17 PM
  6. Code to clear contents of specific cells based upon if key exists in other sheets
    By seputus in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-31-2013, 07:25 PM
  7. [SOLVED] Delete/clear a cell based on another cells contents
    By jademaddy in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-19-2005, 02:06 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