Excel 打开xlsm文件时在所有工作表中自动运行vba宏

Excel 打开xlsm文件时在所有工作表中自动运行vba宏,excel,vba,Excel,Vba,为什么不在所有工作表中自动运行此vba宏 Private Sub Workbook_Open() Dim cRow As Long Dim rRow As Range Dim LastRow As Long Dim ws As Worksheet For Each ws In ThisWorkbook.Worksheets With ws LastRow = [A65000].End(xlUp).Row For cRow = 1 To LastRow

为什么不在所有工作表中自动运行此vba宏

Private Sub Workbook_Open()
Dim cRow As Long
Dim rRow As Range
Dim LastRow As Long
Dim ws As Worksheet

For Each ws In ThisWorkbook.Worksheets
    With ws
        LastRow = [A65000].End(xlUp).Row
        For cRow = 1 To LastRow
            If Cells(cRow, 15) = "OnGoing" Then
                Rows(cRow).Font.Bold = True
                Rows(cRow).Font.Color = RGB(156, 204, 0)
            End If
            If Cells(cRow, 15) = "Modified" Then
                Rows(cRow).Font.Bold = True
            End If
        Next cRow
        Columns("A:O").EntireColumn.AutoFit
    End With
Next ws
End Sub
我做错了什么?

创建一个名为auto_open的公共子组件,以便在打开xlsm工作簿时运行代码-您构建的内容似乎与MS文档一致,但在项目模块中auto_open始终可以正常工作

如果正在触发模块,请将msgbox放入以进行验证,可能是因为您没有使用完全限定的范围/单元格名称,因此需要。在单元格和行的前面


在进行计算之前,您可以尝试激活工作表,因此您的代码应该如下所示

Private Sub Workbook_Open()
Dim cRow As Long
Dim rRow As Range
Dim LastRow As Long
Dim ws As Worksheet

For Each ws In ThisWorkbook.Worksheets
    With ws
        .activate
        LastRow = [A65000].End(xlUp).Row
        For cRow = 1 To LastRow
            If Cells(cRow, 15) = "OnGoing" Then
                Rows(cRow).Font.Bold = True
                Rows(cRow).Font.Color = RGB(156, 204, 0)
            End If
            If Cells(cRow, 15) = "Modified" Then
                Rows(cRow).Font.Bold = True
            End If
        Next cRow
        Columns("A:O").EntireColumn.AutoFit
    End With
Next ws
End Sub

@自由流你是说这个吗

Private Sub Workbook_Open()
Dim cRow As Long
Dim rRow As Range
Dim LastRow As Long
Dim ws As Worksheet

For Each ws In ThisWorkbook.Worksheets
    With ws
        LastRow = ws.Cells(ws.Cells.Rows.Count, 1).End(xlUp).Row
        For cRow = 1 To LastRow
            If ws.Cells(cRow, 15) = "OnGoing" Then
                ws.Rows(cRow).Font.Bold = True
                ws.Rows(cRow).Font.Color = RGB(156, 204, 0)
            End If
            If ws.Cells(cRow, 15) = "Modified" Then
                ws.Rows(cRow).Font.Bold = True
            End If
        Next cRow
        ws.Columns("A:O").EntireColumn.AutoFit
    End With
Next ws
End Sub

它仍在activeworkingsheet上工作。

此行LastRow=[A65000]。EndxlUp.Row将更具弹性,它将被写为ws.cellsws.cells.rows.count,1.EndxlUp.Row。您使用了“With ws”,这表明您希望将ws用作限定引用。但是,您没有在单元格、行和列方法前面添加“.”。因此,单元格、行和列方法将只引用ActiveWorksheet。您应该改为使用.Cell、.Rows和.Columns,即ws.Rows、ws.Cells和ws.Columns,这是@freeflow提到的两个链接,这是解决问题的一个非常糟糕的方法。正确的解决方案是在我对原始帖子的评论中显示的前缀为“.”。我同意@freeflow:你可能希望看到它已经正常工作了。谢谢大家。
Private Sub Workbook_Open()
Dim cRow As Long
Dim rRow As Range
Dim LastRow As Long
Dim ws As Worksheet

For Each ws In ThisWorkbook.Worksheets
    With ws
        LastRow = ws.Cells(ws.Cells.Rows.Count, 1).End(xlUp).Row
        For cRow = 1 To LastRow
            If ws.Cells(cRow, 15) = "OnGoing" Then
                ws.Rows(cRow).Font.Bold = True
                ws.Rows(cRow).Font.Color = RGB(156, 204, 0)
            End If
            If ws.Cells(cRow, 15) = "Modified" Then
                ws.Rows(cRow).Font.Bold = True
            End If
        Next cRow
        ws.Columns("A:O").EntireColumn.AutoFit
    End With
Next ws
End Sub