我想根据单元格中包含的值来定义和复制一系列单元格。
如果单元格W2包含条目" B6:B12",则代码将选择该范围并将内容复制到X列
Sub RangeSel()
Dim rng As Range
Dim Sel As String
Sel = Range("W2").Value
Set rng = Range("Sel").Copy(Range(Range("X2"), Range("X2").End(xlDown)))
End Sub
答案 0 :(得分:1)
您的描述有点神秘。那样的东西?
Sub RangeSel()
Dim rng As Range
Dim Sel As String
Sel = Range("W2").Value
Set rng = Range(Sel)
rng.Copy
Range(Range("X2"), Range("X2").End(xlDown)).PasteSpecial xlPasteAll
End Sub
答案 1 :(得分:1)
这是你在找什么?
Sub RangeSel()
Dim rng As Range
Dim Sel As String
Sel = Range("W2").Value
Set rng = Range(Sel)
rng.Copy
Range("X2").PasteSpecial
End Sub
您试图同时设置范围和副本,但无法正常工作
答案 2 :(得分:1)
如果您想复制>>粘贴(不使用PasteSpecial
)然后您可以使用1行代码执行此操作,请参阅以下内容:
Sub RangeSel()
Dim rng As Range
Dim Sel As String
Sel = Range("W2").Value
Set rng = Range(Sel)
' optional : make sure there is a valid range
If Not rng Is Nothing Then
rng.Copy Destination:=Range("X2") ' copy>>paste in 1 line, paste at column X second row
End If
End Sub