Thank you Andy and Yogi.
Andy's code doesn't work for me. I find that my code works sometimes and fails sometimes. When my codes fails, Andy's code doesn't work either.
Yogi's code works but I have to assign the output workbook in the codes and it must be saved first. Can I just make new workbook without saving it?
The most frustrating thing is that I don't know what's going wrong with my macro.
Yes, my code works, but not always. I have the experience that my macro works perfectly in testing and it fails at that line one hour later when I test it again on the same computer. I programed the workbook so that I cannot be saved so there should be no difference in the workbook between two testings.