I have been working on code to calculate the percentage of complete pipes opposed to incomplete pipes in excel spreadsheets, here is the code:
Sub PercentCompletePipes()
Dim k As Range
Dim Counter As Integer
Dim Green As Integer
Dim Red As Integer
Red = 0
Green = 0
Counter = 0
xTitleId = "Percentage Completed Inverts"
MsgBox "This macro defines the percentage of pipes with completed inverts. It ignores all PRIVATE pipes."
MsgBox "WARNING: This macro only works with COMPLETED invert excel sheets. Please select the row that you would like to display the data in, and highlight it yellow."
For Each k In ActiveSheet.UsedRange.Rows
If Counter >= 4 Then
If ActiveSheet.Cells(k.Row, 1).Interior.ColorIndex = 4 Then
Green = Green + 1
ElseIf ActiveSheet.Cells(k.Row, 1).Interior.ColorIndex = 3 Then
Red = Red + 1
ElseIf ActiveSheet.Cells(k.Row, 1).Interior.ColorIndex = 6 Then
ActiveSheet.Cells(k.Row, 1).Value = "COMPLETED PIPES:"
ActiveSheet.Cells(k.Row, 4).Value = Green
ActiveSheet.Cells(k.Row + 1, 1).Value = "INCOMPLETE PIPES:"
ActiveSheet.Cells(k.Row + 1, 4).Value = Red
ActiveSheet.Cells(k.Row + 2, 1).Value = "PERCENTAGE COMPLETE:"
ActiveSheet.Cells(k.Row + 2, 4).Value = (Green / (Red + Green)) * 100
ActiveSheet.Cells(k.Row + 2, 5).Value = "%"
ActiveSheet.Cells(k.Row + 3, 1).Value = "NOTE: These values do not account for PRIVATE pipes."
End If
End If
Counter = Counter + 1
Next k
End Sub
As you can see this code will do what I mentioned earlier, albeit only after I have found the cell after the last row with data in it, and highlighted it yellow. Since I have hundreds of sheets to do this for, it is extremely tedious, and I'd instead rather make it find this cell itself. However, this blank cell is outside of the UsedRange in excel, thus my previous attempts at making this possible have failed since the code doesn't seem to work past the used range. Here is a picture of the sheet that I am trying to achieve this on, as you can see I have highlighted the cell outside of the used range yellow for the code to work.
Also, I cannot simply make the code look for the first "empty" (white, blank) row to put data in since 2 rows at the beginning are format separators, and are meant to stay blank. Any help is appreciated, let me know if you have any questions. Thanks!