2014-06-10 3 views
0

저는 주로 C++/C# 프로그래머이고 VBA에 익숙하지 않으므로이 코드의 문제점이 무엇인지는 확실하지 않습니다. 다음과 같은 오류가 발생합니다 :개체 변수가 설정되지 않았습니다?

"Run-time error '91': Object variable or With block variable not set."

FOR 루프의 줄에 오류가 발생했습니다. 진술의 오른쪽 부분이 오류를 던지고있는 것 같습니다. 그 라인의 문제는 정확히 무엇이며 어떻게 고칠 것인가?

Option Explicit 

Private gEmployees() As Employee 

Const gLastNameStartingCell = "A4" 
Const gNamesCountCell = "A1" 

Const gNamesTab = "NamesTab" 

Function BuildEmployeeNameArray() 

    ' Declare all variables 
    Dim iNameCount As Integer 
    Dim wksActive As Object 

    ' Counter 
    Dim i As Integer 

    ' Select the sheet with all the names 
    Set wksActive = Sheets(gNamesTab) 

    ' Get the number of names on the sheet 
    iNameCount = wksActive.Range(gNamesCountCell) 

    ' Resize the Array as appropriate 
    ReDim gEmployees(0 To iNameCount - 1) 

    ' Fill out the employee list 
    For i = 0 To iNameCount - 1 
     gEmployees(i).mLastName = wksActive.Range(gLastNameStartingCell).Offset(i, 0).Value 
    Next i 

End Function 

직원은 클래스 모듈입니다 :

다음은 코드입니다. 이 파일의 관련 정보는 다음과 같습니다.

Option Explicit 
Public mLastName As String 

Private Sub Class_Initialize() 

    ' Initialize variables 
    mLastName = "" 

End Sub 

답변

2

배열에 추가 할 실제 직원 생성이 누락되었습니다.

Dim e as Employee 
' Fill out the employee list 
For i = 0 To iNameCount - 1 
    'Create a new employee each time through the loop 
    set e = new employee 
    gEmployees(i)=e 

    'set the last name appropriately 
    gEmployees(i).mLastName = wksActive.Range(gLastNameStartingCell).Offset(i, 0).Value 
Next i 

이렇게하면 문제가 해결됩니다.

이 배열은 타입을 설정하지 않습니다. 그래서 당신이 C#/C#에서 작동하도록 비슷한 구문을 얻었을지 모르겠습니다.

+0

아, 그 "설정"은 제가 놓친 것입니다 ... 나는 똑같은 자리에서 습관적으로 사용하려고 시도했지만 오류도 던졌습니다. 이것은 문자 그대로 가상 기본의 어떤 형태로 던져지고 하루 중 하나입니다, 그래서 나는 어둠 속에서 조금 주위를 느끼고 있어요. – Ishnatal

1
ReDim gEmployees(0 To iNameCount - 1) 

iNameCount 슬롯 빈 배열을 생성 - 객체가없는 경우는 mLastName 속성을 설정하지 수 있도록이 Employee 객체와 그 각 슬롯을 채워하지 않았습니다.

관련 문제