![](/img/trans.png)
[英]Copying a range of cells in a column in one sheet to specified cells in a row in another
[英]VBA clear contents from cells in row if range of rows in column match range from column in another sheet
我想我很接近,但我不知道如何解決這個問題。 一切看起來都不錯。 我有數據 sht1 列“T2”和“MO123”,它匹配 Sht2 上 B 列中的第 1 行,不確定它是否抱怨 .ClearContent 或我的范圍。 謝謝你能給我的任何幫助。 我會添加代碼以防萬一您想嘗試一下。 -保羅
Sub Delete_Picklist_Rec()
Dim Sht1 As Worksheet, Sht2 As Worksheet
Dim LstRw As Long, lookup_rng As Range, x
Set Sht1 = Sheets("Pick Ticket Schedule")
Set Sht2 = Sheets("Pick Ticket (History)")
Set lookup_rng = Sht2.Range("B2:B7")
With Sht1
LstRw = .Cells(.Rows.Count, "T").End(xlUp).Row
MsgBox "LstRw:" & LstRw
For x = LstRw To 1 Step -1
If .Cells(x, 1) = lookup_rng Then
Range("J" & x & ":K" & x).ClearContents Shift:=xlUp 'Need to delete value in cell J & K for the row that matches
End If
Next x
End With
End Sub
為了有一個答案,所以每個人都知道它已經解決了:
第一個問題是Shift:=xlUp
不是一個參數ClearContents
(它沒有參數),以便消除,允許明確的工作。 或者,您可以使用.Delete
代替,然后使用Shift
參數。
其次,您沒有為Range指定工作表,因此默認情況下它使用Active Sheet 。
第三,您將單個單元格值與需要通過 find 命令或循環完成的范圍進行比較。 我使用了Find
命令,因為它通常是解決此類問題的最快解決方案。 它還具有比我使用的更多的參數,因此如果需要,您可以將它們添加到其中。 在此處查看有關 find 命令的更多信息。
這是基於我所說的解決方案:
Sub Delete_Picklist_Rec()
Dim Sht1 As Worksheet, Sht2 As Worksheet
Dim LstRw As Long, lookup_rng As Range, x As Long
Set Sht1 = Sheets("Pick Ticket Schedule")
Set Sht2 = Sheets("Pick Ticket (History)")
Set lookup_rng = Sht2.Range("B2:B7")
With Sht1
LstRw = .Cells(.Rows.Count, "T").End(xlUp).Row
MsgBox "LstRw:" & LstRw
For x = LstRw To 1 Step -1 'Doesn't need to loop backwards if just clearing but does if using Delete.
If Not lookup_rng.Find(what:=.Cells(x,1), LookIn:=xlValues, lookat:=xlWhole) Is Nothing Then
.Range("J" & x & ":K" & x).ClearContents 'Or .Delete Shift:=xlUp
End If
Next x
End With
End Sub
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.