Hi again Ben,
Original code changed so that it only deletes 8 columns of data. There is an
InputBox where you will have to select any cell in the column with the
incrementing/decrementing data to tell the code which set of data to work
with.
To make it easier to code your second part, I have also changed the labels
so they have a fixed number of digits like Set 01, Set 02 etc. See the
comments where you can change the number of digits and make it 3 digits or 4
digits if you like.
The second sub is for deleting specific sets of data. It has 3 InputBoxes:
The first InputBox to select the column with the Set labels.
The second InputBox to enter the number of the first label set to delete.
The third InputBox to enter the last number of the labels sets to delete.
Note when entering the number of the labels to delete, you only enter a
number like 8. Does not require the leading zeros.
If only deleting one set say Set 05 then just enter 5 for the first one and
5 for the last one.
As I did previously, the code is set to color the rows to delete. When you
are satisfied that it is identifying the correct rows, remove the comment
(single quote) from the Exit sub line.
Following is replacement for previous code.
Sub DeleteDecrementingRows()
Dim lngCol As Long
Dim i As Long
Dim lngStart As Long
Dim lngLastRow As Long
Dim lngCount As Long
Dim strFormat As String
'Edit following line to first row of data
'Assumes column headers & starts row 2
lngStart = 2
'*********************************************
'Edit the following line to the number of _
digits required in the Set label _
NOTE: Zeros not aplha characters
strFormat = "00"
'*********************************************
lngCount = 0 'Initialize
On Error Resume Next
lngCol = Application.InputBox _
(Prompt:="Select the column with the" & _
vbLf & "Incrementing/Decrementing data", _
Title:="Data Select", Type:=8).Column
If Err.Number > 0 Then
MsgBox "User cancelled." & vbLf & vbLf & _
"Processing terminated."
Exit Sub
End If
On Error GoTo 0 'Reset error trapping
'Edit "Sheet1" to your sheet name
With Sheets("Sheet1")
lngLastRow = .Cells(.Rows.Count, lngCol) _
.End(xlUp).Row
'Insert label for first group if ascending
If .Cells(lngStart, lngCol) < _
.Cells(lngStart + 1, lngCol) Then
lngCount = lngCount + 1
.Cells(i + 2, lngCol - 2) = "Set " _
& Format(lngCount, strFormat)
Else
.Cells(lngStart, lngCol) _
.Interior.ColorIndex = 6
End If
For i = lngStart To lngLastRow
If .Cells(i + 1, lngCol) < .Cells(i, lngCol) Then
'If next cell is start of another incrementing _
group instead of decrementing group
If .Cells(i + 1, lngCol) < .Cells(i + 2, lngCol) _
And .Cells(i + 1, lngCol) >= 1 Then
lngCount = lngCount + 1
.Cells(i + 1, lngCol - 2) = "Set " _
& Format(lngCount, strFormat)
GoTo Next_i_Loop
End If
Do
i = i + 1
If i > lngLastRow Then
Exit Do 'Past end of data
End If
.Range(.Cells(i, lngCol - 2), _
.Cells(i, lngCol + 5)) _
.Interior.ColorIndex = 6
Loop While .Cells(i + 1, lngCol) <= _
.Cells(i, lngCol) Or _
.Cells(i + 1, lngCol) < 1
lngCount = lngCount + 1
'Suppress label below actual data
If i + 1 <= lngLastRow Then
.Cells(i + 1, lngCol - 2) = "Set " _
& Format(lngCount, strFormat)
End If
End If
Next_i_Loop:
Next i
'*************************************
'Remove the Exit sub and run again
'if you are satisfied that the highlighted
'rows are the correct ones to delete.
Exit Sub
'*************************************
'Must work backwards when deleting rows
For i = lngLastRow To 2 Step -1
If .Cells(i, lngCol).Interior.ColorIndex = 6 Then
.Range(.Cells(i, lngCol - 2), _
.Cells(i, lngCol + 5)).Delete
End If
Next i
End With
End Sub
Following is new code to delete specifiec sets of data.
Sub DeleteDataSets()
Dim lngCol As Long
Dim lngLastRow As Long
Dim varSet1 As Variant
Dim varSet2 As Variant
Dim strFormat As String
Dim rngColumn As Range
Dim rngTofind As Range
Dim i As Long
'******************************************
'Edit the following line to the number of _
digits in the Set label _
NOTE: Zeros not aplha characters
strFormat = "00"
'*******************************************
On Error Resume Next
lngCol = Application.InputBox _
(Prompt:="Select the column with the" & _
"Set xx labels", _
Title:="Data Select", Type:=8).Column
If Err.Number > 0 Then
MsgBox "User cancelled." & vbLf & vbLf & _
"Processing terminated."
Exit Sub
End If
On Error GoTo 0 'Reset error trapping
Set rngColumn = Columns(lngCol)
'Last row of column with incrementing data sets
lngLastRow = Cells(Rows.Count, _
lngCol + 2).End(xlUp).Row
varSet1 = Application.InputBox _
(Prompt:="Enter first set number to delete.", _
Title:="Delete Selection", Type:=1)
If varSet1 = False Then
MsgBox "User cancelled." & vbLf & vbLf & _
"Processing terminated."
Exit Sub
End If
varSet2 = Application.InputBox _
(Prompt:="Enter second set number to delete.", _
Title:="Delete Selection", Type:=1)
If varSet2 = False Then
MsgBox "User cancelled." & vbLf & vbLf & _
"Processing terminated."
Exit Sub
End If
varSet1 = "Set " & Format(varSet1, strFormat)
varSet2 = "Set " & Format(varSet2, strFormat)
Set rngTofind = rngColumn.Find(What:=varSet1, _
LookIn:=xlFormulas, _
LookAt:=xlWhole, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not rngTofind Is Nothing Then
i = rngTofind.Row
Do
Range(Cells(i, lngCol), Cells(i, lngCol + 7)) _
.Interior.ColorIndex = 6
i = i + 1
Loop While Cells(i, lngCol) <= varSet2 _
And i <= lngLastRow
Else
MsgBox "Numberset " & varSet1 & " not found." _
& vbLf & "Processing terminated."
Exit Sub
End If
'*************************************
'Remove the Exit sub and run again
'if you are satisfied that the highlighted
'rows are the correct ones to delete.
Exit Sub
'*************************************
For i = lngLastRow To 2 Step -1
If Cells(i, lngCol).Interior.ColorIndex = 6 Then
Range(Cells(i, lngCol), _
Cells(i, lngCol + 7)).Delete
End If
Next i
End Sub
Regards,
OssieMac