Proof check for closed sub scheme of an affine scheme is affine

Basically I was solving an exercise of Hartshorne. So let $X=Spec A$ be an affine scheme and $i : Y \rightarrow X$ is a closed immersion. I have to show that $Y$ is affine. This is how I proceed. Firstly $Y$ is quasicompact from elementary topology since $X$ is quasicompact. Now $Y$ can be covered by open affines of the form $i^{-1}(V)$ where $V$ is open in $X$. Now for $D(f) \subset V, f \in A$ we have $D(f)= V_{f|_V}$ and hence $i^{-1}(D(f)) = i^{-1}(V_{f|_V})=(i^{-1}V)_{i^*(f|_V)}$ which is affine since $i^{-1}V$ is affine. Thus we have shown Y can be covered by finitely many open affines of the form $i^{-1}(D(f))=Y_{i^*(f)}$. Thus to apply the criterion for affineness we have to show these $i^*(f)$’s generate the unit ideal in $\mathscr {O}_Y(Y)$. But since $X-i(Y)$ is open by putting in some additional finitely many $f$s we can assume that $D(f)$ cover $Spec A$ and hence they generate the unit ideal and hence $i^*(f)$ generate unit ideal as well. So we can conclude $Y$ is affine. I am new to scheme theory so kindly point out if there’s any flaw in the above arguments. Also other suggestions/comments/insights are most welcome. Also I can’t figure out where I used the surjectivity of the sheaf morphism.

• Why not pick $V=X$? This shortens things substantially. – KReiser Aug 14 '18 at 20:50
• But I want $i^{-1}(V)$ to be affine. If u pick $X$ then u are basically assuming $Y$ is affine. – Ignorant Mathematician Aug 14 '18 at 20:51
• If you object to that, then your proof has bigger holes: how do you know that $i^{-1}(V)$ is ever affine? You need to either prove that $i^{-1}(V)$ is affine for enough affine $V\subset X$ to cover $Y$, or pick a different approach. – KReiser Aug 14 '18 at 21:01
• Well $Y$ is a scheme so it definitely has an open affine cover. Since i is a homeomorphism onto a closed subset of spec A clearly any open set is of the form $i^{-1}V$ where V is open in Spec A. – Ignorant Mathematician Aug 14 '18 at 21:04
• Ahh, I had misread the order of your adjectives. Sure, this works, but there are nicer solutions. (Think about completing $\mathcal{O}_X\to i_*\mathcal{O}_Y\to 0$ to a short exact sequence of $\mathcal{O}_X$ modules and using the equivalence between $\mathcal{O}_X$-mod and $\mathcal{O}_X(X)$-mod.) – KReiser Aug 14 '18 at 21:11