They are 2 different laws that say 2 different but related things.
Can. 927 It is absolutely forbidden, even in extreme urgent necessity, to consecrate one matter without the other or even both outside the eucharistic celebration.
That answers your question about when such a thing might be considered permitted. The canon is quite clear in answering that question.
The penalty for violating that canon is “a just penalty.”
Stop here. That’s all for canon 927. Now we are changing subjects.
Now, the other law is one that is not found directly in the Code itself. The Code of Canon Law talks about certain crimes that incur that Latae sententiae excommunication. The canons list some of them, but in very general terms like “crime against the Eucharist” To clarify what is meant by a crime against the Eucharist, the Pope (JPII) issued an Apostolic Constitution listing some of them. In that, he says that consecrating for sacrilegious purposes is one of those crimes to which the canon refers.
Violating canon 927, all by itself, is a crime, one to be punished by “a just penalty.”
If a priest goes beyond what 927 describes and adds the crime of sacrilege, then it incurs the excommunication.
Does that help?