Full code below
the purpose of the macro is to insert a row above certain row labels (in col
2), add a new row label for this row, then add together two particualr
existing rows and paste into the new row. (using pastespecial add operation).
Note that the row labels are repeated once in each sheet.The data looks like:
col2: month1 month2 month3...........................
rowlabel1
rowlabel2
rowlabel3
rowlabel4
rowlabel5
rowlabel1
rowlabel2
rowlabel3
rowlabel4
rowlabel5
Sub tst()
Dim sht As Worksheet
Dim cll As Range
Dim r As Integer
Dim c As Integer
Dim rwn As Integer
Dim rwn2 As Integer
Dim rwn3 As Integer
Dim rwn4 As Integer
Dim rwn5 As Integer
Dim rwn6 As Integer
Dim counter As Integer
'for each "name" extract
For Each sht In ThisWorkbook.Worksheets
If Trim(sht.Range("a1").Value) = "name" Then
'insert new rows just below "CANC"
For Each cll In sht.UsedRange.Columns(2).Cells
If cll.Row >= 2 Then
If Trim(cll.Value) = "DECLINED" And cll.Offset(-1, 0) <> 0 And
cll.Offset(-1, 0).Value <> "CANC+NTU" Then
cll.EntireRow.Insert
cll.Offset(-1, 0).Value = "CANC+NTU"
Else: End If
Else: End If
Next cll
'get row numbers of ranges to be added together
For Each cll In sht.UsedRange.Columns(2).Cells
If cll.Value = "CANC" Or cll.Value = "NTU" Or cll.Value =
"CANC+NTU" Then
counter = counter + 1
If counter = 1 Or counter = 2 Or counter = 3 Then
Select Case Trim(cll.Value)
Case Is = "CANC"
rwn = cll.Row
Case Is = "NTU"
rwn2 = cll.Row
Case Is = "CANC+NTU"
rwn3 = cll.Row
Case Else
End Select
ElseIf counter = 4 Or counter = 5 Or counter = 6 Then
Select Case Trim(cll.Value)
Case Is = "CANC"
rwn4 = cll.Row
Case Is = "NTU"
rwn5 = cll.Row
Case Is = "CANC+NTU"
rwn6 = cll.Row
Case Else
End Select
Else: End If
Else: End If
Next cll
' add rows into new row
'm
c = sht.UsedRange.Columns.Count
MsgBox c
MsgBox sht.Range(Cells(rwn, 3), Cells(rwn, c)).Address
MsgBox c
sht.Range(Cells(rwn, 3), Cells(rwn, c)).Copy
sht.Range(Cells(rwn3, 3), Cells(rwn3, c)).Paste
sht.Range(Cells(rwn2, 3), Cells(rwn2, c)).Copy
sht.Range(Cells(rwn3, 3), Cells(rwn3, c)).PasteSpecial operation:=xlAdd
'p
sht.Range(Cells(rwn4, 3), Cells(rwn4, c)).Copy
sht.Range(Cells(rwn6, 3), Cells(rwn6, c)).Paste
sht.Range(Cells(rwn5, 3), Cells(rwn5, c)).Copy
sht.Range(Cells(rwn6, 3), Cells(rwn6, c)).PasteSpecial operation:=xlAdd
Else: End If
Next sht
End Sub