Serial on the Arduino Micro

So I recently got an arduino micro, I was trying out the new board and I tried to do serial printing, the code uploads fine but every time I try to open the serial port this error code pops up “Board at COM 18 is not available”

This is the code i have been using

void setup() {
  Serial.begin(9600);
  while(!Serial){
    ;}
    

}

void loop() {
  // put your main code here, to run repeatedly:
  Serial.println("Hello World!");
}

Please help, I don’t know why it is doing this.

So I kept trying to figure out how to fix the problem and I was looked at the serial port and it had changed from the previous one, instead of 18 it was now 20. It still didn't work. I did it again and then it was 19 and now it works. I guess it was just being weird. I guess if you're having the same problem just try changing, updating the serial port (try it multiple times) and maybe it will work.
Weird...

the code uploads fine but every time I try to open the serial port this error code pops up "Board at COM 18 is not available"

That error is nothing to do with your code and everything to do with the secret thing you are trying to communicate with.

It's a problem with your stupid computer, mine keeps changing the serial port too.

On windows prior to version 8, you can use the "device manager" screen of the "control panel", to check which serial port windows thinks that your arduino is connected to.

i recommend you to use one common USB port.
check whether it changes the port frequently ; as per me once port get allocated it wont change even if it changing there is problem with OS.