[英]vba copy and paste dynamic ranges
不習慣使用 VBA。我有一個程序記錄數據系列的 1 分鍾快照並創建日志。 然后我想再拍攝日志 X 列的快照以填充其他快照列。 即 1 分鍾前、10 分鍾前、60 分鍾前等的數據。只要日志表填寫得足夠多,代碼似乎就可以正常工作。 但如果它只是 8 分鍾前,它會在嘗試復制 10 分鍾前不存在的數據時出錯。 我似乎無法弄清楚為什么這個 Sub 不像我想的那樣工作。
運行時錯誤“1004”應用程序或 object 定義的錯誤。 我認為我的 If 語句會消除這一點。
這是我的代碼:
Sub tenMinBack()
Dim rng As Range
Application.CutCopyMode = False
Application.ScreenUpdating = False
ThisWorkbook.Worksheets("Log").Select
rng = Range(Cells(1, Columns.Count).End(xlToLeft).Offset(0,-10), Cells(13,Columns.Count).End(xlToLeft).Offset(0,-10)).Select
#### So I know this rng is pointing to cells that don't exist for first 10 minutes,
#### Not sure why this If statement isn't stopping it from attempting to copy it
If Not IsEmpty(rng) Then
rng.Copy
ThisWorkBook.Worksheets("Prices").Range("D3:D15").PasteSpecial xlValues
ThisWorkBook.Worksheets("Log").Cells.EntireColumn.AutoFit
ThisWorkBook.Worksheets("Prices").Cells.EntireColumn.AutoFit
End If
ThisWorkBook.Worksheets("Prices").Select
End Sub
嘗試這個
Sub tenMinBack()
Application.CutCopyMode = False
Application.ScreenUpdating = False
ThisWorkbook.Worksheets("Log").Select
col = Cells(1, Columns.Count).End(xlToLeft).Column - 10
If col > 0 Then
Range(Cells(1, col), Cells(13, col)).Copy
ThisWorkbook.Worksheets("Prices").Range("D3:D15").PasteSpecial xlValues
ThisWorkbook.Worksheets("Log").Cells.EntireColumn.AutoFit
ThisWorkbook.Worksheets("Prices").Cells.EntireColumn.AutoFit
End If
ThisWorkbook.Worksheets("Prices").Select
End Sub
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.