Posted to microsoft.public.excel.programming
|
|
Creating vectors of numbers
This does +10% of the number of elements specified (of a non-existent array
???) and spacing is fixed at 1. Doesn't seem to match the problem
statement.
--
Regards,
Tom Ogilvy
"keepITcool" wrote in message
...
You could try like this:
i,ve added margins on ONE sides..
not too sure how matlab does this :)
adapt to suit your needs..
it's about 50% faster then the loop,
BUT you're limited to 65636 elements.
Sub Test()
Dim x
'Start at 120, 7500 elements +10%
x = MakeArray(123, 7500, 10)
Stop
End Sub
Function MakeArray(initial, elements, margin%)
Dim n&
On Error GoTo oops
n = elements * (1 + margin / 100)
MakeArray = Evaluate("=ROW(1:" & n & ")+" & initial)
Exit Function
oops:
MakeArray = CVErr(xlErrValue)
End Function
keepITcool
< email : keepitcool chello nl (with @ and .)
< homepage: http://members.chello.nl/keepitcool
(David Stickland) wrote:
Hi,
I have recently started to try to learn VBA for Excel. I have
experience in Matlab.
I am trying to create a simple column vector. I take a user input from
my spreadsheet and then create an evenly spaced vector which starts
10% below the input and ends 10% above the input.
If anyone knows Matlab, this could be done in one line, like this:
IL = (ILi-ILi*0.1):100:(ILi+ILi*0.1)
So far I have only managed to do this in a rather ugly loop (code
below). Is this really the easiest way to do this?
Thanks,
David
------------------------------
Sub Temp_correct()
' Define variables
Dim IL(), ILi As Integer
'Get initial values from user input
ILi = Range("C5")
'Set up boundaries +/-10% for trial solutions
i = 1 'Counter
IL(i) = ILi - ILi * 0.1
Do While IL(UBound(IL)) < (ILi + ILi * 0.1)
IL(i + 1) = IL(i) + 100
i = i + 1
Loop
End Sub
|