Excel macro is acting differently on two computers

不打扰是莪最后的温柔 提交于 2019-12-11 07:39:18

问题


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


回答1:


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



回答2:


Here's a thought. Check for differences in the VBA references that are available to Excel on the two computers.

  • Press ALT + F11 to open the VB Editor.
  • Go to Tools > References.
  • On the computer where the script does not work, click the check boxes for the references that are selected on the other computer.



回答3:


To make sure that there is more than one sheet with that tab color, and that nothing else is changing the selection, you can try this code.
It will show a message box if less than 2 sheets with that tab color are found.

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    Dim ws As Excel.Worksheet
    Dim strNames As String
    Dim wsNames() As String
    Dim wsCount As Integer

    For Each ws In ThisWorkbook.Worksheets
        If ws.Tab.Color = 5296274 Then
            strNames = strNames & ":" & ws.Name
        End If
    Next

    strNames = Mid(strNames, 2)
    wsNames = Split(strNames, ":")
    wsCount = UBound(wsNames) + 1
    If wsCount < 2 Then MsgBox strNames  ' to make sure more than one sheet 
    If wsCount > 0 Then
        ThisWorkbook.Sheets(wsNames(0)).Select
        ThisWorkbook.Sheets(wsNames).Select
    End If
End Sub



回答4:


I've got the same issue since 1 week on the same computer. Before the macros were working correctly and suddenly they started selecting only the first worksheet!!! and on the same laptop!!

It seems the (False) argument of ActiveWorkbook.Worksheets(SourceTabs(t, 2)).Select (False) is not recognized anymore

I think some windows or Office update were pushed in the meantime and they are generating the problem. This is the only thing I can think about.

I had to change the way I select the sheets to Sheets(SheetList).Select using SheetList as array with the names of the sheets I need.




回答5:


Something that worked for me -

Earlier this is how i worked :-

  • Added the macro button to my quick access toolbar
  • Clicked the button to run macro

Always worked seamlessly. Until I renamed the template

The button seems to be hard-coded into the specific excel, and remembers only the file name for which it was created

So if the previous file name doesn't exist, it won't run. And if you have the previous file version, it will open that, and run the macro on that, hence making your new input data pointless




回答6:


It's very weird because it's working on some computers but not on others. We are using VMWare workstations and the code stopped working for no reason 1 year after we created that Workbook template.



来源:https://stackoverflow.com/questions/38646418/excel-macro-is-acting-differently-on-two-computers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!