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.
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
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
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.
Sub Demo1() Dim V V = InputBox(vbLf & vbLf & "Valeur plancher :", " Suppression de lignes") If Not IsNumeric(V) Then Exit Sub 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 Sub
Do you like it ? So thanks to click on bottom left star icon « ★ Add Reputation » !
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 …
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
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.
Bookmarks