获取有关应用程序定义或对象定义的错误的错误

时间:2018-12-16 00:11:25

标签: excel vba

任何帮助将不胜感激

Sub changeRange(startRange As Integer, endRange As Integer, rowNum As Integer, col As Integer)

    Select Case col
        Case 5
            ThisWorkbook.Sheets("Campus Summary #").Cells(rowNum, col).Formula = "=COUNTIF('TCP Export'!L" & startRange & ":L" & endRange & ", '>0')"
        Case 7
            Worksheets("Campus Summary #").curCell.Formula = "COUNTCase('TCP Export'!MstartRange:MendRange, '>0')"
        Case 9
            Worksheets("Campus Summary #").curCell.Formula = "COUNTCase('TCP Export'!NstartRange:NendRange, '>0')"
        Case 11
            Worksheets("Campus Summary #").curCell.Formula = "COUNTCase('TCP Export'!OstartRange:OendRange, '>0')"
        Case 13
            Worksheets("Campus Summary #").curCell.Formula = "COUNTCase('TCP Export'!PstartRange:PendRange, '>0')"
        Case 15
            Worksheets("Campus Summary #").curCell.Formula = "COUNTCase('TCP Export'!QstartRange:QendRange, '>0')"
        Case 17
            Worksheets("Campus Summary #").curCell.Formula = "COUNTCase('TCP Export'!RstartRange:RendRange, '>0')"
        Case 19
            Worksheets("Campus Summary #").curCell.Formula = "COUNTCase('TCP Export'!SstartRange:SendRange, '>0')"
        Case 21
            Worksheets("Campus Summary #").curCell.Formula = "COUNTCase('TCP Export'!TstartRange:TendRange, '>0')"
        Case 23
            Worksheets("Campus Summary #").curCell.Formula = "COUNTCase('TCP Export'!UstartRange:UendRange, '>0')"
        Case 25
            Worksheets("Campus Summary #").curCell.Formula = "COUNTCase('TCP Export'!VstartRange:VendRange, '>0')"
        Case 27
            Worksheets("Campus Summary #").curCell.Formula = "COUNTCase('TCP Export'!WstartRange:WendRange, '>0')"
    End Select

End Sub

2 个答案:

答案 0 :(得分:2)

一些指针:

  1. 我发现您的错误最初令人困惑,因为没有.curCell对象的Worksheet方法/属性。您正在用它来寻找.Range.Cells
  2. 使用.Cells或实际上使用.Range时,行引用和列引用都不能为0。
  3. 以整数形式传递变量名称的范围具有误导性。
  4. 最后,您希望使用Long而不是Integer来避免潜在的溢出。

答案 1 :(得分:0)

整rick整rick

Sub changeRange(startRange As Integer, endRange As Integer, _
    rowNum As Integer, col As Integer)

  Const cStrWs1 As String = "Campus Summary #"
  Const cStrWs2 As String = "TCP Export"
  Const cStrCase As String = "5,7,9,11,13,15,17,19,21,23,25,27"
  Const cStrColumn As String = "L,M,N,O,P,Q,R,S,T,U,V,W"
  Const cStr1 As String = "COUNTIF('"
  Const cStr2 As String = "'!"
  Const cStr3 As String = ":"
  Const cStr4 As String = "," & Chr(34) & ">0" & Chr(34) & ")"

  Dim vntCase As Variant
  Dim vntColumn As Variant

  Dim i As Integer

  vntCase = Split(cStrCase, ",")
  vntColumn = Split(cStrColumn, ",")

    For i = 0 To UBound(vntCase)
      If col = CInt(Trim(cStrCase(i))) Then
        ThisWorkbook.Worksheets(cStrWs1).Cells(rowNum, col).Formula _
            = cStr1 & cStrWs2 & cStr2 & Trim(vntColumn(i)) & startRange & _
            cStr3 & Trim(vntColumn(i)) & endRange & cStr4
        Exit For
      End If
    Next

End Sub