Can an institution which had a bad accreditation grade get a good rank and vice versa?
It is possible. The institutions can slip or do better after they got their accreditation. Assessments and evaluation for the accreditation is done once every few years and the performance and outcomes of the institution may rise or fall between those terms.
Whereas ranking is a reflection of the yearly performance. It is not at all dependant on the accreditation status or grade. It merely tells us the performance scores of the institution and it’s rank among similar institutions.