我有一段代码可以从Sheet1复制某个范围,在Sheet4中搜索一个名为“标记1”的文本,然后将复制自Sheet1的复制范围粘贴到Sheet4的“标记1”单元中。
Sub FindCopyPasteV1()
Dim FindM1 As Range
Dim CopyM1 As Range
With Worksheets("Sheet1").Range("A:DD")
Set CopyM1 = Sheets("Sheet1").Range("E6:E32")
End With
With Worksheets("Sheet4").Range("A:DD")
Set FindM1 = .Find(What:="Marker 1", LookAt:=xlWhole, MatchCase:=True, SearchFormat:=False)
CopyM1.Copy FindM1
End With
End Sub
现在,我想更改代码,以便可以使用Range Object(FindH1)选择要从Sheet1复制的单元格。
Sub FindCopyPasteV2()
Dim FindH1 As Range
Dim FindM1 As Range
Dim CopyM1 As Range
With Worksheets("Sheet1").Range("A:DD")
Set FindH1 = .Find(What:="Header 1", LookAt:=xlWhole, MatchCase:=True, SearchFormat:=False)
Set CopyM1 = Sheets("Sheet1").Range("FindH1.E32") 'This doesn't work.
'Instead of Set CopyM1 = Sheets("Sheet1").Range("E6:E32")
End With
With Worksheets("Sheet4").Range("A:DD")
Set FindM1 = .Find(What:="Marker 1", LookAt:=xlWhole, MatchCase:=True, SearchFormat:=False)
CopyM1.Copy FindM1
End With
End Sub
答案 0 :(得分:1)
您可以使用“调整大小”来引用从找到的单元格开始的范围:
Set CopyM1 = FindH1.Resize(howManyRows, howManyCols)