我正在尝试创建允许我找到最后 8 个包含数据的单元格并重命名它们的宏。我正在使用它来查找最后一个单元格并重命名它。
ActiveSheet.Range("K65536").End(xlUp).Offset(0, 0).Select
ActiveCell.Name = "BackRho"
然后我需要转到接下来的 8 个数据点并重命名它们。问题是列并不总是满的,所以有时数据之间会有空格。我想我需要使用 Is() 空但无法弄清楚。
我相信这可以满足您的要求:
Sub RenameLast8()
Dim ws As Excel.Worksheet
Dim SearchColumn As Excel.Range
Dim FoundCell As Excel.Range
Dim FirstAddr As String
Dim FoundCells As Long
Set ws = ActiveSheet
Set SearchColumn = ws.Range("K:K")
Set FoundCell = SearchColumn.Cells.Find(searchdirection:=XlSearchDirection.xlPrevious, what:="*", lookat:=xlPart)
If Not FoundCell Is Nothing Then
FirstAddr = FoundCell.Address
FoundCells = FoundCells + 1
FoundCell.Name = "Name" & FoundCells
End If
Do Until FoundCell Is Nothing Or FoundCells = 8
Set FoundCell = SearchColumn.Cells.FindPrevious(after:=FoundCell)
If FoundCell.Address = FirstAddr Then
Exit Do
Else
FoundCells = FoundCells + 1
FoundCell.Name = "Name" & FoundCells
End If
Loop
End Sub
如果它发现少于 8 个,它就会停止。
改变ws
和SearchColumn
适应。我不确定你的命名约定,所以只使用了“名称”和FoundCells
索引。