PDA

View Full Version : Store Value in A temporary Variable



jo15765
06-12-2012, 06:40 PM
Hello All...If I am not detailed enough here (or if anyone has any questions pertaining to this, please let me know :) )

Public Sub Trying()
Dim sup As String
Dim rName() As Variant
Dim Pre() As Variant
Dim q As String

Application.DisplayAlerts = False
sup = "Joseph"

Pre = Array("Alpha", "Omega")
rName = Array("Test1", "Test2")


If Not IsEmpty(Pre) And Not IsEmpty(rName) Then
If UBound(Pre) = UBound(rName) Then
For q = LBound(rName) To UBound(rName)
Call DailySales(CStr(rName(q)), sup, CStr(Pre(q)))
Call WeeklySalesTotal(Cstr(rName(q)), sup, CStr(Pre(q)))
Next q
End If
End If
End Sub

In DailySales in cell D17, there is a Dollar amount. I was wondering if there was a way to store that in a variable, let's say TOTAL, then once WeeklySalesTotal is opened compare the value in TOTAL to the last row with data in column G. Then once the line Next q is hit set Total = 0 or null (whatever it would be to clear a value) then when the 2nd workbook in rName is opened do the same process (store D17 in the variable TOTAL)

***One thing I thought of is I am wanting to store the value in a variable, because the 1st workbook is closed when the 2nd workbook is opened. I have seen multiple posts on how to compare two opened workbooks, but haven't been able to find anything similar to what I want to do.

CodeNinja
06-13-2012, 03:53 AM
jo15765,
You have 2 choices to store that data in a variable... a global variable (Bad choice) and passing the variable into the subroutine (Better choice). To create a global variable (It is considered bad code practice to have a lot of global variables as they are always there and you can forget to re-adjust them, etc) you would dim the variable outside of the sub at the top of your module. To pass the variable you would do so like this:

sub test passVariable()
dim str as string
str = "Hi"

call passedVar(str)
end sub

sub passedVar(s as string)
msgbox(s)
end sub

Hope this helps...

jo15765
06-13-2012, 05:35 AM
jo15765,
You have 2 choices to store that data in a variable... a global variable (Bad choice) and passing the variable into the subroutine (Better choice). To create a global variable (It is considered bad code practice to have a lot of global variables as they are always there and you can forget to re-adjust them, etc) you would dim the variable outside of the sub at the top of your module. To pass the variable you would do so like this:

sub test passVariable()
dim str as string
str = "Hi"

call passedVar(str)
end sub

sub passedVar(s as string)
msgbox(s)
end sub

Hope this helps...

I will steer away from the global variable option. One follow-up...will each subsequent workbook overwrite the value of str? Looking at your sample coding, you have str = "Hi", now let's take into account my coding with that being Test1, once Test2 is hit, would the value of D17 overwrite "Hi" with the new value?