Authentic Supplication Number 176.

It is reported that the Prophet  said: "Whoever sees a person that has been inflicted [with anything] and then says: Al-hamdu lil-lahi 'allathee `afanee mimma 'ibtalaka bihi, wa fadh-dhalanee `ala katheerin mimman khalaqa tafdheelan (189), he would not be inflicted with that infliction."

Footnotes.
(189) All praise be to Allah who has pardoned me of that which He has tried you with, and made me much better than many of those He has created. [This is not supposed to be heard by the inflicted person.]

Reporters.
At-Tirmithi said it was comely, and Al-Albani agreed pointing out that it had many channels and testimonials. Al-Albani included this hadeeth in The Authentic of Good Sayings as #182.