Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > VBA / Office Integration > Excel > vector->worksheet


Reply
 
Thread Tools Display Modes
  #1  
Old 04-30-2008, 01:38 AM
tjanstad tjanstad is offline
Newcomer
 
Join Date: Apr 2008
Location: Lund, Sweden
Posts: 1
Default vector->worksheet


Hello,

I create a vector in a macro and want to send it to the worksheet. How do I do?

Sub simulete()
'simulates randomnumbers around a line y(i)=beta_0+beta_1*x(i)
Dim beta0, beta1 As Double
Dim n As Integer
Dim x(1 To 100) As Double
Dim y(1 To 100) As Double
Dim uniform As Double

Dim vRand

beta0 = Range("A2") 'intercept
beta0 = Range("B2") 'slope
n = 100
For i = 1 To n
x(i) = i
vRand = Rnd()
NormRnd = WorksheetFunction.NormSInv(vRand)
y(i) = beta0 + x(i) * beta1 + NormRnd
Next i
'Range("A3", "A103") = x(1 To 100) I wanna do something like this

'Range("B3", "B103") = y, or like this but I don't get it to work
End Sub

/Tobias
Reply With Quote
  #2  
Old 04-30-2008, 02:21 AM
Colin Legg's Avatar
Colin Legg Colin Legg is offline
Out Of Office

Retired Moderator
* Expert *
 
Join Date: Mar 2005
Location: London, UK
Posts: 3,398
Default

Hi Tobias and welcome to the forum!

Please be sure to read the posting guidelines.

Before I answer your question I just want to point out a common misconception when declaring variables in Excel VBA:
Code:
Dim beta0, beta1 As Double
That line will dimension beta0 as a variant and beta1 as a double. To declare them both as doubles you should use:
Code:
Dim beta0 as Double, beta1 as double
Now onto arrays into worksheets. If you're working with a 2-D array you can directly dump it into a worksheet without looping. However I don't see the advantage of using arrays here - can't you just use something like this (adapt it as necessary)?

Code:
Sub simulete()
    'simulates randomnumbers around a line y(i)=beta_0+beta_1*x(i)
    Dim beta0 As Double, beta1 As Double
    Dim i As Integer
    
    beta0 = Range("A2") 'intercept
    beta1 = Range("B2") 'slope
    
    For i = 1 To 100
        Range("A" & i + 2).Value = i
        Range("B" & i + 2) = beta0 + _
                                i * beta1 + _
                                WorksheetFunction.NormSInv(Rnd())
    Next i
    
End Sub
__________________
RAD Excel Blog

Last edited by Colin Legg; 04-30-2008 at 06:08 AM.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->