简体   繁体   English

Excel VBA代码可在某些PC上运行,但在其他PC上会出错

[英]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. 我对VBA还不够流畅,无法确定我的代码在哪里损坏了。

  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) 但是与我共享文件的那些文件却遇到了(Microsoft Visual Basic错误-对象'_Global'的方法'Sheets'失败)

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. 我使用的是Microsoft Office Professional Plus 2016,我的妻子使用的是MS Office2013。没有其他人使用的是2013年之前的版本,并且他们启用了宏。

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. 尤其是一个人,甚至在按书计算基本SUM函数时遇到问题(它们被设置为“自动”),这在我在整个工作簿中放置的IFERROR(以帮助识别摩擦点并帮助解决问题)中很明显。

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? 我在VBA中的脚本是否会导致此错误,如果是,该如何纠正?

    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. 您可以对所有其他明确声明变量的子应用相同的原理,看看是否有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Excel VBA功能在某些地方有效但在其他地方无效 - Excel VBA Function Works In Some Places But Not Others Excel Sheet VBA代码可在一个代码上运行,但不能在其他代码上运行 - Excel Sheet VBA code works on one but not the others 无法找出为什么相同的 Excel VBA HTML 代码适用于某些条目而不适用于其他条目 - Cannot find out why the same Excel VBA HTML code works for some entries and not for others Excel VBA代码读取一些行,并丢弃其他行 - Excel VBA code reads some rows and discards others 代码在VBA for Excel 2013中有效,但在Excel 2016中出错 - Code works in VBA for Excel 2013, but it errors in Excel 2016 VBA 代码适用于一个选项卡而不适用于其他选项卡 - VBA Code works on one tab and not others Excel工作表突然无法运行VBA代码-在“某些”其他计算机上工作-重复工作表正常 - Excel Sheet Suddenly Won't Run VBA Code - Works on 'Some' Other Machines - Duplicated Sheet Works Fine Vba Shell 调用适用于某些程序,但不适用于其他程序 - Vba Shell call works with some programs but does not work with others VBA 代码 - 如果执行其他部分,请跳过某些部分 - VBA Code - Skip some parts if some others are excecuted Excel VBA “错误”捕获一些但不是所有错误 - Excel VBA “On Error” trapping some but not all errors
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM