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