The Excel macro acts differently on two computers

advertisements

I have a problem with an excel file. I programmed a macro last year to select all the worksheets with green as tab color in a Workbook. Everything was working fine on every computers until yesterday. The computer that actually need to modify that file start to act differently and only select the first worksheet. I uninstalled and installed excel again but it did'nt change anything. We're running on Windows 7 Pro x64 with Office 2013 Home and business.

Here's my code :

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)

    Dim selection As Boolean
    selection = False
    For Each ws In ActiveWorkbook.Worksheets
        If ws.Tab.Color = 5296274 Then
            If selection = False Then
                ws.Select
                selection = True
            Else
                ws.Select (False)
            End If
        End If
    Next

End Sub

Thank you


try changing the variable name to something else like selection1

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)

    Dim replaceSelection As Boolean
    replaceSelection = True

    For Each ws In ActiveWorkbook.Worksheets
        If ws.Tab.Color = 5296274 Then
            ws.Select replaceSelection
            replaceSelection = False
        End If
    Next
End Sub