98.7% Of all customers recommend us, we're so confident about our results we publish all reviews and stats
View Live Stats View ReviewsForum home » Delegate support and help forum » Microsoft Excel VBA Training and help » Variable scope
Variable scope
Resolved · Medium Priority · Version 2016
Diane has attended:
Excel VBA Introduction course
Excel VBA Intermediate course
Excel VBA Advanced course
Variable scope
Hi
I created an array variable at module level and filled the array via a called sub. When the sub is finished and returns to RunMain, the array values are lost and the array shows as out of context. I then created a simple variable aB declared in the same way and that maintains the context and the value. Do arrays not work across subs in the same way?
Please see my code below
Kind regards
Diane
Private CENamesArray As Variant
Private aB As Integer
Sub RunMain()
Application.ScreenUpdating = False
CheckOutError = False
Dim WKbook As Workbook
Dim sfilename As String
'Following calls clearexisting out of date data
Call ClearOHPermData 'This will clear existing current employees data before latest current employees update
sfilename = "w:\brop_oh_perm.xlsx" 'This is the latest OpenHR Current employees for import
Set WKbook = Workbooks.Open(sfilename, ReadOnly:=True) 'open the latest OpenHR Current employees
'MsgBox "active wb is " & ActiveWorkbook.Name
Call FillCurrentEmployeesArray 'import the latest OpenHR current employees into local array
aB = aB + 500
WKbook.Close savechanges:=False 'latest OpenHR current employees
End Sub
Sub FillCurrentEmployeesArray()
'MsgBox "active wb is " & ActiveWorkbook.Name
CENamesArray = Range("b5").CurrentRegion
aB = 100
'MsgBox UBound(CENamesArray, 1) & " " & UBound(CENamesArray, 2) 'this will display count for rows then cols
End Sub
Sub ClearOHPermData()
Dim lBlankRow As Long
lBlankRow = Range("a2").CurrentRegion.Rows.Count
Range("a2", "c" & lBlankRow).ClearContents
End Sub
RE: Variable scope
Hi Diane,
Thanks for the forum question and the email you sent me.
Did I understand you right that I can just ignore this question?
Kind regards
Jens Bonde
Microsoft Office Specialist Trainer
Tel: 0207 987 3777
STL - https://www.stl-training.co.uk
98%+ recommend us
London's leader with UK wide delivery in Microsoft Office training and management training to global brands, FTSE 100, SME's and the public sector
Training information:
Welcome. Please choose your application (eg. Excel) and then post your question. Our Microsoft Qualified trainers will then respond within 24 hours (working days). Frequently Asked Questions
Any suggestions, questions or comments? Please post in the Improve the forum thread. |
Excel tip:Deleting cells, Rows & columnsplace your cursor on a cell, row number or column letter and use CTRL + -. |