You have a code fragment that can be grouped together. Tips: Turn the fragement into a method whose name explains the purpose of the method.
Mechanics:
Create a new method, and name it after the intention of the method (name it by what it does, not by how it does it).
Copy the extracted code from the source method into the new target method.
Scan the extracted code for references to any variables that are local in scope to the source method. These are local variables and parameters to the method.
See whether any temporary variables are used only within this extracted code. If so, declare them in the target method as temporary variables.
Look to see whether any of these local-scope variables are modified by the extracted code. If one variable is modified, see whether you can treat the extracted code as a query and assign the result to the varialbe concerned. If this is awkward, or if there is more than one such variable, you may need to use Split Temporary Variable and try again. You can eliminate temporary variables with Replace Temp with Query.
Pass into the target method as parameters local-scope variables that are read from the extracted code.
compile when you have dealt with all the locally-scoped variables.
Replace the extracted code in the source method with a call to the target method.