Problem with IF / BEGIN / ELSE
I don't have an easy way to test this theory right now, but I'd bet that there are some validation checks of the OPENROWSET parameters going on when the batch compiles (the whole batch, regardless of IF / ELSE and whether or not those blocks eventually get executed).
Whenever I encounter this problem, using dynamic SQL is usually a solution.  Give that a shot:
IF (SELECT COUNT(1) FROM  #listavarejo) &gt; 0 
        EXEC sys.sp_executesql N'SELECT *  FROM OPENROWSET(''Microsoft.ACE.OLEDB.15.0'',''Excel.0;Database=C:\folder\excelfile.xlsx'', [Planilha1$])';
ELSE
        RAISERROR('NO FILES INSIDE FOLDER.',16,10);

This allows the code inside the dynamic SQL to be compiled only if it's executed.