You will need an integer, and a unit variable and here is the code:
set intvariable = 0
Pick up every unit within x range matching conditions ((Matching Unit) is a hero equal to true)) and do actions
-> If (Distance Between (Position of UUNIT) and (Position of (Picked Unit)) is less than intvariable or intvariable == 0 then do
set intvariable = Distance Between (Position of Unit) and (Position of (Picked Unit))
set unitvariable = (Picked Unit)
And at the end, the unitvariable is your unit. Change x with the range in which you may pick the closest hero and the UUNIT with the unit around which you pick the closest target. Have a nice day!
~Daelin