Excel VBA:在函数中引用工作表时的“应用程序定义或对象定义的错误”

时间:2014-08-21 01:12:42

标签: excel vba function formula cells

问题:

我有一个函数可以创建工作表并使用值和公式填充单元格。在这个函数createTS(创建测试表,得到它?...)中,我调用子函数来填充工作表中的某些部分。我的问题是我收到了这个错误:

"application defined or object defined error" 

每当我尝试调用以createTS作为其中一个参数的子函数时,因为它将值和公式添加到createTS正在创建的工作表中(或者实际上是createTS的输出{1}}是一张工作表。

我特意得到这一行的错误:

createTS.Cells(r_aS, c_aS).Value = "Animal #" & aNum & ":"

守则:

1)函数createTS:

这个函数中有很多代码,其中很多都是我省略的。请关注与问题相关的部分,并假设其他未使用的变量与现在无关

Function createTS(ByRef index As Integer, ByRef wBStruct As wBStruct, ByRef gcBStruct As gcBStruct) As Worksheet
    '1) set up worksheet

     '...code...

    '2) variables
    'structure, it is created earlier 
    Dim aL As aL

    With aL
        ReDim .r_aS(gcBStruct.animalNum - 1) 'this is a string array 
        ReDim .c_aS(gcBStruct.animalNum - 1) 'this is a string array
    End With

    'row/col starts
    Dim r_hdStart As Integer          'heading row start
    Dim r_bckCS As Integer            'background counts row start
    Dim r_stdCS As Integer            'standard counts row start
    Dim r_tStart As Integer           'test section row start
    Dim r_summS As Integer            'summary section row start
    Dim r_aS As Integer               'row of animal section start

    Dim c_hdStart As Integer          'heading column start
    Dim c_aS As Integer               'column of animal section start

    'block sizes (blocks are gaps in between cells of text and defined by row #)
    Dim hspR As Integer             'heading block size
    Dim bsBlock As Integer          'background-standard block size
    Dim stBlock As Integer          'standard-tests block size
    Dim aBlock As Integer           'animal block size
    Dim asBlock As Integer          'animal-summary block size
    Dim sumBlock As Integer         'summary block size

    hspR = 1
    bsBlock = 4
    stBlock = 4
    aBlock = 3
    asBlock = 6
    sumBlock = 4

    'other
    Dim bsNum As Integer
    Dim i As Integer
    Dim j As Integer
    Dim oNum As Integer

    Dim avgBck As String
    Dim soiDate As String
    Dim tcInj As String

    Dim scTempC1 As String
    Dim scTempC2 As String

    'set variables
    oNum = UBound(wBStruct.wTypeBlock(index).animalBlock(0).organName) + 1
    r_hdStart = 1
    c_hdStart = 1

    r_bckCS = r_hdStart + hspR + 1
    bsNum = 2
    r_stdCS = r_bckCS + bsNum + bsBlock + 1
    r_tStart = r_stdCS + bsNum + stBlock + 1
    r_aS = r_tStart + 1
    c_aS = c_hdStart

    '3) Make Headings
    '**********This is how I would like to set cell values and formulas in the sub function************
    'set SoiDate + LAble
    With createTS            
        .Cells(r_hdStart, c_hdStart).Value = "Start of Injection:"
        .Cells(r_hdStart, (c_hdStart + 1)).Value = gcBStruct.soiDate
        soiDate = .Cells(r_hdStart, (c_hdStart + 1)).Address(True, True)        'set soiDate cell
    End With
    'set Background Counts
    '...code...
    'set Standard Counts
   '...code...

'4) create test type section

    '4.1) create test type heading
    '...more code...

    '4.2) create all animal sections '**********I Call the sub function here************

    For j = 0 To (gcBStruct.animalNum - 1)
        aLTemp = createABlock(createTS, j, index, wBStruct, gcBStruct, soiDate, avgBck, tcInj, r_aS, c_aS, oNum)
        r_aS = r_aS + oNum + aBlock + 1
    Next j

End Function

^我理解省略了很多代码,如果没有上述变量的值,这可能无法测试。我保留了函数中设置的大多数变量用于测试目的(但是,正如您所说,我没有包含变量,这些变量是createTS的参数,因为它们来自该函数所在的整个模块)。如有必要,我可以并将为每个参数添加示例值。如果提供我提供的信息,我的错误可能会很明显。

2)子功能:

Private Function createABlock(ByRef createTS As Worksheet, ByVal index As Integer, ByVal indexOld As Integer, _
ByRef wBStruct As wBStruct, ByRef gcBStruct As gcBStruct, ByVal soiDate As String, ByVal avgBck As String, _
ByVal tcInj As String, ByVal r_aS As String, ByVal c_aS As String, ByVal oNum As Integer) As aL

'1) Variables
    ' initialize structure and structure's variables
    '...code...

    ' create variables
    Dim aNum As Integer
    '...code...

    ' set variables
    aNum = index + 1
    '...code...

 '************Error occurs below:************** 
    createTS.Cells(r_aS, c_aS).Value = "Animal #" & aNum & ":"

'...lots and lots of code...

End Function

如果您需要有关此问题的更多信息,请告诉我,但我确信这很明显,我只是错过了它。谢谢!

1 个答案:

答案 0 :(得分:0)

除非您没有发布该部分代码,否则永远不会在函数过程中为函数名赋值。由于这是一个对象,您需要使用Set关键字

e.g:

Set CreateTS = ActiveSheet