简体   繁体   English

如果列中的行范围与另一工作表中的列的范围相匹配,则 VBA 会清除行中单元格的内容

[英]VBA clear contents from cells in row if range of rows in column match range from column in another sheet

I think I am very close but I cant figure out how to solve this issue.我想我很接近,但我不知道如何解决这个问题。 Everything looks right.一切看起来都不错。 I have data sht1 column "T2" with "MO123" Which matches 1 of row in column B on Sht2, not sure if its complaining about the .ClearContent or my range.我有数据 sht1 列“T2”和“MO123”,它匹配 Sht2 上 B 列中的第 1 行,不确定它是否抱怨 .ClearContent 或我的范围。 Thank you for any help you can give me.谢谢你能给我的任何帮助。 I will add the code just in case you want to try it out.我会添加代码以防万一您想尝试一下。 -Paul -保罗

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

在此处输入图片说明

For the sake of there being an answer so everyone knows it's solved:为了有一个答案,所以每个人都知道它已经解决了:

The first issue was Shift:=xlUp isn't a argument for ClearContents (it has no arguments) so removing that allows the clear to work.第一个问题是Shift:=xlUp不是一个参数ClearContents (它没有参数),以便消除,允许明确的工作。 Alternatively you can use .Delete instead and utilize the Shift argument then.或者,您可以使用.Delete代替,然后使用Shift参数。

Secondly you didn't specify the sheet for your Range so it by default used the Active Sheet .其次,您没有为Range指定工作表,因此默认情况下它使用Active Sheet

Thirdly, you're comparing a single cell value to a range which needs to be done either by a find command or looping.第三,您将单个单元格值与需要通过 find 命令或循环完成的范围进行比较。 I've used the Find command as it's typically the fastest solution for something like this.我使用了Find命令,因为它通常是解决此类问题的最快解决方案。 It also has more arguments than what I've used so if needed you can add them to it.它还具有比我使用的更多的参数,因此如果需要,您可以将它们添加到其中。 Check out more about the find command here. 在此处查看有关 find 命令的更多信息

Here is the solution based on what I have said:这是基于我所说的解决方案:

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.

相关问题 将一个工作表中一列中的单元格区域复制到另一工作表中一行中的指定单元格中 - Copying a range of cells in a column in one sheet to specified cells in a row in another 根据第一张工作表中列的非空单元格,将特定的行范围复制到另一张工作表的特定范围 - Copy specific row range to another sheet's specific range based on non empty cells of a column in first sheet 从Excel VBA中的一系列单元格中获取列x的单元格 - Get column x's Cells from a Range of Cells in Excel VBA 如果在vba中更改了单元格范围,则清除单元格内容 - Clear cell contents if another is changed for a range of cells in vba 如何将一系列单元格复制到VBA中的另一列? - How to copy a range of cells to another column in VBA? 从一张纸到另一张纸复制并粘贴一系列单元格,然后清除原始单元格中的数据 - Copy and paste a range of cells from one sheet to another then clear data from orignal cells 检查列中的值是否与其他工作表中的范围匹配 - Check if the values from a column match the range from a different sheet 仅当在另一个工作表中找不到匹配项时,VBA 才能复制一系列单元格 - VBA to copy a range of cells only if no match is found in another sheet Excel VBA宏:在一个工作表中查找/匹配单元格到另一个工作表中的列 - Excel VBA Macros: Find/Match cells from one sheet to a column in another 从一系列单元格中添加列 - Adding Column from a Range of Cells
相关标签
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM