Here is and idea which might work. I havent used it yet.

How acurate must your range detection be. Do you need for your weapon to change to other target as soon as it is one pixel closer or would lets say 5 pixel distance variation be enough.
If it is the later then I sugest next aproach:
Make yourself several concentric circle sprites ranging from verry small to the size of maximum weapons range. You then position theese sprites so that they are centered on your weapon.
Finally you simply use colision detection, starting with smalest sprite and if there is no colision with enemy unit you continue with larger ones until you manage to get first enemy in range.
On another thought you should first check colision detection with largest one (representing maximum weapons range) and only then use previously mentioned aproach to find closest one.