我对VBA还不够流畅,无法确定代码的破坏位置。
它既适用于我的电脑,也适用于我的妻子。
但我与之共享文件的其中一些正在经历(Microsoft Visual Basic错误 - 方法'表格'对象' _Global'失败)
我使用的是Microsoft Office Professional Plus 2016,我的妻子正在使用MS Office 2013.没有其他人使用2013之前的版本,并且他们启用了宏。
起初我以为它可能是一个损坏的文件,所以我将它分享给多个云服务并分别测试它们,所有这些都不起眼。特别是一个人甚至在书中计算基本SUM功能的问题(他们设置为"自动"),我在整个工作簿中放置的IFERROR可以明显帮助识别摩擦点并帮助解决问题。
我尝试过this thread。但我不确定这是否适用于我的。
我的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
我知道这件事可能是一个可怕的混乱,但我试图在我的休息时间学习这些东西!
答案 0 :(得分:0)
您是否尝试过明确声明工作表和工作簿(实际上所有变量都是良好实践),如链接帖子中推荐的那样?如果我为第一个sub做那个,它看起来像这样:
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
您可以将同样的原则应用于明确声明变量的所有其他子,并查看是否有帮助。