Excel VBA删除复制到工作表中的第一个单元格?

我有一些代码复制特定单元格到另一个工作表,每个新logging接收一个新的行。 它看起来正确地复制正在处理的一切,因为我可以看到正在复制到新工作表(包括A1)的单元格。 但是,完成后,新工作表(A1)中的第一个单元格始终为空。 不知道这里出了什么问题,如果有人有任何想法/build议,会很好吗?

提前致谢

Dim Countrows As Integer Dim k As Integer Dim serialid As String Dim NextRow As Range Dim backgrounddate As Date Countrows = 0 'find last row in spreadsheet, For k = 2 To Lastrow If IsEmpty(CallCalculate.Cells(k, 8)) = False Then Set NextRow = Range("A" & Countrows + 1) CallCalculate.Cells(k, 2).Copy BackgroundCalc.Activate NextRow.PasteSpecial Paste:=xlValues, Transpose:=False serialid = CallCalculate.Cells(k, 2) 'add date Set NextRow = Range("B" & Countrows + 1) CallCalculate.Cells(k, 9).Copy BackgroundCalc.Activate NextRow.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Transpose:=False backgrounddate = CallCalculate.Cells(k, 9) Set NextRow = Nothing Countrows = Countrows + 1 End If Next k 

如果macros启动时CallCalculate处于活动状态, CallCalculate将第一个复制的单元格写入该表格(覆盖之前可能在单元格A1中的任何内容),而不是写入BackgroundCalc表格。 因此, BackgroundCalc上的单元格A1在macros运行之前仍然保持不变(这可能是空的)。

你的代码的重构版本(可以同时修复这个bug)是:

 Dim Countrows As Integer Dim k As Integer Dim serialid As String Dim backgrounddate As Date Countrows = 0 'find last row in spreadsheet, For k = 2 To Lastrow If Not IsEmpty(CallCalculate.Cells(k, "H")) Then Countrows = Countrows + 1 serialid = CallCalculate.Cells(k, "B").Value BackgroundCalc.Cells(Countrows, "A").Value = serialid 'add date backgrounddate = CDate(CallCalculate.Cells(k, "I").Value) BackgroundCalc.Cells(Countrows, "B").NumberFormat = CallCalculate.Cells(k, "I").NumberFormat BackgroundCalc.Cells(Countrows, "B").Value = backgrounddate End If Next k