[英]Setting up a dynamic range in excel-vba
I'm basing my code off of this. 我的代码基于此。 Excel VBA - select a dynamic cell range
Excel VBA - 选择动态单元格范围
I'm trying to find the syntax to create a dynamic range. 我正在尝试找到创建动态范围的语法。 Example: I always start on D8 but the upper bound of the range is based on an int count in another cell.
示例:我总是从D8开始,但范围的上限基于另一个单元格中的int计数。 [h4]
[H4]
Dim count As Integer
count = Sheet2.Cells(8,1).Value
Set refRng = Sheet2.Range("D8:" & Cells(8, i).Address)
Is the relevant code sample. 是相关的代码示例。
I now know that Sheet2.Range("H1") doesn't return an int, it returns a variant or something? 我现在知道Sheet2.Range(“H1”)不返回一个int,它返回一个变量或什么?
I've tried a million different things and have figured out that none of them work. 我已经尝试了一百万种不同的东西,并且已经发现它们都不起作用。 There has to be a better way to set up a dynamic range.
必须有一种更好的方法来设置动态范围。
Not 100% sure what you're trying to achieve but in terms of messing around with ranges maybe this is a start: 不是100%肯定你想要实现的目标,但是在搞乱范围方面可能这是一个开始:
Option Explicit
Sub select_Range()
Dim count As Integer
count = ThisWorkbook.Worksheets("Sheet2").Range("A8").Value
Dim i As Integer
i = count
Dim refRng As Excel.Range
Set refRng = ThisWorkbook.Worksheets("Sheet2").Range("D8:D" & i)
refRng.Select
End Sub
This results in the following on Sheet2
: 这导致
Sheet2
上的以下内容:
This was originally a comment, but it is also the solution so I am adding it as an answer 这最初是一个评论,但它也是解决方案所以我将其作为答案添加
Cells(8, 1)
= "A8". Cells(8, 1)
8,1 Cells(8, 1)
=“A8”。 If you want cell H1 it would be Cells(1, 8)
or Cells(1, "H")
. 如果你想要单元格H1,它将是
Cells(1, 8)
或Cells(1, "H")
。 If you want cell H4 it would be Cells(4, 8)
or Cells(4, "H")
. 如果你想要细胞H4,它将是
Cells(4, 8)
或Cells(4, "H")
。
Alternately, just Range("H1")
or Range("H4")
或者,只是
Range("H1")
或Range("H4")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.