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". 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
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...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.