2012-12-31 115 views
2

我需要爲此單元格複製/粘貼的開始位置創建一個變量,以便我可以將其用作其他數據的參考點。那可能嗎?我不確定語法。爲單元格位置創建變量

wbkCS.Worksheets("Cut Sheet").Range("S4:S2000").Copy 

With wbkVer.Worksheets("Cutsheets") 
    .Range("A" & .Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteValues 
End With 

回答

8

下面是將這些範圍保存爲Range對象的語法。

dim firstRange as Range, secondRange as Range 

set firstRange = wbkCS.Worksheets("Cut Sheet").Range("S4:S2000") 

firstRange.copy 

With wbkVer.Worksheets("Cutsheets") 
    set secondRange = .Range("A" & .Rows.Count).End(xlUp).Offset(1) 
    secondRange.PasteSpecial xlPasteValues 
End With 

如果你想第一小區的那些範圍之一,這將是這樣的:

firstRange.Cells(1,1)

+0

正是我在找什麼,謝謝先生! :) – Mike

+0

+ 1 :) @Mike:如果你希望你可以選擇這篇文章作爲你的問題的答案:) –

+0

哎呀,是的!我努力做到這一點。再次感謝! – Mike

2

你已經有答案了,但也什麼都可以很好的避免剪貼板總共有這樣的事情:

Sub WithoutClipboard() 

'define the two ranges variables 
Dim firstRange As Range 
Dim secondRange As Range 

'set the range variables to specific ranges 
Set firstRange = wbkCS.Worksheets("Cut Sheet").Range("S4:S2000") 
With wbkVer.Worksheets("Cutsheets") 
    Set secondRange = .Range("A" & .Rows.Count).End(xlUp).Offset(1) 
End With 

'resize the second range so it exactly the same size as the first range 
With firstRange 
     Set secondRange = secondRange.Resize(.Rows.Count, .Columns.Count) 
End With 

'move the data without having to use copy/paste 
secondRange.Value = firstRange.Value 

End Sub 
+0

Ooo我喜歡用.value代替;在這種情況下甚至沒有考慮過這個問題。也謝謝你! – Mike