简体   繁体   English

复制M列中以“ xx”开头的单元格,然后将其粘贴到另一张表中

[英]Copy cells in column M that start with “xx” and paste them into another sheet

I am trying to make a button that will find all cells in column H starting with rm and copy them from one sheet to another. 我正在尝试创建一个按钮,该按钮将查找以rm开头的H列中的所有单元格,并将它们从一张纸复制到另一张纸。

sheet2 column M would go to sheet 1 column A sheet2列M将转到工作表1列A

Dim i As Variant
Dim rng As Range

Set x = Range("m" & i)

If Left(rng.Value, 2) = "rm" Then
For Each i In x
Worksheets("scope sheet").Range("A5").Value = Range(i)
Next i
End If

this gives an error "method 'range' of Object '_global' failed." 这给出了错误“对象'_global'的方法'范围'失败”。

if i add a With statement i get "subscript out of range." 如果我添加一个With语句,我将得到“下标超出范围”。

does anyone know a work around. 有谁知道解决方法。

You need to use Left(Range, 2) and make some changes to your loop structure. 您需要使用Left(Range, 2)并对循环结构进行一些更改。 The code is currently looping from the second row down to the last used row ( lr ) 该代码当前从第二行向下循环到最后使用的行( lr

Option Explicit

Sub test()

Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Sheet2") '<-- UPDATE SHEET
Dim i As Long, LR As Long

LR = ws.Range("M" & ws.Rows.Count).End(xlUp).Row

For i = 2 To LR
    If Left(ws.Range("M" & i), 2) = "rm" Then
        ThisWorkbook.Sheets("Sheet1").Range("A" & i).Value = ws.Range("M" & i).Value
    End If
Next i

End Sub

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 VBA代码可根据条件复制特定列中的单元格并将其粘贴(值)到另一张工作表中特定列中的单元格 - VBA code to copy cells in specific column and paste (value) it to cells in specific column on another Sheet based on conditions 从一张纸复制一列中的每三个单元格,然后粘贴到另一张纸上 - Copy every three cells in a column from one sheet and paste to another sheet 我想复制一系列单元格并将它们粘贴到另一个工作表,具体取决于下拉选择并使用按钮激活 - I want to copy a range of cells and paste them to another sheet dependent on a drop down selection and activated using a button 如果行包含单词“ Hello”,则将该列中所有已填充的单元格复制到另一张纸上,并将其粘贴到“ A”列中 - If row contains word “Hello” copy all filled cells from that column to another sheet and paste it in column “A” 通过根据条件覆盖另一个工作表中的单元格来复制单元格和粘贴 - Copy cells and paste by overwriting cells in another sheet based on a criteria VBA 将一张纸中的单元格相乘并将它们粘贴到另一张纸上 - VBA Multiply Cells in One Sheet and Paste Them to Another 如何将单元格复制到最后一行并粘贴到另一张表格? - How to copy cells to last row and paste to another sheet? 满足条件时将各种单元格复制并粘贴到另一个工作表 - Copy and Paste various cells to another sheet when condition met 尝试将单元格从一张纸复制粘贴到另一张纸上? - Attempting copy paste cells from one sheet to another? Excel可以将某些单元格从一张纸复制/粘贴到另一张纸上,但要稍加改动 - Excel to copy / paste some cells from one sheet to another but with a twist
相关标签
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM