[英]Excel VBA deletes first cell copied into a sheet?
我有一些代碼可以將特定的單元格復制到另一張紙上,每條新記錄都接收到一行。 它似乎在處理時正確復制了所有內容,因為我可以看到正確的單元格被復制到了新的工作表中,包括A1。 但是,完成此操作后,新工作表(A1)中的第一個單元格始終為空。 不知道這里出了什么問題,如果有人有任何想法/建議會很棒嗎?
提前致謝
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
如果在宏啟動時CallCalculate
處於活動狀態,則將第一個復制的單元格寫入該工作表(覆蓋先前在其單元格A1中可能存在的所有內容),而不是BackgroundCalc
表。 因此,與宏運行之前相比, BackgroundCalc
上的單元格A1仍將保持不變(並且可能為空白)。
您的代碼的重構版本(將同時修復該錯誤)將是:
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.