A very impressive piece of code! I have been programing for over 40 years and never seen a novice produce such elegant code before.

The issue is in your select case section. If case =0 then within this code case is set to 1 then case 1 code is run and again Case is set to 2 then Case is set to 3. Here is the main issue Case 3 is bulk charging because the battery is low as you would want but if a cloud comes over then Panel Voltage is less than Battery Voltage and Case is set to 0. Once the cloud goes then the Case will again revert to 3 bulk charge until the next cloud. Obviously depending on how dark the cloud is there is a possibility that the Panel Voltage can be above Battery Voltage changing to below Battery Voltage and then above as the cloud passes.

This explains what is happening. My only question is why are you switching the Panel on and off?

All my solar panels are permanently connected to the battery regulator. The panels do have a diode in line.

If you do not switch the panels on and off you can take out all of the switching code and solve the problem.