Excel VBA执行代码时不会出错,并且在逐步执行时正确执行代码,但在单独运行时则不会
首先,我知道这是一篇很长的文章,但我不知道问题出在哪里,我不想用我没有包含的代码不断更新每个人,所以我包含了所有内容。有些代码可以查看,因为它不会引起任何问题,我提到这一点是为了让它更快。非常感谢 背景:大家好!在工作中,我们有一个电子表格,用户可以选择1到2个计算器来运行一些数字,并确定公寓的成本应该是多少。我的任务是为电子表格创建SOP。我认为在不同的单元格中添加注释,然后添加application.wait是一个很好的方法,这样它们就可以留在工作簿中,而不是通过Word文档被告知和显示 问题:当我单步执行宏时,所有内容都按其应有的方式运行。然而,当我试图同时运行整个程序时,许多代码行并没有完成我认为它们应该做的事情 下面是代码的开头部分,它按预期工作Excel VBA执行代码时不会出错,并且在逐步执行时正确执行代码,但在单独运行时则不会,excel,vba,Excel,Vba,首先,我知道这是一篇很长的文章,但我不知道问题出在哪里,我不想用我没有包含的代码不断更新每个人,所以我包含了所有内容。有些代码可以查看,因为它不会引起任何问题,我提到这一点是为了让它更快。非常感谢 背景:大家好!在工作中,我们有一个电子表格,用户可以选择1到2个计算器来运行一些数字,并确定公寓的成本应该是多少。我的任务是为电子表格创建SOP。我认为在不同的单元格中添加注释,然后添加application.wait是一个很好的方法,这样它们就可以留在工作簿中,而不是通过Word文档被告知和显示 问
Sub Macro1()
Dim rng1 As Range, rng2 As Range
MsgBox "This tour will auto play and show you how to go about using the Rate Calculator. Let's begin!"
'1 Selecting a calculator
Range("A2").AddComment
Range("A2").Comment.Visible = True
Range("A2").Comment.Text Text:= _
"Start by selecting the type of rate calculator you would like to use. Do so by typing in your selection or using the drop down menu."
Range("A2").Comment.Shape.TextFrame.AutoSize = True
Application.Wait (Now + TimeValue("0:00:07"))
Range("A2").Comment.Delete
我有另一个子,在A2:A3中查找事件更改,所以我打开它以允许此宏运行
'2 After calculator selection
Application.EnableEvents = True
Range("A2").Value = "Match Lease"
Application.EnableEvents = False
Range("A2").AddComment
Range("A2").Comment.Visible = True
Range("A2").Comment.Text Text:= _
"Once we've selected an option, 'Match Lease' in this case, the appropriate calculator becomes visible. Fill in the appropriate information in the gray boxes to obtain your daily rate."
Range("A2").Comment.Shape.TextFrame.AutoSize = True
Application.Wait (Now + TimeValue("0:00:09"))
Range("A2").Comment.Delete
最后三行是它开始变得不稳定的地方。“一旦我们选择”注释出现,但在应该删除时不会删除。接下来的代码应该开始在F4中显示注释。相反,activecell转到F4,消失,然后添加注释(看不见,但可以看到红色三角形)。它等待一段时间,然后在接下来的3行中添加红色三角形,但不显示注释。下面是它没有正确执行的其余代码(包括F4部分):
使用上面的代码,我们希望添加4条(未隐藏)注释,添加和删除一张图片以显示右键单击菜单,然后删除4条注释。虽然它确实做到了(不显示注释),但它正确地触发了“相当酷”的msgbox并删除了F4:F7注释。代码的下一部分将适当地进行:
'4 Discuss helpful hints
Range("F27").AddComment
Range("F27").Comment.Visible = True
Range("F27").Comment.Text Text:= _
"Notice that when your active cell is at F27 or F28, a tip is provided to remind you to select an option in the drop down to the left of your current, active cell."
Range("F27").Comment.Shape.TextFrame.AutoSize = True
Application.Wait (Now + TimeValue("0:00:11"))
Range("F27").Comment.Delete
下一节主要是工作。它应该在D41中显示一条注释,并在两次选中和取消选中两个框之间来回切换,以帮助用户显示这些框的位置。第一个框被选中,等待一段时间,然后最终取消选中(但不是两次,第二个框永远不会得到复选标记)
最后,我们设置了两个非连续范围的并集,以显示我们在让员工知道如果其中任何一个单元格有值,则必须在能够打印之前填写“$Amount(+/=)”字段(通过beforeprint宏处理)。虽然union selection和event宏起作用,但在F102中添加“我的注释”不会起作用
'6 Explain printing won't work if table is partially filled out
With Sheets("Rate Calculator v6")
Set rng1 = Range("A102:D102")
Set rng2 = .Range("P102:Q102")
Application.Union(rng1, rng2).Select
End With
Range("F102").AddComment
Range("F102").Comment.Visible = True
Range("F102").Comment.Text Text:= _
"If any of the cells on this bottom table are filled in, Excel will not allow you to print until the '$ Amount (+/-)' field is entered."
Range("F102").Comment.Shape.TextFrame.AutoSize = True
Application.Wait (Now + TimeValue("0:00:11"))
Range("F102").Comment.Delete
如果是我的一个叫做宏的宏导致了这个问题,下面是我得到的:
Sub AutoFitAndAddSpaceToComments()
Dim rngComments As Range, cell As Range
Dim Cmt As Comment
Dim wb As Workbook
Dim ws As Worksheet
Dim Cntr As Long
ActiveSheet.Unprotect "password"
Set rngComments = Sheets("Rate Calculator v6").Range("F3:N46").SpecialCells(xlCellTypeComments)
If rngComments.Count = 0 Then MsgBox "There are no comments."
For Each cell In rngComments 'Run through all comments and...
cell.Comment.Shape.TextFrame.AutoSize = True 'autofit
Next cell
For Each cell In rngComments 'Run through all comments and...
If BottomCntr > cell.Comment.Shape.Top Then cell.Comment.Shape.Top = BottomCntr + 2 'If bottom of last comment is greater than the top of current comment, add 2 points
TopCntr = cell.Comment.Shape.Top
HeightCntr = cell.Comment.Shape.Height
BottomCntr = TopCntr + HeightCntr
'Debug.Print cell.Comment.Shape.Name; " Top:"; TopCntr; " Height:"; HeightCntr; " Bottom:"; BottomCntr
Next cell
ActiveSheet.Protect "password", "false"
End Sub
Sub ShowHideComments()
If Application.DisplayCommentIndicator = xlCommentIndicatorOnly Then
Application.DisplayCommentIndicator = xlCommentAndIndicator
Else
Application.DisplayCommentIndicator = xlCommentIndicatorOnly
End If
End Sub
我用睡眠功能代替application.wait,取得了更好的成功。谢谢你的建议 我曾经调试过这个问题,我试图记住解决方案是什么。基本上,在中断模式下运行代码与“正常”运行代码之间存在(微小的、微小的)区别。您有一些选择,您确定在中断模式下运行代码时,您从调用代码的同一页开始吗?我的第一个想法涉及Application.Wait。这可能会对实时执行与单步执行产生影响。请尝试对所有应用程序进行注释。请稍候,然后再次运行比较。@Selkie是的,我只保留一张表可见,因此我只在此处开始。实际上会有一个按钮来运行这个宏,以确保它只从正确的工作表中启动。克里斯:我认为可能是等待,所以我试着在我的一个测试中跳过它们,但只是通过移动VBA中的活动线箭头来跳过它。我会把所有的等待时间都记录下来,看看我能找到什么。谢谢你们两个!至于取出application.waits,程序运行得太快了,我无法确定到底发生了什么。我试着查找应用程序周围的一些方法。等等,发现了一些没有帮助的循环。然后我偶然发现,虽然我不太了解DoEvents除了什么都不做之外所做的事情,但我用DoEvents替换了所有的application.waitis,一些我没有看到的东西现在正在显示出来,但相反,一些曾经起作用的东西现在不起作用了。将尝试application.wait和doevents的组合。
'6 Explain printing won't work if table is partially filled out
With Sheets("Rate Calculator v6")
Set rng1 = Range("A102:D102")
Set rng2 = .Range("P102:Q102")
Application.Union(rng1, rng2).Select
End With
Range("F102").AddComment
Range("F102").Comment.Visible = True
Range("F102").Comment.Text Text:= _
"If any of the cells on this bottom table are filled in, Excel will not allow you to print until the '$ Amount (+/-)' field is entered."
Range("F102").Comment.Shape.TextFrame.AutoSize = True
Application.Wait (Now + TimeValue("0:00:11"))
Range("F102").Comment.Delete
Sub AutoFitAndAddSpaceToComments()
Dim rngComments As Range, cell As Range
Dim Cmt As Comment
Dim wb As Workbook
Dim ws As Worksheet
Dim Cntr As Long
ActiveSheet.Unprotect "password"
Set rngComments = Sheets("Rate Calculator v6").Range("F3:N46").SpecialCells(xlCellTypeComments)
If rngComments.Count = 0 Then MsgBox "There are no comments."
For Each cell In rngComments 'Run through all comments and...
cell.Comment.Shape.TextFrame.AutoSize = True 'autofit
Next cell
For Each cell In rngComments 'Run through all comments and...
If BottomCntr > cell.Comment.Shape.Top Then cell.Comment.Shape.Top = BottomCntr + 2 'If bottom of last comment is greater than the top of current comment, add 2 points
TopCntr = cell.Comment.Shape.Top
HeightCntr = cell.Comment.Shape.Height
BottomCntr = TopCntr + HeightCntr
'Debug.Print cell.Comment.Shape.Name; " Top:"; TopCntr; " Height:"; HeightCntr; " Bottom:"; BottomCntr
Next cell
ActiveSheet.Protect "password", "false"
End Sub
Sub ShowHideComments()
If Application.DisplayCommentIndicator = xlCommentIndicatorOnly Then
Application.DisplayCommentIndicator = xlCommentAndIndicator
Else
Application.DisplayCommentIndicator = xlCommentIndicatorOnly
End If
End Sub