Connecting Tech Pros Worldwide Forums | Help | Site Map

Variables

Newbie
 
Join Date: Aug 2006
Posts: 1
#1: Aug 10 '06
I am new to VB for Excel. I do have some programming training in intro VB but I am a beginner so bear with me. Here is what I need to know for now. Let's say that I have a numeric value in cell position E5. My cell pointer is in position E6. I want to create a variable that will take on the value of E5. I create a state Dim Temp as Integer where Temp is the numeric variable. In my VB script, how do I write the command that will make Temp = to the value of E5?? I have tried such commands as:

temp = "rc[-1]"
temp = range("rc[-1]")

But not working. What would be the correct statement to put in the VB editor.??

Butch Godin
<email snipped>

sashi's Avatar
Expert
 
Join Date: Jun 2006
Location: Seremban, Malaysia
Posts: 1,630
#2: Aug 11 '06

re: Variables


Hi there,

there are some major dif when it comes to vb and vbscript.. heard of eplicit and implicit before? sounds weird ah? :)

option explicit
-- every variable used within a particular scope must be declared with a proper datatype assignment

option implicit
-- variable need or need not to be declared to matching datatype assignment at all

vb vs. vbscript
-- with vb you have an option to declare or not to declare variable with proper datatype assignment

-- with vb script you still have an option, but this time proper datatype assignment is not a must

confused ah?

well.. refer to sample code segment below.. take care my fren..

in vb with option explicit
Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2.  
  3. Dim Str as String
  4. Dim nCount as Integer
  5.  
  6.   Str = Trim(Me.Text1.Text)
  7.   nCount = Val(Me.Text2.Text)
  8.  
in vb with option implicit
Expand|Select|Wrap|Line Numbers
  1. Option Implicit
  2.  
  3.   Str = Trim(Me.Text1.Text)
  4.   nCount = Val(Me.Text2.Text)
  5.  
in vbscript with option explicit
Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2.  
  3. Dim Str
  4. Dim nCount
  5.  
  6.   Str = Trim(Request.Form("txtName"))
  7.   nCount = Val(Request.Form("txtCount"))
  8.  
in vbscript with option implicit
Expand|Select|Wrap|Line Numbers
  1. Option Implicit
  2.  
  3.   Str = Trim(Request.Form("txtName"))
  4.   nCount = Val(Request.Form("txtCount"))
  5.  
Newbie
 
Join Date: May 2009
Posts: 11
#3: Jun 29 '09

re: Variables


Hi Everyone,

I'm making a stopwatch application. The Form are consisting of 4 buttons which are Start Timing, End Timing, Exit And Clear. Also it has 6 Labels. Namely
Expand|Select|Wrap|Line Numbers
  1. Label 1 Caption=Start Time
  2. Label 2=End Time
  3. Label 3=Elapsed Time
  4. Label4=BorderStyle 1-Fixed Single
  5. Caption [Blank]
  6. Name lblStart
  7. Label5=BorderStyle 1-Fixed Single
  8. Caption [Blank]
  9. Name lblEnd
  10. Label6=BorderStyle 1-Fixed Single
  11. Caption [Blank]
  12. Name lblElapsed
Now my problem is that when I click the End Timing the time is appeared without the Start Timing which is not. It should be the Start Timing first. I'm thinking of an command enabled Property and using it by If Then Else Statement.Here is the code that I did:
Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2. Dim StartTime As Variant
  3. Dim EndTime As Variant
  4. Dim ElapsedTime As Variant
  5.  
  6.  
  7. Private Sub cmdClear_Click()
  8.   lblStart = ""
  9.   lblEnd = ""
  10.   lblElapsed = ""
  11. End Sub
  12.  
  13. Private Sub cmdEnd_Click()
  14.   EndTime = Now
  15.   ElapsedTime = EndTime - StartTime
  16.   lblEnd.Caption = Format(EndTime, "hh:mm:ss")
  17.   lblElapsed.Caption = Format(ElapsedTime, "hh:mm:ss")
  18. End Sub
  19.  
  20. Private Sub cmdExit_Click()
  21.   End
  22. End Sub
  23.  
  24. Private Sub cmdStart_Click()
  25.   StartTime = Now
  26.   lblStart.Caption = Format(StartTime, "hh:mm:ss")
  27.   lblEnd.Caption = ""
  28.   lblElapsed.Caption = ""
  29.  
  30. End Sub
Any possible solutions would be appreciated. This is my second post.. I hope someone will response. Just the concepts on how it works. Thanks..:)
Expert
 
Join Date: Jun 2007
Location: Derbyshire, UK
Posts: 347
#4: Jun 29 '09

re: Variables


Quote:

Originally Posted by bsgodin View Post

I am new to VB for Excel. I do have some programming training in intro VB but I am a beginner so bear with me. Here is what I need to know for now. Let's say that I have a numeric value in cell position E5. My cell pointer is in position E6. I want to create a variable that will take on the value of E5. I create a state Dim Temp as Integer where Temp is the numeric variable. In my VB script, how do I write the command that will make Temp = to the value of E5?? I have tried such commands as:

temp = "rc[-1]"
temp = range("rc[-1]")

But not working. What would be the correct statement to put in the VB editor.??

Butch Godin
Butchs.godin@energizer.com

Hi

Not withstanding previouse answer, I think this should do what you require
Expand|Select|Wrap|Line Numbers
  1. Sub YourSub()
  2.     Dim Temp As Integer
  3.     If ActiveCell.Row > 1 Then Temp = ActiveCell.Offset(-1, 0)
  4.  
  5.     MsgBox Temp
  6. End Sub
I think a look at Excel VB Help may yeald some useful information for you in the future on this and many more issues !?


MTB
smartchap's Avatar
Familiar Sight
 
Join Date: Dec 2007
Location: Lucknow, India
Posts: 194
#5: Jul 17 '09

re: Variables


Dear Kyosuke18
In Click event of StartTime use enable event for EndTime like this:

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdStart_Click()
  2.  
  3.     StartTime = Now
  4.     lblStart.Caption = Format(StartTime, "hh:mm:ss")
  5.     lblEnd.Caption = ""
  6.     lblElapsed.Caption = ""
  7.     cmdEnd.Enabled=True
  8.  
  9. End Sub
  10.  
In Form Load event use:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Load()
  2.      cmdEnd.Enabled=False
  3. End Sub
  4.  
Now if you click cmdEnd more than once without clicking cmdStart, it will give Elapsed time from earlier Start time. IAs soon as you click cmdStart now new time will be Start Time.

Hope it is clear.
smartchap's Avatar
Familiar Sight
 
Join Date: Dec 2007
Location: Lucknow, India
Posts: 194
#6: Jul 17 '09

re: Variables


I think use
Expand|Select|Wrap|Line Numbers
  1. temp = Cells(5,5).Value
  2.  
It will work irrespective of current pointer position.
Reply