Cyber security of microgrids attracts increasing attention since the relatively open communication network of the microgrids is vulnerable to hacker attacks. This study proposes a real-time distributed economic dispatch scheme for the grid-connected microgrid against the cyberattacks. Firstly, a virtual leader agent is placed at the point of common coupling to measure the real-time power tracking error for achieving the power supply–demand balance, and then the optimal solution of the economic dispatch model is solved by the consensus algorithm of the multi-agent system theory. Subsequently, a set of detection algorithms is designed for each generator to determine whether its neighbouring generators suffer from the cyberattacks. The attacked generator is gradually marginalised from the communication network through updating the weight in the communication adjacency matrix based on its reputation value, thereby the impact of the attacked generator on the whole system is reduced greatly. Finally, the numerical simulations are carried out to verify the effectiveness of the proposed cyberattack-resilient distributed economic dispatch scheme.