Excel宏剪切行并将其粘贴到另一个工作表 [英] Excel Macro To Cut Rows And Paste Into Another Worksheet

查看:379
本文介绍了Excel宏剪切行并将其粘贴到另一个工作表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当第I列等于LS时,我都试图获取一个宏,以将某些行从工作表ASR剪切并粘贴到工作表LS.

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.

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

在过去的8个小时里,我一直在打开和关闭此代码的各种变体,无法弄清什么不起作用.任何提示,不胜感激!

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!

推荐答案

这是因为您尚未声明工作表.尝试以下代码:

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

这篇关于Excel宏剪切行并将其粘贴到另一个工作表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆