This is my first time working with VB. I’m mostly used to working in matlab and am finding that a lot of what I take for granted in matlab must be explicitly declared in VB. Frustrating!
In particular I need to declare two array sizes s1 and s2. As you can see in the code below, s1 is the number of elements in set “n” where the variable “stratum” equals 1. s2 is the number of elements in set “n” where the variable “stratum” equals 2. Pretty straightforward.
My approach is simply to loop the stratum variable 1 through n and count these occurences; and then declare the resulting sums as constants. This would work swimmingly in matlab, but VB is not accepting s1 and s2 as constants. It won’t even display s1 and s2 when I insert a debug.print command after the loop.
I have looked through the relevant posts already made. I appreciate any advice. Thanks.
Sub TOAinput()
Const n As Integer = 648
Dim stratum(n), hybrid(n), acres(n), hhsz(n), offinc(n)
For i = 1 To n
stratum(i) = Worksheets("hhid level").Cells(i + 1, 2).Value
Next i
Dim s1 As Integer
Dim s2 As Integer
s1 = 0
s2 = 0
For i = 1 To n
If stratum(i) = 1 Then
s1 = s1 + 1
Else:
s2 = s2 + 1
End If
Next i
Dim acres1(s1), hhsz1(s1), offinc1(s1), acres2(s2), hhsz2(s2), offinc2(s2)
(...)
End Sub
This should work slightly better for you:
I would also recommend that, if possible, you declare the types on the arrays. I have assumed that they are all ints and modified the code appropriately, but this may not work in your situation (i.e. different data types).