If you did it from Status Window or from a channel or query or something, it's
//write -dl %MyVar filename.txt
The // is needed to tell mIRC to check for $identifiers and %vars and to evaluate them, with / they are just passed as text.

If it is part of a script (ie written in remotes (alt-r alt-r) section or aliases (alt-r alt-a) section) then you don't need the / anyway, since each line is supposed to start with a command. If it doesn't work then, you probably have an error somewhere else, best to echo -s myvar %MyVar before and check if that value is correct.

Although the help file says it's write -dlN, write -dl N works too without the need for $+