Sub PercentCompletePipes()
Dim k As Range
Dim Counter As Integer
Dim Green As Integer
Dim Red As Integer
Red = 0
Green = 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."
For Each k In ActiveSheet.UsedRange.Rows
Counter = 0
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, 2).Value = Green
ActiveSheet.Cells(k.Row + 1, 1).Value = "INCOMPLETE PIPES:"
ActiveSheet.Cells(k.Row + 1, 2).Value = Red
ActiveSheet.Cells(k.Row + 2, 1).Value = "PERCENTAGE COMPLETE:"
ActiveSheet.Cells(k.Row + 2, 2).Value = Green / (Red + Green)
End If
End If
Counter = Counter + 1
Next k
End Sub
很抱歉入门级问题,但我试图根据每行中的第一个单元格是绿色还是红色打印出我的Excel工作表中的行完成情况(绿色表示完整,红色表示不完整)。它应该忽略所有其他颜色(灰色,白色等)。当我使用上面发布的代码段时,不会生成任何输出。一旦我突出显示行中的第一个单元格黄色(颜色索引= 6),它应该生成输出。 Image of excel file I'm trying to analyze.
答案 0 :(得分:0)
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."
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, 2).Value = Green
ActiveSheet.Cells(k.Row + 1, 1).Value = "INCOMPLETE PIPES:"
ActiveSheet.Cells(k.Row + 1, 2).Value = Red
ActiveSheet.Cells(k.Row + 2, 1).Value = "PERCENTAGE COMPLETE:"
ActiveSheet.Cells(k.Row + 2, 2).Value = Green / (Red + Green)
End If
End If
Counter = Counter + 1
Next k
End Sub
这解决了它。我错误地将计数器变量放在for循环中。