|
-
Apr 27th, 2006, 11:59 AM
#1
Thread Starter
Junior Member
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
-
Apr 27th, 2006, 12:18 PM
#2
Lively Member
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 )
-
Apr 27th, 2006, 12:19 PM
#3
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:
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 
-
Apr 27th, 2006, 12:39 PM
#4
-
Apr 27th, 2006, 12:41 PM
#5
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 
-
Apr 27th, 2006, 02:00 PM
#6
Thread Starter
Junior Member
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??
-
Apr 27th, 2006, 02:03 PM
#7
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 
-
Apr 27th, 2006, 02:13 PM
#8
Thread Starter
Junior Member
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
-
Apr 27th, 2006, 02:22 PM
#9
Addicted Member
Re: Excel Macro Variable Scope problem
VB Code:
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
Way easier to read with VBCODE tags
-
Apr 27th, 2006, 02:24 PM
#10
Thread Starter
Junior Member
Re: Excel Macro Variable Scope problem
how do I add visual basic code tags
-
Apr 27th, 2006, 02:32 PM
#11
Addicted Member
Re: Excel Macro Variable Scope problem
[VBCADE]insert your code between theses tags[/VBCADE]
and replace A by O VBCADE = VBCODE
-
Apr 27th, 2006, 02:33 PM
#12
Thread Starter
Junior Member
Re: Excel Macro Variable Scope problem
Anyone any ideas on whats wrong with the code?
-
Apr 27th, 2006, 03:16 PM
#13
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:
Option Explicit
Dim accountmappingsheetname As String
Dim count_gl_acct_mapping_vertical As Integer
Dim accountnumber
Dim accountnumberfound As Integer
Sub Macro1()
'MsgBox "" & endofblock
' Macro1 Macro
' Macro written 26/04/2006 by david.taverner
Dim downloadsheetname As String
Dim Count As Integer
Dim verticalcounter As Integer
Dim endofblock As Integer
Dim startofblock As Integer
'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 
-
Apr 27th, 2006, 04:34 PM
#14
Thread Starter
Junior Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|