The government’s finances were in surplus by £2bn last month, the biggest surplus for July in 18 years, official figures show.
The figure was up from a surplus of £1bn a year ago.
At the same time, borrowing in the April-to-July period fell to its lowest level since 2002.
Analysts said the reduction in borrowing should give Chancellor Philip Hammond extra money to play with in the Budget this autumn.
Borrowing for the financial year so far has reached £12.8bn, £8.5bn less than in the same period in 2017, the Office for National Statistics said.
Public sector net debt, excluding public sector banks, was £1,777.5bn, equivalent to 84.3% of GDP. That is £17.5bn higher than a year earlier, but lower as a percentage of GDP than in 2017, when it was 86%.
‘Sharp downward trend’
Over the financial year to March 2019, the Office for Budget Responsibility (OBR), which produces the official government forecasts, expects the public sector to borrow £37.1bn.
That amounts to about one-quarter of what it borrowed in 2009-10, at the peak of the financial crisis.
“Public borrowing has remained on a sharp downward trend, creating scope for the chancellor to pause the fiscal consolidation next year and still meet his self-prescribed targets,” said Samuel Tombs, chief UK economist at Pantheon Macroeconomics.
“If this trend persists, borrowing will total just £23.7bn this year, much less than the £37.1bn forecast by the OBR in the Spring Statement.”
Ruth Gregory, senior UK economist at Capital Economics, was more cautious, pointing out that “an undershoot of this magnitude is unlikely”.
She added: “The recent improvement has been partly due to temporary factors, such as the timing of gilt issuance and redemptions.”
However, she said Mr Hammond “should be able to deliver the extra funds for the NHS without compromising his fiscal target or having to find savings elsewhere”.