(No calamity occurs on the earth nor in yourselves) meaning, `there is nothing that touches you or happens in existence,'
إِلاَّ فِى كِتَـبٍ مِّن قَبْلِ أَن نَّبْرَأَهَآ
(but it is inscribed in the Book of Decrees before We bring it into existence.) meaning, `before We created the creation and started life.' Qatadah commented on this Ayah,
مَآ أَصَابَ مِن مُّصِيبَةٍ فِى الاٌّرْضِ
(No calamity occurs on the earth) refers to famine, while,
لِّكَيْلَا تَأْسَوْا عَلَىٰ مَا فَاتَكُمْ وَلَا تَفْرَحُوا بِمَا آتَاكُمْ (...so that you may neither grieve on what has escaped you, nor over-exult on what He has given you ...57:23) This means that Allah has informed us of His encompassing knowledge, recording all things before they occur and creating all things in due measure known to Him, so that we may know that what has met us would never have missed us, and what has missed us would never have met us. Therefore, we are commanded not to over-grieve on the good things we have missed, nor to over-exult on the comforts or wealth we enjoyed in this life, and in turn be neglectful about Allah and the Hereafter. Sayyidna ` Abdullah Ibn ` Abbas ؓ says that it is man's natural disposition that certain things cheer him up and other things make him miserable, whereas the true position should have been as follows: When any misery befalls him, he should endure it with patience and earn reward in the Hereafter, and when he experiences joy, he should experience it with gratitude to Allah and earn reward in the Hereafter. [ Reported by Hakim who rates it as sahib. See Ruh ]
The receiving of things in the world or the losing of them is purely for the purpose of putting man to the test. Almighty God has settled in advance what shape man’s test-paper should take. A man should pay real attention not to what he has received or what has been taken away from him, but to how he reacted on each of these occasions. The correct and required reaction is that a man should not be disheartened if he suffers some loss, nor should he develop feelings of pride and vanity if he gains something.
Uthmani text of the Madinah mushaf (King Fahd Complex). Translation: Saheeh International. Tafsir via Quran.com.