vba 用于剪切行并粘贴到另一个工作表中的 Excel 宏

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24252205/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 03:28:29  来源:igfitidea点击:

Excel Macro To Cut Rows And Paste Into Another Worksheet

excelexcel-vbaexcel-2010vba

提问by srrojas

I am trying to get a macro to cut and paste certain rows from sheet ASR to sheet LS, whenever column I is equal to LS.

我正在尝试获取一个宏,以在 I 列等于 LS 时将某些行从工作表 ASR 剪切并粘贴到工作表 LS。

Sub MoveLS()
    Dim i As Variant
    Dim endrow As Integer

    endrow = Sheets("ASR").Range("A" & Rows.Count).End(xlUp).Row
    For i = 2 To endrow
    If Cells(i, "I").Value = "LS" Then
       Cells(i, "I").EntireRow.Cut Destination:=Sheets("LS").Range("A" & Rows.Count).End(xlUp).Offset(1)
            End If
    Next

End Sub

I have been staring at different variations of this code on and off for the past 8 hours and cannot figure out what isn't working. Any tips are appreciated!

在过去的 8 小时里,我一直在盯着这段代码的不同变体,无法弄清楚什么不起作用。任何提示表示赞赏!

回答by Netloh

It is because you haven't declared your sheets. Try the following code:

那是因为你还没有声明你的床单。试试下面的代码:

Sub MoveLS()
    Dim i As Variant
    Dim endrow As Integer
    Dim ASR As Worksheet, LS As Worksheet

    Set ASR = ActiveWorkbook.Sheets("ASR")
    Set LS = ActiveWorkbook.Sheets("LS")

    endrow = ASR.Range("A" & ASR.Rows.Count).End(xlUp).Row

    For i = 2 To endrow
        If ASR.Cells(i, "I").Value = "LS" Then
           ASR.Cells(i, "I").EntireRow.Cut Destination:=LS.Range("A" & LS.Rows.Count).End(xlUp).Offset(1)
        End If
    Next
End Sub