vba - Excel VBA : Simplify long Code

标签 vba excel

是否可以简化以下代码?有什么办法可以缩短这个:

    ' Paste last value
    Range(Cells(LastRowP + 1, 10), Cells(LastRowP + 1, 10)).Select
    Selection.Cut
    Range(Cells(LastRowP + 1, 9), Cells(LastRowP + 1, 9)).Select
    ActiveSheet.Paste
    Range(Cells(LastRowP + 2, 10), Cells(LastRowP + 2, 10)).Select
    Selection.Cut
    Range(Cells(LastRowP + 2, 9), Cells(LastRowP + 2, 9)).Select
    ActiveSheet.Paste
    Range(Cells(LastRowP + 3, 10), Cells(LastRowP + 3, 10)).Select
    Selection.Cut
    Range(Cells(LastRowP + 3, 9), Cells(LastRowP + 3, 9)).Select
    ActiveSheet.Paste

这种情况一直持续到+20。我是 VBA 和编码的新手,所以请多多包涵:)

最佳答案

Range(Cells(LastRowP + 1, 10), Cells(LastRowP + 20, 10)).Cut _
          Cells(LastRowP + 1, 9)

或者
 Cells(LastRowP + 1, 10).Resize(20,1).Cut Cells(LastRowP + 1, 9)

关于vba - Excel VBA : Simplify long Code,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19406801/

相关文章:

excel - VB6/VBA 不允许加载 COM 加载项

VBA - 打开Excel,查找和替换,删除行,另存为csv

excel - VBA 中的 LoadXML 未将字符串解析为可用的 DOMDocument60

vba - VBA 的隐藏陷阱

ms-access - Microsoft Access 2007 和 2010 : "Run-Time Error ' 42 9': ActiveX component can' t create object"

excel - VBA Excel - 在 VBA 中复制 Excel MAX 公式

excel - 书签特殊粘贴,Excel 在同一文件夹中查找

excel - excel中多次出现MAX值 - 选择相邻单元格中具有最高值的MAX值

vba - Excel VBA在一行循环中删除三重重复

excel - 不太清楚如何写出数学公式