If you sell the company prior to the ex-dividend date, you would not get the dividend. So, a company going ex-dividend on April 1st, if you hold the stock on April first, you will get the dividend.
Keep in mind, the stocks value is marked down by the dividend paid on the ex-dividend date. For example, a $10 stock paying a $0.10 dividend will be marked down to $9.90 on open the next day. So there is no difference to selling before or waiting until the ex-dividend date.
One thing I do want to clarify is market movements can send the stock higher or lower on the ex dividend date. So, it is not a guarantee the stock will only go down by the dividend paid on the ex date. But it's something that is very important for investors to understand.