我一直在编写代码来计算excel电子表格中与不完整管道相对的完整管道的百分比,这里是代码:
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
正如您所看到的,此代码将执行我之前提到的操作,尽管只是在我找到包含数据的最后一行之后的单元格之后,并将其突出显示为黄色。由于我有数百张要做的事情,这非常繁琐,而我宁愿让它找到这个细胞本身。但是,这个空白单元格在excel中的UsedRange之外,因此我之前尝试实现此功能的尝试失败了,因为代码似乎没有超出使用范围。这是我试图实现这一目标的工作表的图片,正如您所看到的,我已经突出显示了使用范围黄色之外的单元格以使代码生效。 此外,我不能简单地让代码查找第一个“空”(白色,空白)行来放入数据,因为开头的两行是格式分隔符,并且意味着保持空白。如有任何帮助,请与我们联系。谢谢!
答案 0 :(得分:1)
请考虑以下事项:
Sub Foo1()
Dim ws As Worksheet: Set ws = ActiveSheet
Dim Rng As Range: Set Rng = Application.Intersect(ws.Range("A:A"), ws.UsedRange)
Set Rng = Application.WorksheetFunction.Index(Rng, Rng.Count).Offset(1, 0)
Rng.Select
End Sub
在UsedRange中找到“A”列中的最后一个单元格,然后偏移1行。
答案 1 :(得分:1)
我想用来选择最后一次使用行之后的第一行的模式是.Range("A" & .Rows.Count).End(xlUp).Offset(1)
。 You Flephal指出,它依赖于最后一行中有数据的第一个单元格。
Sub DemoPercentCompletePipes()
PercentCompletePipes ActiveSheet
End Sub
Sub PercentCompletePipes(ws As Worksheet)
Dim cell As Range
Dim Counter As Integer, Green As Integer, Red As Integer
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."
With ws
'Define target range starting at "A4" and extending to the last used cell in column A
With .Range("A4", .Range("A" & .Rows.Count).End(xlUp))
For Each cell In .Cells
If cell.Interior.ColorIndex = 4 Then
Green = Green + 1
ElseIf cell.Interior.ColorIndex = 3 Then
Red = Red + 1
End If
Next
End With
'Target the next empty row in Column A
With .Range("A" & .Rows.Count).End(xlUp).Offset(1).EntireRow
.Cells(1, 1).Value = "COMPLETED PIPES:"
.Cells(1, 4).Value = Green
.Cells(2, 1).Value = "INCOMPLETE PIPES:"
.Cells(2, 4).Value = Red
.Cells(3, 1).Value = "PERCENTAGE COMPLETE:"
.Cells(3, 4).Value = (Green / (Red + Green)) * 100
.Cells(3, 5).Value = "%"
.Cells(4, 1).Value = "NOTE: These values do not account for PRIVATE pipes."
End With
End With
End Sub
提取的数据