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