简体   繁体   English

如果工作表“ X”中的单元格包含工作表“ y”的名称,请删除行工作表“ x”

[英]Delete row sheet 'x' if cell in sheet 'X' contains name of sheet 'y'

second Q here, still learning but trying to do my best! 第二个问题,仍然在学习,但尽我所能!

Question: 题:

I want to run a macro which takes the name of a sheet (it is the active sheet at that moment) and uses it to delete every row in sheet "PD" which contains the name of that "previous active sheet" in column "M". 我想运行一个宏,该宏使用工作表的名称(当时是活动工作表),并使用它来删除工作表“ PD”中的每一行,该行包含“ M”列中该“先前活动工作表”的名称”。 Than the macro should go back to that "previous active sheet" and fill some cells with color (that part should be ok) 比宏应返回到该“上一个活动表”,并用颜色填充一些单元格(该部分应该可以)

I tried a couple of things and with help of other topics below and the record button i managed to get this code, which doesn't work 我尝试了几件事,并在下面的其他主题和记录按钮的帮助下设法获得了此代码,该代码不起作用

Sub FindandDelete
Sheets("PD").Select
Range("M").Select
With ActiveSheet
.AutoFilterMode = False
With Range("M1", Range("M" & Rows.Count).End(xlUp))
    .AutoFilter 1, ActiveSheet.Previous.Name.Select
    On Error Resume Next
    .Offset(1).SpecialCells(12).EntireRow.Delete
End With
.AutoFilterMode = False
End With
ActiveSheet.Previous.Select
Range("N16,N17").Select
With Selection.Interior
    .Pattern = xlSolid
    .PatternColorIndex = xlAutomatic
    .Color = 49407
    .TintAndShade = 0
    .PatternTintAndShade = 0
End With
Range("A1").Select
End Sub

For activate sheet: How to activate a specific worksheet in Excel 对于激活表: 如何在Excel中激活特定的工作表

Delete row: http://www.mrexcel.com/forum/excel-questions/537771-delete-row-if-specific-cell-value-matches-value-found-another-worksheet.html 删除行: http : //www.mrexcel.com/forum/excel-questions/537771-delete-row-if-specific-cell-value-matches-value-found-another-worksheet.html

Hope that anyone can help, if more effort or explanation needed, happy to hear. 希望任何人都可以提供帮助,如果需要更多的努力或解释,我们乐于倾听。

To be honest your code might work with some adjustments, but I'd rather start from scratch and use this: 老实说,您的代码可以进行一些调整,但我宁愿从头开始并使用以下代码:

Sub FindAndDelete
    Dim strAName As String
    Dim lngCounter as Long
    strAName = ActiveSheet.Name

    With Worksheets("PD")
        For lngCounter = .Cells(Rows.Count, 13).End(xlUp).row to 1  Step -1
            if .Cells(lngcounter, 13).value = strAName then
                .Rows(lngCounter).Delete
            end if
        Next lngCounter
    End with

    'Do your coloring stuff, which you said is fine now
End Sub

You should avoid changing sheets. 您应该避免换页。 There usually is no need to "activate" or "select" anyhting in VBA, it is just something humans have to do and therefore the MacroRecorder uses it... 通常不需要在VBA中“激活”或“选择”任何操作,这只是人类要做的事情,因此MacroRecorder会使用它。

暂无
暂无

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

相关问题 如果工作表1上的单元格A1包含X,则将工作表2中的A1复制到工作表1上的单元格A2 - If cell A1 on sheet1 contains X then copy A1 from sheet2 to cell A2 on sheet 1 仅当单元格 1 包含“x”且单元格 2 包含“y”时才将行复制到另一个工作表中 - Copy rows into another sheet only if the cells1 contains "x" and cell2 contains "y" 在sheet(x)列(x)中查找值,如果它们与粘贴行匹配,则与sheet(y)列(y)中的值匹配 - Look up values in sheet(x) column(x), match to values in sheet(y) column(y), if they match paste row 如果单元格X与另一个工作表中的单元格Y匹配,则为平均Vlookup值 - Average Vlookup values if it Cell X matches Cell Y in another sheet 如果A列中的单元格与工作表2列A中的任何单元格都不匹配,请删除包含工作表1列A的行 - If a cell in column A doesn't match a cell anywhere in sheet 2, column A, delete the row that contains Sheet 1 Column A Sheet.Range(Sheet,Cells(x,y), Sheet.Cells(x2,y2)) 语法 - Sheet.Range(Sheet,Cells(x,y), Sheet.Cells(x2,y2)) Syntax 如果工作表上的单元格值与另一张表上的单元格值相同,则删除该行 - Delete the row if the cell value on sheet is the same then the cell value on another sheet Google工作表如果工作表1中的X =工作表2中的Y,则在工作表1中显示工作表2中的Z - Google Sheets If X in Sheet 1 = Y in sheet 2, display Z from sheet 2 on sheet 1 在不发出警告的情况下删除名称中包含单词“sheet”的任何工作表 - Delete without warning any sheet for which the name contains the word "sheet" 如果字段具有X,则将单元格复制到另一张工作表 - If field has an X, copy cell to another sheet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM