Variables

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bsgodin
    New Member
    • Aug 2006
    • 1

    Variables

    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; Jul 21 '09, 08:20 PM. Reason: Email linke removed. Please do not post your email or anone else's email address. This breaks the posting guidelines.
  • sashi
    Recognized Expert Top Contributor
    • Jun 2006
    • 1749

    #2
    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
    Code:
    Option Explicit
    
    Dim Str as String
    Dim nCount as Integer
      
      Str = Trim(Me.Text1.Text)
      nCount = Val(Me.Text2.Text)
    in vb with option implicit
    Code:
    Option Implicit
    
      Str = Trim(Me.Text1.Text)
      nCount = Val(Me.Text2.Text)
    in vbscript with option explicit
    Code:
    Option Explicit
    
    Dim Str
    Dim nCount
      
      Str = Trim(Request.Form("txtName"))
      nCount = Val(Request.Form("txtCount"))
    in vbscript with option implicit
    Code:
    Option Implicit
    
      Str = Trim(Request.Form("txtName"))
      nCount = Val(Request.Form("txtCount"))

    Comment

    • Kyosuke18
      New Member
      • May 2009
      • 11

      #3
      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
      Code:
      Label 1 Caption=Start Time
      Label 2=End Time
      Label 3=Elapsed Time
      Label4=BorderStyle 1-Fixed Single
      Caption [Blank]
      Name lblStart
      Label5=BorderStyle 1-Fixed Single
      Caption [Blank]
      Name lblEnd
      Label6=BorderStyle 1-Fixed Single
      Caption [Blank]
      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:
      Code:
      Option Explicit
      Dim StartTime As Variant
      Dim EndTime As Variant
      Dim ElapsedTime As Variant
      
      
      Private Sub cmdClear_Click()
        lblStart = ""
        lblEnd = ""
        lblElapsed = ""
      End Sub
      
      Private Sub cmdEnd_Click()
        EndTime = Now
        ElapsedTime = EndTime - StartTime
        lblEnd.Caption = Format(EndTime, "hh:mm:ss")
        lblElapsed.Caption = Format(ElapsedTime, "hh:mm:ss")
      End Sub
      
      Private Sub cmdExit_Click()
        End
      End Sub
      
      Private Sub cmdStart_Click()
        StartTime = Now
        lblStart.Caption = Format(StartTime, "hh:mm:ss")
        lblEnd.Caption = ""
        lblElapsed.Caption = ""
      
      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; Jul 21 '09, 08:19 PM. Reason: Please post code in [code] [/code] tags. Added code tags.

      Comment

      • MikeTheBike
        Recognized Expert Contributor
        • Jun 2007
        • 640

        #4
        Originally posted by bsgodin
        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@en ergizer.com
        Hi

        Not withstanding previouse answer, I think this should do what you require
        Code:
        Sub YourSub()
            Dim Temp As Integer
            If ActiveCell.Row > 1 Then Temp = ActiveCell.Offset(-1, 0)
            
            MsgBox Temp
        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

        Comment

        • smartchap
          New Member
          • Dec 2007
          • 236

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

          Code:
          Private Sub cmdStart_Click()
          
              StartTime = Now
              lblStart.Caption = Format(StartTime, "hh:mm:ss")
              lblEnd.Caption = ""
              lblElapsed.Caption = ""
              cmdEnd.Enabled=True
          
          End Sub
          In Form Load event use:
          Code:
          Private Sub Form_Load()
               cmdEnd.Enabled=False
          End Sub
          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.

          Comment

          • smartchap
            New Member
            • Dec 2007
            • 236

            #6
            I think use
            Code:
            temp = Cells(5,5).Value
            It will work irrespective of current pointer position.

            Comment

            Working...