I know that it is bad practise to use activesheet as reference in the code but I'm not able to get rid of it without breaking the code.
Here is my code:
For Each HeaderMatch In SrchRange
If HeaderMatch Like HeaderKeyWord Then
HeaderCount = HeaderCount + 1
HeaderRows(HeaderCount) = HeaderMatch.Row
Sheets("Data").Cells(HeaderMatch.Row, 1).Resize(3, 7).Copy
Sheets("Critical Components").Select
NextRow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row + 3
ActiveSheet.Cells(NextRow, 1).Select
ActiveSheet.Paste
End If
Next
My code is running in the button which is located at the "Main" sheet. There is Data on the "Data" sheet where I collect all the 3 row headers which contain HeaderKeyWord on their first row. Those 3 rows are then copien from "Data" to "Critical Components" sheet.
This code is basically copied from the sample found
here.
The code heavily relies on ActiveSheet
and Select
which I guess is not good practice and I have been struggling with these as it often seems quite misleading.
Current code works and will do what it supposed to but I would like to make it with absolute referencies like Sheets("Data")
etc..
.Select
and .Paste
just won't work if ActiveSheet
is replaced with Sheets("Critical Components")
.
I don't get it why those methods(?) applies for ActiveSheet but not for sheets specified with Sheets?