[英]Excel vba get a column's range with only column's number
I need to get a range of cells from a column number k
with rows from e
to f
. 我需要从列号
k
到行从e
到f
的单元格范围。 Variables may vary. 变量可能会有所不同。
Is there a way to do so without converting column's number to a string and doing other string manipulations ? 有没有一种方法可以不将列号转换为字符串并进行其他字符串操作 ?
So far that's what I found: 到目前为止,这就是我发现的内容:
Sub testRanges()
Dim tempRange As Range
Set tempRange = ThisWorkbook.Worksheets("setup").Columns(2)
' tempRange.Select 'it works fine
Dim cell As Range
Dim i As Integer
i = 1
For Each cell In tempRange.Cells
ThisWorkbook.Worksheets("setup").Cells(i, 7).value = cell.value 'works
i = i + 1
Next
End Sub
However I don't know the syntax to make it pick only rows 5-80 但是我不知道使它仅选择5-80行的语法
Dim e as Integer;
Dim f As Integer;
Dim k As Integer;
e=5;
f=80;
k=6;
in sixth Columns(k)
. 在第六
Columns(k)
。 Any ideas? 有任何想法吗?
Example: you're interested in Cells 5 and 6 of column 2. You could use: 示例:您对第2列的单元格5和6感兴趣。可以使用:
With ThisWorkbook.Worksheets("setup")
For Each cell In .Range(.Cells(5, 2), .Cells(6, 2))
'...
Next
End With
Any number above can be replaced by a numeric variable, typically of type Long. 上面的任何数字都可以由通常为Long类型的数字变量代替。
If i understand you correct you want the range to be F5:F80.(unless you have 1R1C table format) 如果我理解您正确的话,您希望范围为F5:F80。(除非您具有1R1C表格式)
Sub testy()
Dim e As Integer
Dim f As Integer
Dim k As Integer
e = 5
f = 80
k = 6
Range(Cells(e, k), Cells(f, k)).Select
End Sub
If you have to take the intersection of rows and columns, in VBA the command for this is Intersect()
. 如果必须采用行和列的交集,则在VBA中,此命令为
Intersect()
。
Try this: 尝试这个:
Sub Test
Dim k as range
set k =intersect(Rows("3:4"),columns("E"))
k.select
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.