如何在excel中使用VBA复制过去?

时间:2018-03-29 23:35:20

标签: excel vba excel-vba

Hellow,
我有复制过去代码的问题 我无法识别行中的最后一个单元格"我想要过去的#34; !?
在下一个代码中,我写了" Shet.Cells(Rows.Count," N")。结束(xlUp).row + 1",它运行良好,以防万一没有隐藏的行,除了最后一行的值总是替换自己!!

那么,每次执行Sub Copy_Past()时,我应该怎么做才能更新最后一行的值???

 Sub Copy_Past()
    Dim Shet As Worksheet
    Set Shet = ThisWorkbook.Sheets(1)
    Dim LRow As Long
    'To get the latest cell in the column "N", where I would like to paste my data.
    LRow = Shet.Cells(Rows.Count, "N").End(xlUp).row + 1

    'To make a copy form where I selected
    Selection.SpecialCells(xlCellTypeVisible).Copy ActiveSheet.Cells(LRow, "M")
    'To delete the range of data that I selected and after coping them
    Selection.SpecialCells(xlCellTypeVisible).Delete (xlShiftUp)
 End Sub

1 个答案:

答案 0 :(得分:0)

我找到了解决方案:)

我从这里带来了这个方法 https://answers.microsoft.com/en-us/msoffice/forum/msoffice_excel-msoffice_custom-mso_2007/finding-last-row-including-hidden-rows/af0d7d7c-84f1-44bf-b36a-5abc98a93fa6

Sub xlCellTypeLastCell_Example_Column()
        For LastRow = Columns("N").SpecialCells(xlCellTypeLastCell).row To 1 Step -1
           If Len(Cells(LastRow, "N").Formula) Then Exit For
        Next
        MsgBox LastRow
End Sub