简体   繁体   English

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

[英]Running Automaticaly vba Macro in all worksheets when open xlsm file

Why is not working automatically this vba macro in all worksheets?为什么在所有工作表中这个 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

What am I doing wrong on it?我做错了什么?

Create a public sub called auto_open to run code on opening an xlsm workbook - what youve constructed seems consistent with MS documentation, but auto_open in a project module always works with no problems..创建一个名为auto_openpublic sub以在打开xlsm工作簿时运行代码 - 您构建的内容似乎与 MS 文档一致,但项目模块中的auto_open始终可以正常工作。

https://support.microsoft.com/en-us/office/automatically-run-a-macro-when-opening-a-workbook-1e55959b-e077-4c88-a696-c3017600db44 https://support.microsoft.com/en-us/office/automatically-run-a-macro-when-opening-a-workbook-1e55959b-e077-4c88-a696-c3017600db44

If the module is being triggered (put a msgbox in to verify) then it could be because you are not using fully qualified range/cell names, so you need .如果模块被触发(放入一个msgbox进行验证),那么可能是因为您没有使用完全限定的范围/单元格名称,所以您需要. in front of cells and rowscellsrows的前面

            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

@freeflow did you mean this one? @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

it is working on the activeworkingsheet yet.它正在处理活动工作表。

You can try activating the sheet before doing the calculation, so your code should look like您可以在进行计算之前尝试激活工作表,因此您的代码应如下所示

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM