Background: Patients who are immunocompromised because of malignancy have an increased risk of herpes zoster and herpes zoster-related complications. We aimed to investigate the efficacy and safety of an inactivated varicella zoster virus (VZV) vaccine for herpes zoster prevention in patients with solid tumour or haematological malignancies. Methods: This phase 3, two-arm, randomised, double-blind, placebo-controlled, multicentre trial with an adaptive design was done in 329 centres across 40 countries. The trial included adult patients with solid tumour malignancies receiving chemotherapy and those with haematological malignancies, either receiving or not receiving chemotherapy. Patients were randomly assigned (1:1) to receive four doses of VZV vaccine inactivated by γ irradiation or placebo approximately 30 days apart. The patients, investigators, trial site staff, clinical adjudication committee, and sponsor's clinical and laboratory personnel were masked to the group assignment. The primary efficacy endpoint was herpes zoster incidence in patients with solid tumour malignancies receiving chemotherapy, which was assessed in the modified intention-to-treat population (defined as all randomly assigned patients who received at least one dose of inactivated VZV vaccine or placebo). The primary safety endpoint was serious adverse events up to 28 days after the fourth dose in patients with solid tumour malignancies receiving chemotherapy. Safety endpoints were assessed in all patients who received at least one dose of inactivated VZV vaccine or placebo and had follow-up data. This trial is registered (NCT01254630 and EudraCT 2010-023156-89). Findings: Between June 27, 2011, and April 11, 2017, 5286 patients were randomly assigned to receive VZV vaccine inactivated by γ irradiation (n=2637) or placebo (n=2649). The haematological malignancy arm was terminated early because of evidence of futility at a planned interim analysis; therefore, all prespecified haematological malignancy endpoints were deemed exploratory. In patients with solid tumour malignancies in the modified intention-to-treat population, confirmed herpes zoster occurred in 22 of 1328 (6·7 per 1000 person-years) VZV vaccine recipients and in 61 of 1350 (18·5 per 1000 person-years) placebo recipients. Estimated vaccine efficacy against herpes zoster in patients with solid tumour malignancies was 63·6% (97·5% CI 36·4 to 79·1), meeting the prespecified success criterion. In patients with solid tumour malignancies, serious adverse events were similar in frequency across treatment groups, occurring in 298 (22·5%) of 1322 patients who received the vaccine and in 283 (21·0%) of 1346 patients who received placebo (risk difference 1·5%, 95% CI −1·7 to 4·6). Vaccine-related serious adverse events were less than 1% in each treatment group. Vaccine-related injection-site reactions were more common in the vaccine group than in the placebo group. In the haematological malignancy group, VZV vaccine was well tolerated and estimated vaccine efficacy against herpes zoster was 16·8% (95% CI −17·8 to 41·3). Interpretation: The inactivated VZV vaccine was well tolerated and efficacious for herpes zoster prevention in patients with solid tumour malignancies receiving chemotherapy, but was not efficacious for herpes zoster prevention in patients with haematological malignancies. Funding: Merck & Co, Inc.