Results 1 to 4 of 4

Thread: two different subs, same variables

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2004
    Posts
    12

    two different subs, same variables

    but i have two subs where i need to use the variables from the former sub in the one that comes later.


    i declared the variables before any sub starts:

    Public time As Integer
    Public time1 As Integer

    ....and so on......

    then:

    Public timestring As String
    Public timestring1 As String

    Then the first Sub starts:

    Public Sub DatenAnzeigen_Click()

    here i set the values for the variables:

    time=Workbooks(2).Worksheets("Zeiterfassung").Range("E9")
    time1=------------------------------------------------------.Range("E10")


    timestring=CStr(time)
    timestring1=CStr(time1)


    End Sub


    Public Sub DatenAnzeigen2()

    And here i need to use the variables declared in the former sub.


    Have i made any mistakes?

    please tell me

    thx, alex

  2. #2
    Addicted Member
    Join Date
    Aug 2002
    Location
    Luton, UK
    Posts
    178
    1. If the macro comes to an end then variable values are lost. We have to call the second routine from the first.
    2. You are using "Time" as your variable name. This would probable generate an error because VBA uses this for something else. So use something else like "MyTime" or whatever.

    Code:
    '-----------------------------------------
    Public Sub DatenAnzeigen_Click()
    'Time = THIS COULD CAUSE AN ERROR !!
    ' CODE HERE ..........
    DatenAnzeigen2  'CALL OTHER SUBROUTINE
    End Sub
    '------------------------------------------
    '-
    Public Sub DatenAnzeigen2()
    ' CODE HERE ..........
    End Sub
    '------------------------------------------
    Regards
    BrianB
    -------------------------------

  3. #3

    Thread Starter
    New Member
    Join Date
    Jul 2004
    Posts
    12
    no the variable name is not the problem because i only translated it to english, but i´m from austria and my variable is in german (zeit).....

    and i also called the other procedure....

    the problem is that there are over 20 time - variables and everytime 2 cells have the same text the value of the time-fields are added. there is no problem in the first sub but it became too long for the compiler so i had to start a new one.

    and from the 17th field on it doesn´t work anymore....and that´s where the 2nd SUB starts...

    i don´t know what to do....

  4. #4
    Addicted Member
    Join Date
    Aug 2002
    Location
    Luton, UK
    Posts
    178
    too long for the compiler
    I have been using VBA for lots of years now and not come across this problem as such. 20 variables is not very much - even if you have several times this besides.

    I would first suspect that your code module has become corrupted. Try copying your code to Notepad or another text editor, then copy & paste from there into a new workbook.

    Are you using lots of controls in worksheets from the Controls Toolbar ? These cause corruption because they contain bugs.

    Failing this, we need to investigate more. What is the overall purpose of your code ? I cannot see any reason for size problems unless you are programming a huge standalone application. It may be that a re-write is required.
    Regards
    BrianB
    -------------------------------

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width