Method Select of Object '_Worksheet' failed - why?

As discussed in comments, cannot select Sheets in VBA that are not active (or Range objects on them).

For example the following code

Sheets(1).Activate
Sheets(2).Range("A1").Select

will cause the error you are receiving.

In your case, it seems you are attempting to Select a sheet which is not active - your mainWS object is presumably not the ActiveSheet at the point you are calling this code. An easy way to test if this is happening is if you add the following to the end of your code:

if (ActiveSheet.Name <> mainWS.Name) then
    msgbox ("Going to crash after clicking ok!")
end if
mainWS.Select

Note that you can refer to the activated worksheet with the command ActiveSheet to either get properties or whatever other operations you are interested in doing.

This error can also happen if you have loop working thru all of the worksheets in the workbook and there are hidden sheets. Lookout for that.


Last, and unrelated to your specific error message, I assume you are declaring those variables somewhere and simply did not copy them here - if not I would consider using Option Explicit as you can often run into all sorts of issues without having Option Explicit at the top of your VBA code.