复制和粘贴链接的工作表时循环卡住
Loop gets stuck when copying and pasting linked worksheet
第一个代码完美地循环 D23:J23 并更改单元格 D7 中的日期(如果可用):
Sub Print_test()
Dim i As Integer, VList As Variant, wshS As Worksheet, wbkT As Workbook, Count As Integer
Dim c As Range
For Each c In Sheets("InForceChgCalc").Range("D23:J23").Cells
If c = "Y" Then
Sheets("InForceChgCalc").Range("D7").Cells.Value = c.Offset(2, 0).Value
MsgBox "counted" & c.Offset(2, 0).Value
End If
Next c
End Sub
但是,当我更进一步,copy/paste 一个链接工作sheet 从活动工作簿到一个新创建的工作簿时,我得到一个“下标超出范围”错误。
它正确地运行了第一个日期,但随后卡在了
Sheets("InForceChgCalc").Range("D7").Cells.Value = c.Offset(2, 0).Value
符合上述错误。
这是更新后的代码片段,其中包含 copy/pasting:
For Each c In Sheets("InForceChgCalc").Range("D48:J48").Cells
If c = "Y" Then
Sheets("InForceChgCalc").Range("D7").Cells.Value = c.Offset(2, 0).Value
Sheets("InForceChangeNotice").Copy After:=wbkT.Worksheets(wbkT.Worksheets.Count) ' Copies sheet (wshS) and places it after the latest sheet
wbkT.Worksheets(wbkT.Worksheets.Count).Cells.Copy
wbkT.Worksheets(wbkT.Worksheets.Count).Cells.PasteSpecial Paste:=xlPasteValues
End If
Next c
“InForceChangeNotice”选项卡链接到“InForceChgCalc”选项卡的单元格 D7。
当 D7 中的日期更改时,我想复制“InForceChangeNotice”,将其粘贴到新工作簿中(累计添加到之前的 sheets),然后范围值 sheet.
下面是“InForceChgCalc”选项卡的图片以供参考。
只需在激活宏所在的原始工作簿的 copy/paste 代码后添加“ThisWorkbook.Activate”即可。
循环卡住了,因为 copy/paste 出现在新工作簿中,而它试图在我的原始工作簿中进行迭代,因此“下标超出范围”
For Each c In Sheets("InForceChgCalc").Range("D48:J48").Cells
If c = "Y" Then
Sheets("InForceChgCalc").Range("D7").Cells.Value = c.Offset(2, 0).Value
Sheets("InForceChangeNotice").Copy After:=wbkT.Worksheets(wbkT.Worksheets.Count) ' Copies sheet (wshS) and places it after the latest sheet
wbkT.Worksheets(wbkT.Worksheets.Count).Cells.Copy
wbkT.Worksheets(wbkT.Worksheets.Count).Cells.PasteSpecial Paste:=xlPasteValues
ThisWorkbook.Activate
End If
Next c
第一个代码完美地循环 D23:J23 并更改单元格 D7 中的日期(如果可用):
Sub Print_test()
Dim i As Integer, VList As Variant, wshS As Worksheet, wbkT As Workbook, Count As Integer
Dim c As Range
For Each c In Sheets("InForceChgCalc").Range("D23:J23").Cells
If c = "Y" Then
Sheets("InForceChgCalc").Range("D7").Cells.Value = c.Offset(2, 0).Value
MsgBox "counted" & c.Offset(2, 0).Value
End If
Next c
End Sub
但是,当我更进一步,copy/paste 一个链接工作sheet 从活动工作簿到一个新创建的工作簿时,我得到一个“下标超出范围”错误。
它正确地运行了第一个日期,但随后卡在了
Sheets("InForceChgCalc").Range("D7").Cells.Value = c.Offset(2, 0).Value
符合上述错误。
这是更新后的代码片段,其中包含 copy/pasting:
For Each c In Sheets("InForceChgCalc").Range("D48:J48").Cells
If c = "Y" Then
Sheets("InForceChgCalc").Range("D7").Cells.Value = c.Offset(2, 0).Value
Sheets("InForceChangeNotice").Copy After:=wbkT.Worksheets(wbkT.Worksheets.Count) ' Copies sheet (wshS) and places it after the latest sheet
wbkT.Worksheets(wbkT.Worksheets.Count).Cells.Copy
wbkT.Worksheets(wbkT.Worksheets.Count).Cells.PasteSpecial Paste:=xlPasteValues
End If
Next c
“InForceChangeNotice”选项卡链接到“InForceChgCalc”选项卡的单元格 D7。
当 D7 中的日期更改时,我想复制“InForceChangeNotice”,将其粘贴到新工作簿中(累计添加到之前的 sheets),然后范围值 sheet.
下面是“InForceChgCalc”选项卡的图片以供参考。
只需在激活宏所在的原始工作簿的 copy/paste 代码后添加“ThisWorkbook.Activate”即可。
循环卡住了,因为 copy/paste 出现在新工作簿中,而它试图在我的原始工作簿中进行迭代,因此“下标超出范围”
For Each c In Sheets("InForceChgCalc").Range("D48:J48").Cells
If c = "Y" Then
Sheets("InForceChgCalc").Range("D7").Cells.Value = c.Offset(2, 0).Value
Sheets("InForceChangeNotice").Copy After:=wbkT.Worksheets(wbkT.Worksheets.Count) ' Copies sheet (wshS) and places it after the latest sheet
wbkT.Worksheets(wbkT.Worksheets.Count).Cells.Copy
wbkT.Worksheets(wbkT.Worksheets.Count).Cells.PasteSpecial Paste:=xlPasteValues
ThisWorkbook.Activate
End If
Next c