Masturbation is a great evil and a sin. It is haram.no matter what.
If a person commits this haraam action at night in Ramadaan, that does not invalidate his fast, either on the day before or the day after.
But the Muslim must strive to control himself and avoid doing this haraam action and others, especially during this blessed month.
The Muslim has to make the most of this blessed month, for fasting is the best treatment for this haraam thing. Hence the Prophet (peace and blessings of Allaah be upon him) commanded young men who were unable to get married to fast. The Prophet (peace and blessings of Allaah be upon him) said: “O young men, whoever among you can afford it, let him get married, and whoever cannot afford it, then he should fast, for that will be a shield for him” – i.e., fasting will curb his desire.
Whoever has been tested with this must repent to Allaah and regret what he has done, and resolve not to do it again.
And Allaah knows best.