如何解决第一次执行宏时,在插入新行后导致终止
我又在这里...
我有一个按钮,它从指定的工作表开始,在两个表中插入一行(将来将有六个表),但是在另一个工作表(相同的工作簿)中,并且在这些行中还插入数据,复制公式等
由于这些表可能具有一些常见的标题和/或单元格内容,因此我“查找”第一个表的标题,然后立即搜索包含文本“(不删除)”的单元格。该单元格指示该表的最后一行(所有表具有相同的格式),这是应在其中插入新行的位置。然后,将行高设置为“ 15”,开始输入新数据,依此类推。它还会复制单元格的ROW和COLumn位置(从新插入的行开始),因此在下一个查找中,该函数将在该ROW + 3之后开始并向下搜索,而不会在上一个表中重复搜索。搜索顺序是向下的。
到目前为止,效果很好,工作正常...
但是...在所有“新”执行(*)期间,在宏将新行插入第一个表中之后,在设置行高之前,它只是简单地终止了过程(就像过早的“结束子”)。
(*)如果我在错误发生后立即尝试第二,第三,第四次,那就可以了!
如果我关闭文件并重新打开,或者如果我编辑宏,无论何时执行宏... >> Pffft!
有人可以看一下代码,看看我是否忘记了什么?
代码如下:
Private Sub CommandButton7_Click()
Application.ScreenUpdating = False
Dim HEALTHY As Workbook
Dim WS1 As Worksheet
Set HEALTHY = ThisWorkbook
Set WS1 = HEALTHY.Worksheets("TEST 1")
'----- "TEST 1" is the target worksheet.
'----- "We" are positioned in another worksheet,where the button is located.
Dim PontoCell As Range
Dim CEL_ROW,CEL_COL,CONTA As Integer
CEL_ROW = 2
CEL_COL = 2
Dim DADO_BUSCA As String
'----- For testing purposes,the tables (only 2) in
'----- "TEST 1" worksheet have their titles as "MENU 1" and "MENU 2"
For CONTA = 1 To 2
DADO_BUSCA = "MENU " & Trim(Str(CONTA))
'----- Here I find the first cell in the table,'----- that contains the searched title
Set PontoCell = WS1.Cells.Find(What:="MENU " & CONTA,_
After:=Cells(CEL_ROW,CEL_COL),_
SearchDirection:=xlNext)
'----- And here I find the first cell in the table,'----- that contains the "(do not delete)" text
Set PontoCell = WS1.Cells.Find(What:="(do not delete)",_
SearchDirection:=xlNext)
'----- Now I insert a new line right on top of the "(do not delete)" line
PontoCell.EntireRow.Insert
'----- If I am executing this macro for the first time,'----- here the Macro just "forgets" to proceed and terminates.
PontoCell.Offset(-1,0).RowHeight = 15
'----- Here I store the cell's address in two variables
CEL_ROW = PontoCell.Offset(-1,0).Row
CEL_COL = PontoCell.Offset(-1,0).Column
'-----If the worksheet is such-and-such,there's a
'----- formula to be copied in the table's new line
If WS1.Name = "TEST 1" Then
WS1.Cells(CEL_ROW - 1,CEL_COL + 6).Copy
WS1.Cells(CEL_ROW - 0,CEL_COL + 6).PasteSpecial Paste:=xlPasteFormulas
End If
WS1.Cells(CEL_ROW,CEL_COL).Value = "Protocol " & CONTA
WS1.Cells(CEL_ROW,CEL_COL + 1).Value = 100 * CONTA
WS1.Cells(CEL_ROW,CEL_COL + 2).Value = 200 * CONTA
WS1.Cells(CEL_ROW,CEL_COL + 4).Value = 300 * CONTA
CEL_ROW = CEL_ROW + 3
Next CONTA
'---------------------------------------------
Application.ScreenUpdating = True
End Sub
就是这样。我无法想象是什么原因造成的。 任何帮助将不胜感激。预先感谢!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。