LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
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
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Count occurances of equal values in two col. - array within SUMPRO John_J Excel Worksheet Functions 5 September 30th 08 02:19 PM
Clearing Variable Values in Array using For...Next RyanH Excel Programming 2 July 29th 08 04:21 PM
Make all variable in Array = Empty each click of a Button on Userf RyanH Excel Programming 5 July 28th 08 07:38 PM
Can I make array position A(12) into a variable A(12*n) ? Paul Excel Worksheet Functions 1 October 9th 06 10:37 PM
how do I make single character as a variable in an array Hazlgrnguy Excel Worksheet Functions 1 September 25th 05 08:12 AM


All times are GMT +1. The time now is 01:25 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"