简体   繁体   中英

Running Automaticaly vba Macro in all worksheets when open xlsm file

Why is not working automatically this vba macro in all worksheets?

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..

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 . in front of cells and rows

            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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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