ok so all u want to do is have it in a function or sub so u can just say "call mywhatever" instead of having to copy that code to each button right? ... if so then do this:

go to the code section of vb n then go to the menus of vb n click on Tools | Add Procedure ...

then a box will come up n give ur procedure a name ... after that make the type (sub) n the scope (public) ... i'm not that good with subs n functions so that's y i'm telling u to put them as i'm telling u ...

well after u do that n click on "ok" then u will see a new procedure be added:

Public Sub whatever()

End Sub

now just place ur code in between those 2 lines ...
n in ur command buttons just type "call whatever"

change "whatever" to the name u want ...