任何帮助将不胜感激
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
答案 0 :(得分:2)
一些指针:
.curCell
对象的Worksheet
方法/属性。您正在用它来寻找.Range
或.Cells
。.Cells
或实际上使用.Range
时,行引用和列引用都不能为0。Long
而不是Integer
来避免潜在的溢出。答案 1 :(得分:0)
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