我使用下面的宏来选择用户定义短语的2次出现之间的所有单元格。
Sub FindOfficer()
Dim FindOfficer As String
Dim Rng1 As Range
Dim Rng2 As Range
Dim Rng3 As Range
FindOfficer = InputBox("Enter Officer's Name")
If Trim(FindOfficer) <> "" Then
With Sheets("Sheet1").Range("J:M")
Set Rng1 = .Find(What:=FindOfficer, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
Set Rng2 = .Find(What:=FindOfficer, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False)
Set Rng3 = .Range(Rng1.Address & ":" & Rng2.Address)
If Not Rng3 Is Nothing Then
Application.Goto Rng3, False
Else
MsgBox "Error"
End If
End With
End If
End Sub
正确选择我需要的行,但是只选择B到AG的列,而我希望选择B到AL。
关于如何更改代码来执行此操作的任何想法?我是VBA的新手
答案 0 :(得分:0)
有很多方法可以做到这一点。最简单的方法之一是使用R1C1单元格引用,其中列由数字引用,就像行一样范围B2是单元格2,2。您还可以使用范围的Row属性来确定该范围的起始行。替换这个:
Set Rng3 = .Range(Rng1.Address & ":" & Rng2.Address)
用这个:
Set Rng3 = .Range(.Cells(Rng1.Row,2), .Cells(Rng2.Row, 38))