我想帮助完成这段代码。我需要突出显示从活动单元格到变量行的列中的单元格。
我的代码片段:
Start = Cells.Find(What:="MEETING", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
ActiveCell.Offset(7, 0).Select
Range(ActiveCell & Stop).Select 'it is this bit that doesn't work
答案 0 :(得分:0)
您很少需要激活或选择范围
Sub BrokenPiece() Dim rStart As Range Dim rStop As Range Dim CompleteSelection As Range Set rStart = Cells.Find(What:="MEETING", After:=ActiveCell, LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False) If Not rStart Is Nothing Then Set rStop = rStart.Offset(7, 0) Set CompleteSelection = Range(rStart, rStop) CompleteSelection.Select End If End Sub
如果要在列(1)中找到最后一次出现的地址:
DateStop = Columns(1).Find(What:=“Note:”,LookAt:= xlPart,SearchDirection:= xlPrevious,MatchCase:= False).Row - 2
您首先要设置范围
DateStop = Columns(1).Find(What:=“Note:”,LookAt:= xlPart,SearchDirection:= xlPrevious,MatchCase:= False)
然后确保你确实找到了什么
如果不是DateStop则不是
现在有办法获得你想要的行
result = DateStop.Row -2
或
result = DateStop.Offset(-2).Row
如果选择Offset方法,则应测试DateStop.Row -2是否为有效行。在这里你不会抛出错误
如果DateStop.Row -2> 0然后
所以我们把它放在一起
Function DateStopRowMinus2() Dim DateStop Set DateStop = Columns(1).Find(What:="Note:", LookAt:=xlPart, SearchDirection:=xlPrevious, MatchCase:=False) If Not DateStop Is Nothing Then If DateStop.Row - 2 > 0 Then DateStopRowMinus2 = DateStop.Offset(-2) End If End If End Function
注意当我提到DateStop的Offset时,我没有指定一列。这是因为Offset属性有两个可选参数
偏移([行],[列])
我们知道它们是可选的,因为参数括在方括号[]中。以下是一些有效的例子