+ Reply to Thread
Results 1 to 15 of 15

Delete row if cells in columns A to D are <0

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    06-05-2013
    Location
    Austria
    MS-Off Ver
    Excel 2016
    Posts
    165

    Delete row if cells in columns A to D are <0

    Hi everyone,
    I need your help, please. I need a macro that deletes all rows in which the value of the cells from columns A to D are <1.
    The values are starting from A10 to D10.

    For Example:

    A B C D
    10 1 2,5 3 0
    11 0 0 0 0
    12 5 0 0 0

    The macro should delete row 11 because there are all values <1.

    Thanks a lot
    Kind regards
    Roman

  2. #2
    Forum Guru
    Join Date
    02-27-2016
    Location
    Vietnam
    MS-Off Ver
    2021
    Posts
    5,927

    Re: Delete row if cells in columns A to D are <0

    Try;
    Public Sub Test()
    Dim lastRow As Long
    Dim i As Long
    With Sheets("Sheet1")
        lastRow = .Range("A" & Rows.Count).End(xlUp).Row
        If lastRow < 10 Then Exit Sub
        For i = lastRow To 10 Step -1
            If .Cells(i, "A") < 1 And .Cells(i, "B") < 1 And .Cells(i, "C") < 1 And .Cells(i, "D") < 1 Then
            .Rows(i).Delete
            End If
        Next
    End With
    End Sub

  3. #3
    Forum Contributor
    Join Date
    06-05-2013
    Location
    Austria
    MS-Off Ver
    Excel 2016
    Posts
    165

    Re: Delete row if cells in columns A to D are <0

    Thanks a lot. I will try it.

  4. #4
    Forum Contributor
    Join Date
    06-05-2013
    Location
    Austria
    MS-Off Ver
    Excel 2016
    Posts
    165

    Re: Delete row if cells in columns A to D are <0

    Hy, I have tried to modify the macro:

    Sub InputNumber()
    Dim lastRow As Long
    Dim i As Long
    
    Dim wert As String, x%, ok As Boolean
    Anfang:
        wert = InputBox("Bitte den Schwellwert eingeben", "Eingabe", "1,000")
        If wert = "" Then Exit Sub
        
        If IsNumeric(wert) Then
    
    With Sheets("Import")
        'With ImportData
        lastRow = .Range("A" & Rows.Count).End(xlUp).Row
        If lastRow < 1 Then Exit Sub
        For i = lastRow To 1 Step -1
            If .Cells(i, "A") <= wert And .Cells(i, "B") <= wert And .Cells(i, "C") <= wert And .Cells(i, "D") <= wert Then
            .Rows(i).Delete
            End If
        Next
    End With
       
        'MsgBox "Zahl OK"
        Else
            For x = 1 To Len(wert)
                Select Case Mid(wert, x, 1)
                    Case 0 To 9, ",", " ": ok = True
                    Case Else: ok = False: Exit For
                End Select
                 
                Next x
                    If Not ok Then
                        If MsgBox("Es dürfen nur Zahlen und Kommas eingegeben werden!", vbOKCancel, "Nur Zahlen eingeben!") = vbOK Then
                    GoTo Anfang
                Else
                Exit Sub
            End If
        End If
    End If
    End Sub
    The idea is to check first if there are only numbers typed into the inputbox. This works. But I have problems to delete the rows if the number is =< in rows A to D. There happens nothing.
    Can you help me please?
    Thanks a lot.
    Kind regards
    Roman

  5. #5
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Re: Delete row if cells in columns A to D are <0


    Hi !

    Attach a workbook, could be easier to help … Criteria is < 0 or = 0 ?

  6. #6
    Forum Contributor
    Join Date
    06-05-2013
    Location
    Austria
    MS-Off Ver
    Excel 2016
    Posts
    165

    Re: Delete row if cells in columns A to D are <0

    Criteria < the value of the input box or equal to the input box

  7. #7
    Forum Contributor
    Join Date
    06-05-2013
    Location
    Austria
    MS-Off Ver
    Excel 2016
    Posts
    165

    Re: Delete row if cells in columns A to D are <0

    Hi, here is the workbook
    Attached Files Attached Files

  8. #8
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Re: Delete row if cells in columns A to D are <0


    It seems your workbook does not match with your original explanation :

    Quote Originally Posted by moosmahna View Post
    The values are starting from A10 to D10.
    As the better original explanation & attachment, the quicker & better solution !

    So explain clearly what do to with your attachment …

  9. #9
    Forum Contributor
    Join Date
    06-05-2013
    Location
    Austria
    MS-Off Ver
    Excel 2016
    Posts
    165

    Re: Delete row if cells in columns A to D are <0

    Hi Marc,
    If you start the macro, you get the inputbox. There you have to type in a number. The macro looks if it is a number. If not, then the macro can be stopped or you can try to type in a number again.
    If you have typed in a number, then it is stored in the variable "wert".
    With this number, the macro searches on sheet "Import" if the number "wert" is smaller or equal in the cells A-D in each row. If the number in a row, column A-D (in all Columns of this row), is equal or smaller then the varible "wert", then the row has to be deletet.

    Thanks again

  10. #10
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Cool Try this ‼


    As a beginner starter like operating manually :

    PHP Code: 
    Sub Demo1()
         
    Dim V
             V 
    InputBox(vbLf vbLf "Valeur plancher :""  Suppression de lignes")
             If 
    Not IsNumeric(VThen Exit Sub
             Application
    .ScreenUpdating False
        With ImportData
    .UsedRange.Resize(, 5).Rows
            
    .Columns(5).Formula Replace("=AND(A1<=#,B1<=#,C1<=#,D1<=#)""#"V)
            .
    Sort .Cells(5), xlAscendingHeader:=xlNo
             V 
    Application.Match(True, .Columns(5), 0)
             If 
    IsNumeric(VThen .Item(":" & .Count).Clear
            
    .Columns(5).Clear
        End With
             Application
    .ScreenUpdating True
    End Sub 
    Do you like it ? So thanks to click on bottom left star icon « Add Reputation » !

  11. #11
    Forum Contributor
    Join Date
    06-05-2013
    Location
    Austria
    MS-Off Ver
    Excel 2016
    Posts
    165

    Re: Delete row if cells in columns A to D are <0

    Hi Marc,
    thanks for your help. It looks very good. I will try to modify it. Can´t believe that the code can be so small.

  12. #12
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Re: Delete row if cells in columns A to D are <0


    Thanks for the rep' !

    The code can be so small just thinking how Excel can help instead of a poor full VBA way
    as using a loop can be the slowest way, as clearing a block at once - even manually ! - is faster than deleting row by row …

  13. #13
    Forum Contributor
    Join Date
    06-05-2013
    Location
    Austria
    MS-Off Ver
    Excel 2016
    Posts
    165

    Re: Delete row if cells in columns A to D are <0

    Hi Marc,
    I have modified the code but now nothing happens.....

    Sub DeleteNumbers()
         Dim V
    Anfang:
             V = InputBox(vbLf & vbLf & "Bitte Zahl eingeben :", "  Schwellwert eingeben")
             If Not IsNumeric(V) Then
                If MsgBox("Only Numbers Allowed!", vbOKCancel, "Numbers") = vbOK Then
                GoTo Anfang
                Else
             Exit Sub
             End If
             Application.ScreenUpdating = False
        With ImportData.UsedRange.Resize(, 5).Rows
            .Columns(5).Formula = Replace("=AND(A1<=#,B1<=#,C1<=#,D1<=#)", "#", V)
            .Sort .Cells(5), xlAscending, Header:=xlNo
             V = Application.Match(True, .Columns(5), 0)
             If IsNumeric(V) Then .Item(V & ":" & .Count).Clear
            .Columns(5).Clear
        End With
             Application.ScreenUpdating = True
             End If
    End Sub
    Any ideas what is wrong with the code?
    Thanks again

  14. #14
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Re: Delete row if cells in columns A to D are <0


    Maybe a bad entry …

    Try my original Demo1 with your original attachment with value = 2.
    Last edited by Marc L; 08-08-2018 at 07:59 AM.

  15. #15
    Valued Forum Contributor
    Join Date
    04-01-2015
    Location
    The Netherlands
    MS-Off Ver
    2003/2007/2010/2016/office 365
    Posts
    880

    Re: Delete row if cells in columns A to D are <0

    As an alternative
    Sub VenA()
      t = Application.InputBox(vbLf & vbLf & "Bitte Zahl eingeben :", "  Schwellwert eingeben", , , , , , 1)
      If t = False Then Exit Sub
      With Sheets("Import")
        .Rows(1).Insert
        .Cells(1).Resize(, 4) = Split("1 2 3 4")
        .Cells(2, 6) = "=AND(A2<=" & t & ",B2<=" & t & ",C2<=" & t & ",D2<=" & t & ")"
        .Cells(1).CurrentRegion.AdvancedFilter xlFilterInPlace, .Range("F1:F2")
        .Cells(1).CurrentRegion.EntireRow.Delete
        .Range("F1:F2").Clear
        .ShowAllData
        Application.Goto .Cells(1), True
      End With
    End Sub
    Last edited by Vraag en antwoord; 08-08-2018 at 08:45 AM.

+ 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. Compare 2 columns, delete cells with no match
    By cmd in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 07-22-2019, 04:52 AM
  2. [SOLVED] VBA to Check Through Cells for 2 Columns and Delete Row if Cells Contains
    By Astriddd in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-29-2015, 02:27 AM
  3. Delete Columns if cells in row 5 has a specific Text
    By VKu in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-19-2015, 08:52 AM
  4. Delete The cells above a red colored cell in columns
    By zeroist in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-10-2014, 05:33 AM
  5. Delete empty cells from Columns
    By MKZS in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-06-2013, 07:57 AM
  6. Delete The cells above a specific cell in columns
    By zeroist in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 11-28-2013, 02:56 AM
  7. Need to delete cells, not columns or rows, based on LEN=0
    By jenny_journalist in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-29-2009, 04:11 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