Move vba Sub in sheet to activate on change

oblix

Board Regular
Joined
Mar 29, 2017
Messages
183
Office Version
  1. 2010
Platform
  1. Windows
Hi there
I have this code I would like clean up
Done mostly with macro recorder (and I know it sucks!)
and then to place in sheet "PERMENANT LIVING IN REGISTER" to run on changes that take place in this sheet

Sub FILTER_PERMEXPORT()

Application.EnableEvents = False
Application.ScreenUpdating = False

Sheets("Perm Transfer").Select
Range("O3:Z1000").Select
Selection.ClearContents

Sheets("PERMENANT LIVING IN REGISTER").Range("C4:AV500").AdvancedFilter Action _
:=xlFilterCopy, CriteriaRange:=Range("'Perm Transfer'!Criteria_P"), _
CopyToRange:=Range("'Perm Transfer'!Extract_P"), Unique:=False
Call Sort_perm2

Application.EnableEvents = True
Application.ScreenUpdating = True

End Sub

please assist
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
copy all between Sub and End Sub, choose worksheet from the top left vb editor dropdown, choose an event from the right dropdown (possibly Change or SelectionChange) and paste in the resulting sub?
 
Upvote 0
Thank you for reply

but my codes is realy a mess
I tried that but then gives me error because of this:

Sheets("Perm Transfer").Select
Range("O3:Z1000").Select
Selection.ClearContents
I think at least it is the problem
Not sure that it is efficient or should be there at all?
 
Upvote 0
You're being rather cryptic by not showing all of the new code and especially by not saying what the error message is. Doubly so by posting multiple lines and not saying which one raises the error. I'd rather look at all the 'messy' code as opposed to bits and pieces that don't reveal much at all.
When posting more than a few lines of code, please use code tags (VBA button on posting toolbar) to maintain indentation and readability.
 
Upvote 0
Sorry, I have changed the code to look like this:

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    Application.EnableEvents = False
    Application.ScreenUpdating = False
    
    ThisWorkbook.Worksheets("Perm Transfer").Range("O3:Z1000").ClearContents
    
    Sheets("PERMENANT LIVING IN REGISTER").Range("C4:AV500").AdvancedFilter Action _
                      :=xlFilterCopy, CriteriaRange:=Range("'Perm Transfer'!Criteria_P"), _
                      CopyToRange:=Range("'Perm Transfer'!Extract_P"), Unique:=False
    
    Call Sort_perm2
    
    Range("O3").Select
    
    Application.EnableEvents = True
    Application.ScreenUpdating = True
 
End Sub

and this part is flaged in yellow:
Sheets("PERMENANT LIVING IN REGISTER").Range("C4:AV500").AdvancedFilter Action _
:=xlFilterCopy, CriteriaRange:=Range("'Perm Transfer'!Criteria_P"), _
CopyToRange:=Range("'Perm Transfer'!Extract_P"), Unique:=False

Can It By because I am trying to place this code in the ....Sheets("PERMENANT LIVING IN REGISTER") it self?
 
Upvote 0
You're being rather cryptic by not showing all of the new code and especially by not saying what the error message is
I'm going to guess that the problem is your range syntax (use of single quotes). Try

Range("Perm Transfer!Criteria_P"),
and
Range("Perm Transfer!Extract_P"), Unique:=False
 
Upvote 0
Solution

Forum statistics

Threads
1,224,828
Messages
6,181,201
Members
453,022
Latest member
RobertV1609

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top