单个动作只发生在循环的最后一个实例上(但循环中其余的动作按预期工作)
我有一个Excel VBAmacros在文件和工作表之间复制内容。 它所做的一件事就是将模板中的内容复制到新工作表中,然后从其他文件中填充这些工作表中的信息。 这一切都按预期工作。 什么不是预期的代码是删除从模板表转移到信息表中的button和形状。 该代码位于每个页面上的for循环中,但它只是从最后一页中删除这些形状,而将它们留在循环接触的其他页面上。 循环中的其他内容按预期影响每个页面。
我的macros的相关部分如下。 masterWB
和currentWB
是两个独立的工作簿:
For k = 1 To masterWB.Sheets.Count j = k + 1 ' Removed because irrelevant to this query: ' Copying of content from other workbooks to each sheet ' Copying of formatting to all lines with content within sheet ' All of this is still working as expected on every sheet. ' Deletes button copied from first (template) sheet currentWB.Worksheets(j).Shapes.SelectAll Selection.Delete Next k
尝试select形状可能需要先select表单。 也许尝试添加下面的currentWB.Activate和currentWB.Worksheets(j)。select行之前的currentWB.Worksheets(j).Shapes.SelectAll
我不知道为什么你通过masterWB
循环,但形状删除操作涉及currentWB
(如上面的Jeeped状态),但尝试直接删除形状,而不是select它们:
currentWB.Worksheets(j).Shapes.Delete
如果这不起作用,一个单独的循环可能:
Dim sht as worksheet For each sht in currentWB.Worksheets sht.shapes.delete next sht
另外值得注意的是 – 你正在循环表单,但用相同的数字来引用工作表。 Sheets集合包含工作表,图表,macros,表单和其他东西 – 你有这些吗?
希望这可以帮助!