Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel VBA错误:激活工作表类的方法失败_Excel_Vba - Fatal编程技术网

Excel VBA错误:激活工作表类的方法失败

Excel VBA错误:激活工作表类的方法失败,excel,vba,Excel,Vba,嗨,伙计们,我有一堆代码: For Each ws In ThisWorkbook.Sheets If ws.Name = "Position calculation" Or ws.Name = "Strategies & weights" Then Else sheet_name = ws.Name Sheets(sheet_name).Visible = True ThisWo

嗨,伙计们,我有一堆代码:

  For Each ws In ThisWorkbook.Sheets
    If ws.Name = "Position calculation" Or ws.Name = "Strategies & weights" Then
    Else
        sheet_name = ws.Name
        Sheets(sheet_name).Visible = True
        ThisWorkbook.Worksheets(sheet_name).Activate
        ws.Range("A2").Select
        For Each c In Range("A2", "A1000")
            If c.Value = "" Then
                c.Activate
                searched_cell = ActiveCell.Offset(-1, 0).Address
                GoTo flag1
            End If
        Next c
每次当我试图运行一个名为“位置计算”的工作表中的代码时,我都会收到这样的错误消息

运行时错误“1004” 激活工作表类的方法失败

我无法区分为什么代码是从其他工作表运行的,但我必须完全从导致此类错误的页面运行此脚本


提前感谢您的帮助

我不明白您为什么会收到您投诉的错误,但如果您不要求,您肯定不会有问题(正如@Siddarth Rout在上面的评论中向您指出的)。在我的分析中,我发现即使删除了所有的
Select
Activate
语句,您的整个方法也有点不正常。请考虑下面的方法。

Private Sub Try()

    Dim Ws      As Worksheet
    Dim NextRow As Long
    
    For Each Ws In ThisWorkbook.Worksheets
        With Ws
            If .Name <> "Position calculation" And .Name <> "Strategies & weights" Then
                NextRow = .Cells(.Rows.Count, "A").End(xlUp).Row + 1
                If NextRow <= 1000 Then Exit For
            End If
        End With
    Next Ws
    
    NextRow = WorksheetFunction.Max(NextRow, 2)
    MsgBox Ws.Name & vbCr & "cell " & Cells(NextRow, "A").Address(0, 0)
End Sub
Private Sub Try()
将Ws设置为工作表
暗淡的下一步一样长
对于此工作簿中的每个Ws。工作表
与Ws
如果.Name“位置计算”和.Name“策略和权重”,则
NextRow=.Cells(.Rows.Count,“A”).End(xlUp).Row+1

如果下一步,您不需要使用
激活
。你必须阅读:)