View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
RyanH RyanH is offline
external usenet poster
 
Posts: 586
Default Make all variable values in a Array equal to 0

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