We present a numerical method and computer code to calculate the radiative transfer and excitation of molecular lines . Formulating the Monte Carlo method from the viewpoint of cells rather than photons allows us to separate local and external contributions to the radiation field . This separation is critical to accurate and fast performance at high optical depths ( \tau \ga 100 ) . The random nature of the Monte Carlo method serves to verify the independence of the solution to the angular , spatial , and frequency sampling of the radiation field . These features allow use of our method in a wide variety of astrophysical problems without specific adaptations : in any axially symmetric source model and for all atoms or molecules for which collisional rate coefficients are available . Continuum emission and absorption by dust is explicitly taken into account but scattering is neglected . We illustrate these features in calculations of ( i ) the HCO ^ { + } J =1–0 and 3–2 emission from a flattened protostellar envelope with infall and rotation , ( ii ) the CO , HCO ^ { + } , CN and HCN emission from a protoplanetary disk and ( iii ) HCN emission from a high-mass young stellar object , where infrared pumping is important . The program can be used for optical depths up to 10 ^ { 3 } -10 ^ { 4 } , depending on source model . We expect this program to be an important tool in analysing data from present and future infrared and ( sub ) millimetre telescopes .