简体   繁体   中英

Excel VBA code works on some PCs, but errors on others

I'm not fluid enough with VBA yet to determine where my code has broken.

  1. It works on both my computers, as well as my wife's.

  2. But some of those I share the file with are experiencing (Microsoft Visual Basic Error - Method 'Sheets' of object '_Global' failed)

I'm using Microsoft Office Professional Plus 2016, my wife is using MS Office 2013. No other person is using a version prior to 2013, and they have enabled Macros.

At first I thought it might have been a corrupted file, so I shared it to multiple cloud services and tested them all respectively, all were unremarkable. One individual in particular was even having an issue computing basic SUM functions within by book (they were set to "Automatic"), which was evident by my IFERROR(s) I place throughout the workbook to help identify friction points and aid in problem solving.

I've tried going through this thread . But I'm not sure if this can be applied to mine.

Is my script in VBA causing this error, and if so, how can I rectify it?

    Private Sub hideColumnsBasedOnConditionZero()
    Sheets("Flight Log").Unprotect "Password"
    FirstColumn = 6 'First Column
    LastColumn = 12 'Last Column
    For i = 6 To 12 'Lopping through each Column
    'Hide all the columns with the values as 0 in Row 1
    If Cells(1, i) = 0 And Cells(1, i) <> "" Then Columns(i).EntireColumn.Hidden = True
    Next
    FirstColumn = 13 'First Column
    LastColumn = 25 'Last Column
    For i = 13 To 25 'Lopping through each Column
    'Hide all the columns with the values as 0 in Row 1
    If Cells(1, i) = 0 And Cells(1, i) <> "" Then Columns(i).EntireColumn.Hidden = True
    Next
    Sheets("Flight Log").Protect "Password"
    End Sub
    Private Sub showColumnsBasedOnConditionBlank()
    Sheets("Flight Log").Unprotect "Password"
    FirstColumn = 6 'First Column
    LastColumn = 12 'Last Column
    For i = 6 To 12 'Looping through each Column
    'Show all the columns with a value as 0 in Row 1
    If Cells(1, i) = "" Or Cells(1, i) = 0 Then Columns(i).EntireColumn.Hidden = False
    Next
    FirstColumn = 13 'First Column
    LastColumn = 25 'Last Column
    For i = 13 To 25 'Looping through each Column
    'Show all the columns with a value as 0 in Row 1
    If Cells(1, i) = "" Or Cells(1, i) = 0 Then Columns(i).EntireColumn.Hidden = False
    Next
    Sheets("Flight Log").Protect "Password"
    End Sub
    Private Sub Worksheet_Change(ByVal Target As Range)
    Sheets("Flight Log").Unprotect "Password"
    On Error Resume Next
    Dim updateCell As Range
    Dim lastDateCell As Range
    Set updateCell = ThisWorkbook.Worksheets("Computations").Range("A43")
    Set lastDateCell = ThisWorkbook.Worksheets("Computations").Range("A45")
    If Target.Column = 16 And Target.Value >= 1 Then
           If Target.Offset(, -14) > updateCell Then
               lastDateCell = updateCell
               updateCell = Target.Offset(, -14) 'This ensures date is not overridden by lesser value
           End If
    End If
    If Target.Column = 10 And Target.Value >= 1 Then
           If Target.Offset(, -8) > updateCell Then
               lastDateCell = updateCell
               updateCell = Target.Offset(, -8) 'This ensures date is not overridden by lesser value
           End If
    End If
    On Error GoTo 0
    Sheets("Flight Log").Protect "Password"
    End Sub
    Private Sub Worksheet_Calculate()
    Sheets("Flight Log").Unprotect "Password"
        If Range("BE1").Value = "No" And Not IsEmpty(Range("BE1")) Then
            Sheets("Flight Log").Visible = xlSheetVeryHidden
        Else
            Sheets("Flight Log").Visible = xlSheetVisible
        End If
    Sheets("Flight Log").Protect "Password"
    End Sub

I know this thing is probably a horrendous mess, but I'm trying to learn this stuff in my off time!

Have you tried explicitly declaring the sheet and workbook (and really all the variables is good practice) like is recommended in the linked post? If I were to do that for the first sub, it would look like this:

Private Sub hideColumnsBasedOnConditionZero()
Dim wb As Excel.Workbook
Dim ws As Excel.Worksheet
Dim FirstColumn As Integer, LastColumn As Integer, i As Integer
Set wb = ThisWorkbook
Set ws = wb.Worksheets("Flight Log")

ws.Unprotect "Password"

FirstColumn = 6 'First Column
LastColumn = 12 'Last Column

For i = FirstColumn To LastColumn 'Looping through each Column
'Hide all the columns with the values as 0 in Row 1
    If ws.Cells(1, i) = 0 And ws.Cells(1, i) <> "" Then ws.Columns(i).EntireColumn.Hidden = True
Next

FirstColumn = 13 'First Column
LastColumn = 25 'Last Column

For i = FirstColumn To LastColumn 'Looping through each Column
'Hide all the columns with the values as 0 in Row 1
    If ws.Cells(1, i) = 0 And ws.Cells(1, i) <> "" Then ws.Columns(i).EntireColumn.Hidden = True
Next

ws.Protect "Password"

End Sub

You can apply this same principle to all the other subs explicitly declaring the variables and see if that helps.

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