Chemically there is no difference between the gas, the liquid, and the solid, all of which are made up of molecules with the formula H2O.
Why "with" is used here? I find an example in Longman: Sugar is represented by the simple formula CHO. So could I change it to "by" or any other preposition?
Thank you.

"made up of molecules by the formula H20" changes the sense of the statement to mean (nonsensically) that the formula is responsible for making the water out of the molecules. Correct use of "by" would be "made up of molecules represented by the formula H20", following the Longman example. "Represented with" would also be acceptable.

Sorry, I couldn't resist.
"Longman" may know a lot about language but its chemistry could be better: asugar is represented by the simple formula (CH2O)6, where all the numbers are subscripts. Also, what is commonly called 'sugar' (= sucrose) has a different formula (C12H22O11), again with numbers as subscripts.