【发布时间】:2012-02-14 17:19:47
【问题描述】:
我正在尝试查找具有字符串(例如名称)的特定列中的单元格,将列中的相应单元格复制到其右侧(即 offset(0,1) ),然后将其粘贴到不同的工作表。我有以下代码来查找我想要的范围变量。但是,我无法从其他工作表中选择它!
当我使用Sheets(1).MyRange.Copy 时,它不接受它。我是否以错误的方式指代范围?我做错了什么?
这是我用来获取 MyRange 的代码:
Option Explicit
Sub SelectByValue(Rng1 As Range, Value As Double)
Dim MyRange As Range
Dim Cell As Object
'Check every cell in the range for matching criteria.
For Each Cell In Rng1
If Cell.Value = Value Then
If MyRange Is Nothing Then
Set MyRange = Range(Cell.Address)
Else
Set MyRange = Union(MyRange, Range(Cell.Address))
End If
End If
Next
End Sub
Sub CallSelectByValue()
'Call the macro and pass all the required variables to it.
'In the line below, change the Range, Minimum Value, and Maximum Value as needed
Call SelectByValue(Sheets(1).Range("A1:A20"), "Tom")
End Sub
还有一个问题:与其指定要查看的确切范围(例如“A1:A20”),我更愿意查看 A 列的所有内容。但我不想使用(“A:A” ) 所以它不会查看 A 的所有行。难道没有一种方法可以只查看在 A 列中有条目的单元格吗?
非常感谢。
阿尔
【问题讨论】: