I have several worksheets in a workbook, that have subtotals seperated by fill color (yellow or fillcolorindex =6). I need a procedure or function that will loop through the worksheet and find all of the fill colored rows and return the count of these rows, then for every 6 insert a row that will seperate them (no matter the total for these 6). Also I need to know the total sum of all of the fill rows per worksheet, and the total of how many seperated areas of 6 subtotals I have.
In other words each power panel can only have 6 branches per panel, and the branches are seperated by 20 amps or less. My code goes through the worksheet and counts up the amperage and when I have reached 20 ( or would go over 20 if I included the next row) it inserts the total amps for this series and highlights the row by fill color of yellow.
Now I need to know how many power panels are on each sheet, and the total amperage of each sheet.
Here is the code to count the amperage and insert the yellow line.
I also already have a function that finds all of the yellow rows:
Hopefully this post is not TOO rambling, but I could use some help coding this little assignment.
Thanks
In other words each power panel can only have 6 branches per panel, and the branches are seperated by 20 amps or less. My code goes through the worksheet and counts up the amperage and when I have reached 20 ( or would go over 20 if I included the next row) it inserts the total amps for this series and highlights the row by fill color of yellow.
Now I need to know how many power panels are on each sheet, and the total amperage of each sheet.
Here is the code to count the amperage and insert the yellow line.
Code:
Public Sub cmdBranches_Click()
Dim R As Long, C As Long
Dim thisval As Variant
Dim MaxAllowed As Variant
Dim tot As Variant
Application.ScreenUpdating = False
If SheetHasRun = True Then
Call FindFillRows
End If
If SheetHasRun = False And ActiveSheet.Name <> ("Formatted_Data") And ActiveSheet.Name <> ("ELM_Data") And ActiveSheet.Name <> _
("Totals") And ActiveSheet.Name <> ("Instructions for use") Then
MaxAllowed = 20
R = 3
C = 10
With ActiveSheet
While .Cells(R, C) Or .Cells(R, 2) <> "" 'loop while the cell tested is not empty
'get this row's value
thisval = .Cells(R, C)
'if over MaxAllowed, show sum
If tot + thisval > MaxAllowed Or .Cells(R, 2) = "X" Then
'insert a line above the current line
If R > 3 Then
.Rows(R).Select
Selection.Insert Shift:=xlDown
'Selection.Insert Shift:=xlDown
End If
'insert the total for this set
.Cells(R, C) = tot
Selection.Font.Bold = True
With Selection.Interior
.ColorIndex = 6
.Pattern = xlSolid
End With
'zero the total count
tot = 0
'move down to the start of the next set
R = R + 1
Else
'we are below the required total so add this value
tot = tot + thisval
End If
'shift the check point to the next row
R = R + 1
Wend
.Rows(R).Select
'insert the total for this set
.Cells(R, C) = tot
'Bold the totals
Selection.Font.Bold = True
'Highlight the rows with totals
With Selection.Interior
.ColorIndex = 6
.Pattern = xlSolid
End With
'Delete rows from top of sheet that are blank
'Rows("2:3").Select
'Selection.Delete Shift:=xlUp
.Range("b3").Select
End With
End If
Application.ScreenUpdating = True
SheetHasRun = True
End Sub
Code:
Function FindFillRows()
Application.ScreenUpdating = False
On Error GoTo errhandler
If SheetHasRun = True And ActiveSheet.Name <> ("Formatted_Data") And ActiveSheet.Name <> ("ELM_Data") And ActiveSheet.Name <> _
("Totals") And ActiveSheet.Name <> ("Instructions for use") Then
R = 3
C = 10
'Loop through the worksheet finding the fill color rows.
With ActiveSheet
While .Cells(R, C) <> "" 'loop while the cell tested is not empty
With Application.FindFormat.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ColorIndex = 6
End With
'Application.FindFormat.Locked = True
'Application.FindFormat.FormulaHidden = False
If SheetHasRun = True Then
ActiveSheet.Cells.Find(What:="", After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
, SearchFormat:=True).Activate
' Select the entire row.
Selection.EntireRow.Select
' Delete the row.
Selection.Delete
'Call the sub to delete the blank rows.
End If
R = R + 1
Wend
End With
End If
errhandler:
Application.ScreenUpdating = True
DeleteBlankRows
End Function
Thanks