Results 1 to 14 of 14

Thread: Excel Macro Variable Scope problem

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Apr 2006
    Posts
    22

    Excel Macro Variable Scope problem

    Hi

    Ive code some code:

    Option Explicit
    Sub Macro1()

    DIm Count As Integer
    Dim verticalcounter As Integer

    'ActiveSheet.Select
    ThisWorkbook.Worksheets(downloadsheetname).Select
    'do lots of stuff

    Call findaccount
    End Sub

    Sub findaccount()


    ThisWorkbook.Worksheets(accountmappingsheetname).Select

    'do some stuff

    End Sub

    Problem:

    VB editor highlights accountmappingsheetname and says:

    "variable not defined"

    I know this is due to scope deceleration of the variable. I have tried declaring it as Public and Global but when i do decleration gets highlighted and error given:

    "Invalid attribute in sub or function"

    Please help, my boss is about to poo all over me

  2. #2
    Lively Member New2vba's Avatar
    Join Date
    Sep 2005
    Location
    UK
    Posts
    95

    Re: Excel Macro Variable Scope problem

    I may be way off here (I don't even know what scope deceleration is!), but if "downloadsheetname" and "accountmappingsheetname" are the names of your worksheets, then shouldn't your code be

    ThisWorkbook.Worksheets("downloadsheetname").Select

    And

    ThisWorkbook.Worksheets("accountmappingsheetname").Select

    ?
    "Those things we must learn to do, we must learn by doing" (or hope somebody else will take pity and help out )

  3. #3
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Excel Macro Variable Scope problem

    You need to declare all your variables, preferrably at the start of your procedure.
    Add the following declartion immediately after the line where you declare verticalcounter.
    VB Code:
    1. Dim accountmappingsheetname as String
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  4. #4
    Lively Member New2vba's Avatar
    Join Date
    Sep 2005
    Location
    UK
    Posts
    95

    Re: Excel Macro Variable Scope problem

    I assume I was way off then Declan?
    "Those things we must learn to do, we must learn by doing" (or hope somebody else will take pity and help out )

  5. #5
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Excel Macro Variable Scope problem

    If they are sheet names you are absolutly correct. If they are variables then my answer is the right one.

    I didn't even think of that possibliity.
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Apr 2006
    Posts
    22

    Re: Excel Macro Variable Scope problem

    1 - they are variable names hence I havn't used ""

    2 - Sorry when I copied and pasted the code in I accidentaly deleted the 2 lines

    Dim downloadsheetname As String
    Dim accountmappingsheetname As String

    These were in there already.

    Any suggestions??

  7. #7
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Excel Macro Variable Scope problem

    Can you post the complete code?
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Apr 2006
    Posts
    22

    Re: Excel Macro Variable Scope problem

    Option Explicit
    Sub Macro1()
    'MsgBox "" & endofblock

    ' Macro1 Macro
    ' Macro written 26/04/2006 by david.taverner

    Global Count As Integer
    Dim verticalcounter As Integer
    Dim endofblock As Integer
    Dim startofblock As Integer
    Dim accountnumber
    Dim count_gl_acct_mapping_vertical As Integer
    Dim accountnumberfound As Integer

    Dim downloadsheetname As String
    Dim accountmappingsheetname As String

    downloadsheetname = "ZGROUPA downloads 25.4.2006" 'SHOULD BE COMMENTED OUT
    accountmappingsheetname = "gl acct mapping"



    Count = 1
    verticalcounter = 1
    count_gl_acct_mapping_vertical = 1

    'ActiveSheet.Select
    ThisWorkbook.Worksheets(downloadsheetname).Select

    Do While Count < 256 ' check all columns from maximum number for column output from SAP Account Number

    If Cells(1, Count).Value = "Account number" Then 'find column with account number
    Do While verticalcounter < Range("A65536").End(xlUp).Row 'find last row
    If Cells(verticalcounter, Count).Value = (Cells((verticalcounter + 1), Count).Value) Then ' check that still same block
    verticalcounter = verticalcounter + 1 ' if same block do nothing
    Else
    endofblock = verticalcounter ' if different block identify last block cell
    startofblock = verticalcounter + 1 ' and first new block cell
    verticalcounter = verticalcounter + 1
    accountnumber = Cells(startofblock, Count).Value ' gets value off account number for block
    'MsgBox "endofblock" & endofblock
    'MsgBox "startofblock" & startofblock
    MsgBox "account number" & accountnumber
    Call findaccount
    End If
    Loop
    Count = 256 'stop counter moving across columns
    Else
    Count = Count + 1
    End If

    Loop
    End Sub

    Sub findaccount()


    ThisWorkbook.Worksheets(accountmappingsheetname).Select



    Do While count_gl_acct_mapping_vertical < Range("A65536").End(xlUp).Row 'find last row

    If Cells(count_gl_acct_mapping_vertical, 1).Value = accountnumber Then
    accountnumberfound = 1
    MsgBox "account found"
    Else
    'count_gl_acct_mapping_vertical = count_gl_acct_mapping_vertical + 1
    End If
    MsgBox "counting"
    count_gl_acct_mapping_vertical = count_gl_acct_mapping_vertical + 1
    Loop

    End Sub

  9. #9
    Addicted Member
    Join Date
    Jan 2006
    Location
    Montreal, Canada
    Posts
    152

    Re: Excel Macro Variable Scope problem

    VB Code:
    1. Option Explicit
    2. Sub Macro1()
    3. 'MsgBox "" & endofblock
    4.  
    5. ' Macro1 Macro
    6. ' Macro written 26/04/2006 by david.taverner
    7.  
    8. Global Count As Integer
    9. Dim verticalcounter As Integer
    10. Dim endofblock As Integer
    11. Dim startofblock As Integer
    12. Dim accountnumber
    13. Dim count_gl_acct_mapping_vertical As Integer
    14. Dim accountnumberfound As Integer
    15.  
    16. Dim downloadsheetname As String
    17. Dim accountmappingsheetname As String
    18.  
    19. downloadsheetname = "ZGROUPA downloads 25.4.2006" 'SHOULD BE COMMENTED OUT
    20. accountmappingsheetname = "gl acct mapping"
    21.  
    22.  
    23.  
    24. Count = 1
    25. verticalcounter = 1
    26. count_gl_acct_mapping_vertical = 1
    27.  
    28. 'ActiveSheet.Select
    29. ThisWorkbook.Worksheets(downloadsheetname).Select
    30.  
    31. Do While Count < 256 ' check all columns from maximum number for column output from SAP Account Number
    32.  
    33. If Cells(1, Count).Value = "Account number" Then 'find column with account number
    34. Do While verticalcounter < Range("A65536").End(xlUp).Row 'find last row
    35. If Cells(verticalcounter, Count).Value = (Cells((verticalcounter + 1), Count).Value) Then ' check that still same block
    36. verticalcounter = verticalcounter + 1 ' if same block do nothing
    37. Else
    38. endofblock = verticalcounter ' if different block identify last block cell
    39. startofblock = verticalcounter + 1 ' and first new block cell
    40. verticalcounter = verticalcounter + 1
    41. accountnumber = Cells(startofblock, Count).Value ' gets value off account number for block
    42. 'MsgBox "endofblock" & endofblock
    43. 'MsgBox "startofblock" & startofblock
    44. MsgBox "account number" & accountnumber
    45. Call findaccount
    46. End If
    47. Loop
    48. Count = 256 'stop counter moving across columns
    49. Else
    50. Count = Count + 1
    51. End If
    52.  
    53. Loop
    54. End Sub
    55.  
    56. Sub findaccount()
    57.  
    58.  
    59. ThisWorkbook.Worksheets(accountmappingsheetname).Select
    60.  
    61.  
    62.  
    63. Do While count_gl_acct_mapping_vertical < Range("A65536").End(xlUp).Row 'find last row
    64.  
    65. If Cells(count_gl_acct_mapping_vertical, 1).Value = accountnumber Then
    66. accountnumberfound = 1
    67. MsgBox "account found"
    68. Else
    69. 'count_gl_acct_mapping_vertical = count_gl_acct_mapping_vertical + 1
    70. End If
    71. MsgBox "counting"
    72. count_gl_acct_mapping_vertical = count_gl_acct_mapping_vertical + 1
    73. Loop
    74.  
    75. End Sub


    Way easier to read with VBCODE tags

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Apr 2006
    Posts
    22

    Re: Excel Macro Variable Scope problem

    how do I add visual basic code tags

  11. #11
    Addicted Member
    Join Date
    Jan 2006
    Location
    Montreal, Canada
    Posts
    152

    Re: Excel Macro Variable Scope problem

    [VBCADE]insert your code between theses tags[/VBCADE]

    and replace A by O VBCADE = VBCODE

  12. #12

    Thread Starter
    Junior Member
    Join Date
    Apr 2006
    Posts
    22

    Re: Excel Macro Variable Scope problem

    Anyone any ideas on whats wrong with the code?

  13. #13
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Excel Macro Variable Scope problem

    The problem, at least one of the problems, is that you are using variables in the findaccount procedure that have not been scoped within that procedure.

    If you need to use the sames variables and have them mean the same thing across multiple procedures within a module then you need to declare them outside of the procedures.
    Move the declaration of all the vairables that are used by both procedures outside of module 1.
    VB Code:
    1. Option Explicit
    2.  
    3. Dim accountmappingsheetname As String
    4. Dim count_gl_acct_mapping_vertical As Integer
    5. Dim accountnumber
    6. Dim accountnumberfound As Integer
    7.  
    8. Sub Macro1()
    9. 'MsgBox "" & endofblock
    10.  
    11. ' Macro1 Macro
    12. ' Macro written 26/04/2006 by david.taverner
    13.  
    14. Dim downloadsheetname As String
    15. Dim Count As Integer
    16. Dim verticalcounter As Integer
    17. Dim endofblock As Integer
    18. Dim startofblock As Integer
    19. 'rest of code continues here....
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  14. #14

    Thread Starter
    Junior Member
    Join Date
    Apr 2006
    Posts
    22

    Re: Excel Macro Variable Scope problem

    nice one kenny,

    how stupid of me, always the silly mistakes that I cant spot

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