【问题标题】:VBA Object/Application defined errorVBA 对象/应用程序定义错误
【发布时间】:2014-07-16 15:56:10
【问题描述】:

这里有一些代码给我一个对象定义或应用程序定义的错误。你们能帮忙吗?提前致谢

检查匹配细胞系时出现错误。

Sub check()

Dim y, x, xb As Integer

'vertical step

For y = 12 To 65

    'check if cell have value
      If Not IsEmpty(Sheet2.Cells(y, 4)) Then

        'horizontal step
          For x = 70 To 600
              xb = x + 1
            'checks for matching cell value
              If Sheet2.Cells(6, x).Value = Sheet2.Cells(y, 4).Value Then

                'sees if the next col over after match is empty
                   If Not IsEmpty(Sheet2.Cells(y, xb)) Then

                    'if not then highlight cell in col d
                     Sheet2.Cells(y, 4).Interior.ColorIndex = 3


                    End If

               End If

          Next x

      End If

    Next y

End Sub

【问题讨论】:

    标签: vba if-statement for-loop


    【解决方案1】:

    您需要定义Sheet2。您可以通过在代码顶部声明它来做到这一点:

    Dim Sheet2 As Worksheet
    Set Sheet2 = ThisWorkbook.Worksheets("Sheet2")
    

    从那里您可以使用With Sheet2 像这样使用它:

    Sub check()
    
    Dim y, x, xb As Integer
    Dim Sheet2 As Worksheet
    
    Set Sheet2 = ThisWorkbook.Worksheets("Sheet2")
    
    'vertical step
    
    With Sheet2
    
        For y = 12 To 65
    
            'check if cell have value
              If Not IsEmpty(.Cells(y, 4)) Then
    
                'horizontal step
                  For x = 70 To 600
    
                      xb = x + 1
    
                    'checks for matching cell value
                      If .Cells(6, x).Value = .Cells(y, 4).Value Then
    
                        'sees if the next col over after match is empty
                           If Not IsEmpty(.Cells(y, xb)) Then
    
                            'if not then highlight cell in col d
                             .Cells(y, 4).Interior.ColorIndex = 3
    
                            End If
    
                       End If
    
                  Next x
    
            End If
    
        Next y
    
    End With
    
    End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多