[英]VBA code to copy cells in specific column and paste (value) it to cells in specific column on another Sheet based on conditions
[英]Copy cells in column M that start with “xx” and paste them into another sheet
我正在嘗試創建一個按鈕,該按鈕將查找以rm開頭的H列中的所有單元格,並將它們從一張紙復制到另一張紙。
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
這給出了錯誤“對象'_global'的方法'范圍'失敗”。
如果我添加一個With
語句,我將得到“下標超出范圍”。
有誰知道解決方法。
您需要使用Left(Range, 2)
並對循環結構進行一些更改。 該代碼當前從第二行向下循環到最后使用的行( 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.