使用列号和行号(整数)vba 复制范围
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45715381/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
copying range using column and row numbers (integer) vba
提问by MyName
I've got a standard template. to counteract the fact that users add their own columns I've indexed all the columns. I made a variable for the title header and assigned a integer to this variable.
So column A is always called the string variable ColumnA
with integer variable IColumnA
. I've also got a set number of rows and the variable LRow
for last row.
我有一个标准模板。为了抵消用户添加自己的列的事实,我已对所有列进行了索引。我为标题标题创建了一个变量,并为该变量分配了一个整数。所以列 A 总是被称为ColumnA
带有整数变量的字符串变量 IColumnA
。我还有一组行数和LRow
最后一行的变量。
My question is:
我的问题是:
How can I select a column range with the integer variable?
如何使用整数变量选择列范围?
For instance range(IcolumnA, lastrow .end (xlUp)).Copy
(or a variant hereof)
例如range(IcolumnA, lastrow .end (xlUp)).Copy
(或其变体)
This doesn't work because the integers don't work with the range method (I reckon).
这不起作用,因为整数不适用于范围方法(我认为)。
Who can help me along?
谁能帮帮我?
回答by Vityata
Edit:This is a general solution of the question "How do I copy ranges". The answer is - declare a source and target and copy the source to the target:
编辑:这是“如何复制范围”问题的一般解决方案。答案是 - 声明一个源和目标并将源复制到目标:
Option Explicit
Public Sub TestMe()
Dim rngSource As Range
Dim rngTarget As Range
With Worksheets(1)
Set rngSource = .Range(.Cells(2, "A"), .Cells(100, "B"))
Set rngTarget = .Range(.Cells(2, "C"), .Cells(100, "D"))
rngSource.Copy
rngTarget.PasteSpecial xlPasteAll
Application.CutCopyMode = False
End With
End Sub
If you only want to copy the whole column, try something like this:
如果您只想复制整列,请尝试以下操作:
Sub TestMe()
Range(Columns(5), Columns(6)).Copy
Range(Columns(7), Columns(8)).PasteSpecial xlPasteAll
Application.CutCopyMode = False
End Sub
It will copy columns 5 to 6 to columns 7 to 8. Of course, instead of numbers you can put variables.
它会将第 5 到 6 列复制到第 7 到 8 列。当然,您可以放置变量而不是数字。
And this is a bit more sophisticated, with variables:
这有点复杂,带有变量:
Sub TestMe()
Dim lngCopyColumn As Long: lngCopyColumn = 2
Dim lngPasteColumn As Long: lngPasteColumn = 10
Dim lngLenC As Long: lngLenC = 3
Range(Columns(lngCopyColumn), Columns(lngCopyColumn + lngLenC)).Copy
Range(Columns(lngPasteColumn), Columns(lngPasteColumn + lngLenC)).PasteSpecial xlPasteAll
Application.CutCopyMode = False
End Sub