This paper studies optimal time-consistent macroprudential policy in a model with endogenous capital formation. Previous studies on optimal time-consistent macroprudential policy in economies where borrowing is limited by the value of collateral assume that aggregate capital is fixed or apply models where production does not depend on capital. I find that it is optimal to restrict borrowing in "good times" in an economy with endogenous capital formation, while borrowing should be supported in times of financial stress. Both of these results are consistent with related studies with fixed aggregate capital. However, the imposition of macroprudential regulation distorts capital formation, which makes a more cautious ex ante intervention compared to previous studies optimal. This is because less capital not only leads to lower production but also hampers borrowing during crises, as capital serves as collateral in debt contracts. As a main result, I find that the optimal macroprudential policy hardly prevents crises and that the induced welfare gains are minimal. If financial regulation is coordinated with an investment policy that addresses the adverse effects on capital formation, almost all crises are prevented and the welfare gains are substantially higher.