对象'_Worksheet'的方法'范围'失败

时间:2017-08-02 09:30:00

标签: excel vba excel-vba

问题:

变量无法正确存储。运行代码后,我使用调试器找出错误。变量setRow不存储任何值。因此,抛出一个错误。

代码:

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语句进行测试。因此,您可以看到声明为row的变量,但它与setRow相同。

任何想法为什么?

2 个答案:

答案 0 :(得分:3)

您的Select Case语句语法错误,您也无法输入setRow

您的Select Case个陈述之一应该是什么样的示例(包括rowsetRow的更正):

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

答案 1 :(得分:1)

将以下内容添加到您的代码中:

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

停止后,请查看即时窗口,查看基于ws2.Range("A" & index) setRow的值function addOwnership(Request $request) { $id = $request->get('land_id'); $land = $this->getDoctrine()->getRepository('DamanBundleCoreBundle:Land')->find($id); $ownership = new OwnershipHistory(); $ownership_form = $this->createForm('Daman\Bundle\CoreBundle\Form\OwnershipType', $ownership); $ownership_form->handleRequest($request); if ($ownership_form->isSubmitted()) { if ($ownership_form->isValid()) { $em = $this->getDoctrine()->getManager(); $ownership->setLandId($land); $em->persist($ownership); $em->flush(); $response = array('success' => true, 'msg' => $this->get('translator')->trans('data_has_been_successfully_added'), 'id' => $ownership->getId(), 'action' => 'refresh' ); } else { $errors = $this->get('form_serializer')->serializeFormErrors($ownership_form, true, false); $response = array('success' => false, 'msg' => $this->get('translator')->trans('error_exist_in_the_form'), 'errors' => $errors); } $jsonResponse = new JsonResponse($response); return $jsonResponse; } }//end function