[英]Range in Excel using VBA
I want to create a function MyRange as follows: 我要创建一个函数MyRange,如下所示:
Input: three numbers a,b,d 输入:三个数字a,b,d
Output: an array of values from a to b with step size of d. 输出:步长为d的从a到b的值的数组。
For example, if A1 contains 1, and B1 contains 5, and you write =MyRange(A1,B1,1) in the cell C1 (and you type Ctrl+Shift+Enter) you get: 例如,如果A1包含1,B1包含5,并且您在单元格C1中写入= MyRange(A1,B1,1)(然后键入Ctrl + Shift + Enter),则会得到:
A | B | C | D | E | F | G
-------------------------
1 | 5 | 1 | 2 | 3 | 4 | 5
Here's my trial: 这是我的审判:
Function MyRange(a, b, d)
Dim Index As Integer, Number As Integer
Dim Res() As Integer
Index = 0
For Number = a To b Step d
Res(Index) = Number
Index = Index + 1
Next Number
MyRange = Res
End Function
When I try this, Excel throws #VALUE! 当我尝试此操作时,Excel抛出#VALUE! and I don't know what's the problem..
我不知道出什么问题了。
BTW yeah, I had a similar question before that got no answer, as it was too complicated, so I'd deleted it and simplified my question here. 顺便说一句,是的,我有一个类似的问题,在此之前没有答案,因为它太复杂了,因此我将其删除并简化了我的问题。
This bit of cleanup worked for me. 清理工作对我有用。 Mainly, I adding the
ReDim Preserve
to keep adding dimensions to the array. 主要是,我添加了
ReDim Preserve
以继续向数组添加尺寸。
Function MyRange(a As Integer, b As Integer, d As Integer) As Integer()
Dim Index As Integer, Number As Integer
Dim Res() As Integer
Index = 0
For Number = a To b Step d
ReDim Preserve Res(Index)
Res(Index) = Number
Index = Index + 1
Next Number
MyRange = Res
End Function
The problem you will have is with how many columns to select over from C to enter your formula. 您将遇到的问题是要从C中选择多少列来输入公式。 This will depend on
d/b
. 这将取决于
d/b
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.