views:

210

answers:

1

I have an array set up

Dim managerList(1 To 50, 1 To 100) As String

what I am trying to do, is set the first, second, and third elements in the row

managerList(index,1) = tempManagerName
managerList(index,2) = tempIdeaNumber
managerList(index,3) = 1    

But get an error when I try to do that saying that the object variable is not set. I maintain index as an integer, and the value corresponds to a single manager, but I can't seem to manually set the third element. The first and second elements set correctly.

On the flip side, I have the following code that will allow for the element to be set,

For x=1 To 50
  If StrConv(tempManagerName,3) = managerList(x,1) Then
    found = x 
      For y=3 to 100
        If managerList(x,y) = "" Then                                                                       
          managerList(x,y) = tempIdeaNumber                                                                                             
            Exit for
    End If
      Next

     Exit For
  End If
Next

It spins through the array (laterally) trying to find an empty element. Ideally I would like to set the index of the element the y variable is on into the 3rd element in the row, to keep a count of how many ideas are on the row.

What is the best way to keep a count like this? Any idea why I am getting a Object variable not set error when I try to manually set the element?

+1  A: 

object variable not set means that you are trying to call methods or access properties on an un-initialized object. I don't see anything like that in the code snippets you have published, are you sure the error occurs in those lines?

A good way to pin-point errors is to include the module and line number in the error message. Add this around your subroutine to get a more detailed message:

Sub Initialize
    On Error Goto errorthrower  
    //
    // your code goes here...
    //
    Exit sub
ErrorThrower:
    Error Err, Str$(Err) & " " & Error & Chr(13) + "Module: " & Cstr( Getthreadinfo(1) ) & ", Line: " & Cstr( Erl )
End sub

(I originally found this on Ferdy Christants blog)

It's not quite clear what problem you are trying to resolve here, but it looks like you have 1..50 "managers" that can have 1..100 "ideas" ? I'd make a class for managers instead:

Class manager
    Private managername As String
    Private ideas(1 To 100) As String

    Sub new(managername As String)
            Me.managername=managername
    End Sub

    // whatever methods you need....

End Class

Then, I'd keep track of them with a list of these objects:

    Dim managerlist List As manager

    Dim key As String
    key = Strconv(tempmanagername,3)

    if not iselement(managerlist(key)) then 
        set managerlist(key) = new manager(key)
    end if

    Dim currentmanager As manager
    Set currentmanager = managerlist(key)

This is only an example to get you started, you will have to adapt this to solve your actual problem.

Anders Lindahl