The formation of molecular clouds in spiral galaxies