Connecting Tech Pros Worldwide Help | Site Map

Variables

  #1  
Old August 10th, 2006, 09:52 PM
Newbie
 
Join Date: Aug 2006
Posts: 1
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>

Last edited by Frinavale; July 21st, 2009 at 09:20 PM. Reason: Email linke removed. Please do not post your email or anone else's email address. This breaks the posting guidelines.
  #2  
Old August 11th, 2006, 04:53 AM
sashi's Avatar
Expert
 
Join Date: Jun 2006
Location: Seremban, Malaysia
Posts: 1,630

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.  
  #3  
Old June 29th, 2009, 05:02 AM
Newbie
 
Join Date: May 2009
Posts: 11

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..:)

Last edited by Frinavale; July 21st, 2009 at 09:19 PM. Reason: Please post code in [code] [/code] tags. Added code tags.
  #4  
Old June 29th, 2009, 12:30 PM
Expert
 
Join Date: Jun 2007
Location: Derbyshire, UK
Posts: 344
Provided Answers: 5

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
  #5  
Old July 17th, 2009, 11:43 AM
smartchap's Avatar
Familiar Sight
 
Join Date: Dec 2007
Location: Lucknow, India
Posts: 194

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.
  #6  
Old July 17th, 2009, 11:48 AM
smartchap's Avatar
Familiar Sight
 
Join Date: Dec 2007
Location: Lucknow, India
Posts: 194

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
The Case Against Global Variables weaknessforcats insights 1 December 20th, 2007 06:04 AM
global variables Sandman answers 5 February 15th, 2007 12:05 AM
Are static variables useful for replacing global variables? CDMAPoster@FortuneJames.com answers 9 November 15th, 2006 03:55 AM
global Variables Michael answers 7 November 14th, 2005 04:42 PM
Variables in standard modules, single record entry wiht foreign keys Ross A. Finlayson answers 5 November 13th, 2005 09:59 AM