Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Edit

So it turns out that the Workbook_Open and Worksheet_Activate functions weren't being triggered properly because of how I'm working on this; if I flip worksheets, the arrays get initialized properly and everything works just fine.

Gah.

Leaving the question in place so others can marvel at my incompetence.

Original question

I'm working on a kind of smart checklist application using Excel 2010 because I am a masochist.

I'm attempting to return an array of strings from a function; when I examine it in the debugger, the returned array has the expected number of elements, but the contents are blank. I need someone to tell me what I'm doing wrong.

I'm declaring a set of arrays of strings at the module level (in the creatively named Module1) like so:

Private BuildTestGroup(0 To 3) As String
Private CodingStandardsGroup(0 To 16) As String
Private ConfigFilesGroup(0 To 2) As String
Private TestingGroup(0 To 1) As String
Private DeploymentGroup(0 To 0) As String

They are initialized when the worksheet is loaded with the following function in the same module:

Sub InitNameGroups()

  BuildTestGroup(0) = "CleanSource"
  BuildTestGroup(1) = "ApplyPatch"
  BuildTestGroup(2) = "BuildPatch"
  BuildTestGroup(3) = "VerifyBuild"

  CodingStandardsGroup(0) = "InputCheck"
  CodingStandardsGroup(1) = "InitializeCheck"
  ...
End Sub

Called from here:

Private Sub Workbook_Open()
  Call Module1.InitNameGroups
  Call Module1.SetCalledFromSource(False)
End Sub

In response to a click event on a checkbox in the worksheet, one of the arrays will be retrieved:

Sub CleanSource_Yes_Click()
  '
  ' Only execute this code in response to an event on the worksheet;
  ' do not execute if called from another function like UpdateGroup
  '
  If Module1.CalledFromSource = False Then  
    Dim Names() As String
    '
    ' Get the names of all button groups for the BuildTest section
    '                           
    Names = Module1.GetNames("BuildTest")
    '
    ' check Yes box, uncheck No and NA
    '
    Call Module1.UpdateGroup("CleanSource", "Yes") 
    '
    ' if all Yes checkboxes in BuildTest
    ' group have been set, set group
    ' label to Complete
    '
    Call Module1.CheckStatus("BuildTest", Names)   
  End If                                           
End Sub                                           

using the following function, again from Module1:

Function GetNames(GroupName As String) As Variant
  If GroupName = "BuildTest" Then
    GetNames = BuildTestGroup
  ElseIf GroupName = "CodingStandards" Then
    GetNames = CodingStandardsGroup
  ElseIf GroupName = "ConfigFiles" Then
    GetNames = ConfigFilesGroup
  ElseIf GroupName = "Testing" Then
    GetNames = TestingGroup
  Else
    GetNames = DeploymentGroup
  End If
End Function

The issue is that when I examine the Names array in the CleanSource_Yes_Click function, it has the expected number of elements, but all of the elements are blank (causing the CheckStatus function to fail with a runtime error). This is happening for all my checkbox handlers, and all attempts to retrieve one of the name arrays give me the same result.

My question is, why are the elements coming back blank? Am I not declaring the Names array properly in the click handler, or am I not declaring the GetNames function to return an array properly?

I'm an old C fart who only started writing VBA a few months ago when I started putting this worksheet together, and everything I know so far I've scraped from random examples on the Web.

This scheme worked once upon a time, but I've obviously tried to do something clever and wound up breaking it. I just don't know what. I've tried to find something here at SO and on the Web to explain it, but my search-fu is weak.

share|improve this question

1 Answer 1

I'm taking one shot: have you forgotten to call InitNameGroups anywhere before the mighty click?

share|improve this answer
    
Missed! now let me reproduce the issue –  Alireza Jul 17 at 23:16
    
Never mind, found the problem; Workbook_Open and Worksheet_Activate weren't being triggered properly. –  John Bode Jul 17 at 23:25
    
I'm glad you found it. Like I wasn't wrong after all ;-) –  Alireza Jul 17 at 23:26

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.