The number of valence electrons for molecules can be calculated by adding the valence electrons of all the atoms that form that respective molecule.
Here are some examples
CO2: Each carbon dioxide molecule is formed from 1 C atom and 2 O atoms. We know that C has 4 valence electrons and that O has 6 valence electrons, which means that the number of valence electrons for 1 CO2 molecule will be
1⋅4+2⋅6=16e−
H2O: Again, each water molecule is formed from 1 O atom and 2 H atoms. Since the number of valence electrons for O and H are 6 and 1, respectively, one molecule of water will have
2⋅1+1⋅6=8 valence electroncs.
H2SO4: One molecule of sulfuric acid has 2 H atoms, 1 S atom, and 4 O atoms, each contributing 1, 6, and 6 valence electrons. So the number of valence electrons for 1 molecule of sulfuric acid is