We derive the general anomaly polynomial for a class of two-dimensional CFTs arising as twisted compactifications of a higher-dimensional theory on compact manifolds $mathcal{M}_d$, including the contribution of the isometries of $mathcal{M}_d$. We then use the result to perform a counting of microstates for electrically charged and rotating supersymmetric black strings in AdS$_5times S^5$ and AdS$_7times S^4$ with horizon topology BTZ$ ltimes S^2$ and BTZ$ ltimes S^2 times Sigma_mathfrak{g}$, respectively, where $Sigma_mathfrak{g}$ is a Riemann surface. We explicitly construct the latter class of solutions by uplifting a class of four-dimensional rotating black holes. We provide a microscopic explanation of the entropy of such black holes by using a charged version of the Cardy formula.