[英]How to set a Range variable using .Find?
I'm trying to set a range variable "rcell" equal to the range of a cell containing the value of variable "lot" by using the .Find
function.我正在尝试使用.Find
函数将范围变量“rcell”设置为等于包含变量“lot”值的单元格的范围。
I want to search in a specific range (column D) instead of all cells.我想在特定范围(D 列)而不是所有单元格中进行搜索。
I get a type mismatch error.我收到类型不匹配错误。
I tried using "Columns(4)" instead of "Range(D:D)".我尝试使用“列(4)”而不是“范围(D:D)”。
Workbooks("lotworkbook.xlsx").Activate
Sheets("Sheet1").Select
lot = Cells(row, 3)
Workbooks("findworkbook.xlsx").Activate
Sheets(3).Select
Dim rcell As Range
Set rcell = Range("D:D").Find(What:=lot, After:=ActiveCell, LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=False) 'this is the line with issues'
If rcell Is Nothing Then
Else
' ...
Expected results: Find cell with the value of variable "lot" in a certain sheet and find that value on another sheet, and set range variable "rcell" equal to that cell location.预期结果:在某个工作表中找到具有变量“lot”值的单元格,并在另一个工作表上找到该值,并将范围变量“rcell”设置为等于该单元格位置。
Actual results: Type mismatch error.实际结果:类型不匹配错误。 This was working with Cells.Find
but not with Range("D:D").Find
.这适用于Cells.Find
但不适用于Range("D:D").Find
。
I believe the issue is After:=ActiveCell
- remove this and try again:我相信问题是After:=ActiveCell
- 删除它After:=ActiveCell
试:
Dim rcell As Range
Set rcell = Range("D:D").Find(What:=lot, LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=False) 'this is the line with issues
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.