Formally means officially or in a way that follows an established custom. Formerly means previously or at an earlier time.

Examples explained
Formally.
The town formally reopened the building with a ribbon-cutting.
Why it works: The ceremony marked the official reopening.
Formerly.
The building formerly housed a bakery.
Why it works: The bakery was there in the past.
Both can appear in one sentence. The town formally reopened a building that formerly housed a bakery.
Try a shorter word in its place
Try officially or in a formal way for formally. In The guests dressed formally, the word describes their manner of dress. Try previously for formerly.
Both are adverbs. See Than vs. Then for another spelling choice where a word can tell you when something happened.
Quick check
- The building was ___ a school.
- The mayor ___ opened the new hall.
Reveal the answers and explanations
1. Formerly. It means previously, describing the building’s past use.
2. Formally. It means officially, describing how the hall was opened.