object'_Worksheet的方法'范围'失败

问题:

variables没有正确存储。 运行代码后,我使用debugging器来找出错误。 variablessetRow不存储任何值。 因此,抛出一个错误。

码:

 Public Sub initData(index As Integer) ' ws = COE Monthly Report ' ws2 = FormattedRaw Dim ws As Worksheet Set ws = ThisWorkbook.Sheets("COE Monthly Report") Dim ws2 As Worksheet Set ws2 = ThisWorkbook.Sheets("FormattedRaw") Dim setRow As Integer Dim countryRow As String countryRow = ws2.Range("E" & index) Select Case ws2.Range("A" & index) Case ws2.Range("A" & index) = "AR" Select Case countryRow Case countryRow = "Australia" setRow = 7 Case countryRow = "New Zealand" setRow = 8 Case countryRow = "Singapore" setRow = 9 Case Else Exit Sub End Select Case ws2.Range("A" & index) = "HW" Select Case countryRow Case countryRow = "Australia" row = 11 Case countryRow = "New Zealand" row = 12 Case countryRow = "Philippines" row = 13 Case countryRow = "Malaysia" row = 14 Case countryRow = "Singapore" row = 15 Case countryRow = "Thailand" row = 16 Case countryRow = "Vietnam" row = 17 Case countryRow = "Indonesia" row = 18 Case countryRow = "India" row = 19 Case countryRow = "Korea" row = 20 Case countryRow = "Taiwan" row = 21 Case Else Exit Sub End Select Case ws2.Range("A" & index) = "PBS" Select Case countryRow Case countryRow = "Australia" row = 23 Case countryRow = "New Zealand" row = 24 Case countryRow = "Philippines" row = 25 Case countryRow = "Malaysia" row = 26 Case countryRow = "Singapore" row = 27 Case countryRow = "Thailand" row = 28 Case countryRow = "Vietnam" row = 29 Case countryRow = "Indonesia" row = 30 Case countryRow = "Korea" row = 31 Case countryRow = "Taiwan" row = 32 Case Else Exit Sub End Select Case ws2.Range("A" & index) = "SWG" Select Case countryRow Case countryRow = "Australia" row = 34 Case countryRow = "New Zealand" row = 35 Case countryRow = "Philippines" row = 36 Case countryRow = "Malaysia" row = 37 Case countryRow = "Singapore" row = 38 Case countryRow = "Thailand" row = 39 Case countryRow = "Vietnam" row = 40 Case countryRow = "Indonesia" row = 41 Case countryRow = "Indonesia" row = 42 Case countryRow = "Korea" row = 43 Case countryRow = "Taiwan" row = 44 Case Else Exit Sub End Select Case ws2.Range("A" & index) = "TSS" Select Case countryRow Case countryRow = "Singapore" row = 46 Case countryRow = "Malaysia" row = 47 Case countryRow = "Vietnam" row = 48 Case countryRow = "Philippines" row = 49 Case countryRow = "Indonesia" row = 50 Case countryRow = "Thailand" row = 51 Case Else Exit Sub End Select End Select ' ~~ If control point type is 'KCFR' If ws2.Range("L" & index) = "KCFR" Then ' ~~ Populate Tested Samples KCFR in COE Monthly Updated ws.Range("D" & setRow) = ws.Range("D" & setRow) + 1 If ws2.Range("AG" & setRow) <> "0" Then ' ~~ Populate Defects in KCFR in COE Monthly Updated ws.Range("E" & setRow) = ws.Range("E" & setRow) + 1 End If ' ~~ If control point type is 'KCO' ElseIf ws2.Range("L" & index) = "KCO" Then ' ~~ Populate Tested Samples KCO in COE Monthly Updated ws.Range("G" & setRow) = ws.Range("G" & setRow) + 1 If ws2.Range("AG" & setRow) <> "0" Then ' ~~ Populate Defects in KCO in COE Monthly Updated ws.Range("H" & setRow) = ws.Range("H" & setRow) + 1 End If Else Exit Sub End If End Sub 

注意:

我只使用第一个CASE语句进行testing。 所以你可以看到声明为rowvariables,但是它和setRow

任何想法为什么?

你的Select Case语句的语法是错误的,你也不能inputsetRow

一个Select Case语句应该是什么样的例子(包括setRowrow更正):

 Select Case countryRow Case "Australia" setRow = 11 Case "New Zealand" setRow = 12 Case "Philippines" setRow = 13 Case "Malaysia" setRow = 14 Case "Singapore" setRow = 15 Case "Thailand" setRow = 16 Case "Vietnam" setRow = 17 Case "Indonesia" setRow = 18 Case "India" setRow = 19 Case "Korea" setRow = 20 Case "Taiwan" setRow = 21 Case Else Exit Sub End Select 

把下面的代码放到你的代码中:

 Debug.Print ws2.Range("A" & index) Select Case ws2.Range("A" & index) Stop Case ws2.Range("A" & index) = "AR" 

一旦你停下来,看看直接的窗口,看看基于setRow分配的ws2.Range("A" & index)的值。