[英]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.