View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Jim Thomlinson Jim Thomlinson is offline
external usenet poster
 
Posts: 5,939
Default Make all variable values in a Array equal to 0

You have 2 questions so lets run through them individually.
1. Why are Var1, var2 and var3 not being reset to 0 or empty. When you set
up your array you populate it with values from var1, var2, and var3. Note
that it is the values that go into the array and not the variables
themselves. When you reset the array values you do not reset the variables.
You will need to rethink that. Perhaps a quick expanation of your fiendish
master plan will help us to send you in the right direction.

2. Your code errors out here
Sheets("Calculator").Cells(i, 1) = myArray(i)
This is because the default index for an array to start at is 0. So the
index values of your array are 0, 1 and 2. So your code errors out becuase
you are trying to put a value in row zero. Row zero is not a valid row so...
There are 2 fixes.
a) Add 1 to i so
Sheets("Calculator").Cells(i+1, 1) = myArray(i)
b) Add Option Base 1 at the top of the module. This tells the compiler to
start indexing the arrays at 1.
--
HTH...

Jim Thomlinson


"RyanH" wrote:

I am revisiting this continueing issue I am having and I apologize to all
those who have tried to help me thus far. Here is a simplified version of my
situation.

I have a UserForm that opens with 2 Buttons and 3 CheckBoxes. If my user
opens the Userform and sets all checkboxes = True and clicks Calculate var1 =
100, var2 = 200, var3 = 300, (Note: UserForm is still open, thus varibles
values are not destroyed and variables are declared in the Declarations
section). The problem happens when my user sets CheckBox1 & 2 = True and
CheckBox3 = False, var1 = 100, var2 = 200, var3 = 300. It should be var1 =
100, var2 = 200, var3 = 0, why is this? I thought the For...Loop set the
values = 0. I also have an Error indicated below, why?

Basically, I want to make all the variable values equal to 0 at the begining
of the btnApply_Click procedure and then calculate the variable values.
Note: I don't want to use Else var1 = 0, because this would make the code
sloppy to me.

Option Explicit

Dim var1 As Single
Dim var2 As Single
Dim var3 As Single

Private Sub btnApply_Click()

Dim myArray As Variant
Dim i As Long

' array of allocated part quantities
myArray = Array(var1, var2, var3)

' resets variables
For i = LBound(myArray) To UBound(myArray)
myArray(i) = Empty
Next i

MsgBox "var1 = " & var1 & " : var1 should equal 0"
MsgBox "var2 = " & var2 & " : var2 should equal 0"
MsgBox "var3 = " & var3 & " : var3 should equal 0"

Call Calculate

' array of allocated part quantities
myArray = Array(var1, var2, var3)

MsgBox "After Calculation var1 = " & var1
MsgBox "After Calculation var2 = " & var2
MsgBox "After Calculation var3 = " & var3

' apply values to worksheet
For i = LBound(myArray) To UBound(myArray)
If Not IsEmpty(myArray(i)) Then
Error= Sheets("Calculator").Cells(i, 1) = myArray(i)
End If
Next i

End Sub

Private Sub Calculate()

If CheckBox1 = True Then
var1 = 100
End If

If CheckBox2 = True Then
var2 = 200
End If

If CheckBox3 = True Then
var3 = 300
End If

End Sub

--
Cheers,
Ryan