A general method and formulae to derive the CO-to-H_2\ conversion factor in galaxies as a function of the metallicity are presented based on an analysis of the observed data for the conversion factor obtained for nearby spiral and dwarf irregular galaxies. From the CO data for eight galaxies we obtained the following formula, which gives the conversion factor (X(*)) as a function of metallicity: \[ \log X^*=-1.0(12+\log O/H) + A, \] where X(*=) N_H_2/I_CO times 10(-20) H_2/(K km s(-1) ) and A is a constant. Since the metallicity is dependent on the luminosity of galaxies, we derived a correlation between the conversion factor and the absolute magnitude of galaxies. We also investigated dependence of the conversion factor on the distance from the center of individual galaxies. We also present a formula to calculate X(*) as a function of the radius in a galaxy.