[英]How To Number In Sequence Only Visible Rows In Excel Using VBA?
The goal is to have my code number only the visible rows in sequence so that if a filter is applied or lines are deleted the end user can click the "Refresh" button, shown in the image below, updating the sequence and the summary at the top of the page.目标是让我的代码仅按顺序排列可见行,以便如果应用过滤器或删除行,最终用户可以单击“刷新”按钮,如下图所示,更新序列和摘要页面顶部。
The code I am using at the moment applies to all the cells in the range including those that are hidden and I have no idea how to change it so that it only applies to the visible cells.我目前使用的代码适用于该范围内的所有单元格,包括隐藏的单元格,我不知道如何更改它以使其仅适用于可见单元格。
And this is the code attached to my "Refresh" button;这是附加到我的“刷新”按钮的代码;
Private Sub Refresh_Click()
Application.Goto Reference:="R10C2"
Selection.End(xlDown).Select
Dim maxRowIndex As Long
Dim rowCounter As Long
maxRowIndex = ActiveCell.Row - 9
rowCounter = 1
Range("A10").Select
For rowCounter = 1 To maxRowIndex
ActiveCell = rowCounter
ActiveCell.Offset(1).Select
Next
End Sub
Thanks in advance!提前致谢!
This is untested, so try it out on a copy of your data, I also assumed you want to put the numbering in column A
:这是未经测试的,因此请在您的数据副本上尝试一下,我还假设您想将编号放在
A
列中:
Private Sub Refresh_Click()
Dim totalRows As Long
Dim rowCounter As Long
Dim i as long
rowCounter = 1
totalRows = ActiveSheet.UsedRange.Rows.Count + ActiveSheet.UsedRange.Row - 1
for i = 10 to totalRows
If ActiveSheet.Rows(i).Hidden = False then
Range("A" & i).Value = rowCounter
rowCounter = rowCounter + 1
End if
Next i
End Sub
This will basically loop through all the rows, check to see if they're visible and if yes, insert the row counter这将基本上遍历所有行,检查它们是否可见,如果是,则插入行计数器
Work through the cells in the SpecialCells(xlCellTypeVisible).处理 SpecialCells(xlCellTypeVisible) 中的单元格。
Option Explicit
Private Sub Refresh_Click()
Dim a As Long, n As Long, r As Range
If CBool(Application.Subtotal(103, Range(Cells(10, "B"), Cells(Rows.Count, "B")))) Then
Range(Cells(10, "B"), Cells(Rows.Count, "B").End(xlUp)).Offset(0, -1).ClearContents
For Each r In Range(Cells(10, "B"), Cells(Rows.Count, "B").End(xlUp)).SpecialCells(xlCellTypeVisible)
n = n + 1
r.Offset(0, -1) = n
Next r
End If
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.