[英]Very Basic Cut/Paste VBA
Very new to VBA: VBA 非常新:
I am looking to cut data from cells C2 to the end of the column and paste that data, beginning in cell e2.我希望将单元格 C2 中的数据剪切到列的末尾并粘贴该数据,从单元格 e2 开始。
I understand this should be extremely simple, but I have tried a few things and am still stuck.我知道这应该非常简单,但是我已经尝试了一些事情并且仍然卡住了。 The data gets cut directly, but cannot get it to paste.
数据被直接剪切,但无法粘贴。
Recent attempt:最近的尝试:
Sub CutPaste_Data()
Range("c2", Range("c2").End(xlDown)).Select
Selection.Cut
Range("e2", Range("e2").End(xlDown)).Select
Selection.PasteSpecial
Since you're new, I'll attempt to coerce some good habits with my example solution:)由于您是新手,我将尝试使用我的示例解决方案来强制养成一些好习惯:)
Option Explicit
Sub CopyData()
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("Sheet1")
With ws
Dim lastRow As Long
lastRow = .Cells(.Rows.Count, 3).End(xlUp).Row
.Range(.Cells(2, 5), .Cells(lastRow, 5)).Value = .Range(.Cells(2, 3), .Cells(lastRow, 3)).Value
.Range(.Cells(2, 3), .Cells(lastRow, 3)).Value = vbNullString
End With
End Sub
Here are the good habits for your VBA code:以下是您的 VBA 代码的好习惯:
Option Explicit
Option Explicit
lastRow
) to help yourself make your code more readable.lastRow
)来帮助自己使代码更具可读性。 Yes, it's a few extra lines of code.Select
in your codeSelect
Good luck!祝你好运!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.