I have a special cases, where I cannot find a way around Activate
which is considered to be bad (slow) coding in VBA. In this case I am looping over sheets of an original workbook, copy contents from those sheets - under certain conditions - then paste these contents into a new workbook and finally try to save the result to csv-files. I am doing this by using the following save-routine:
Private Sub saveWorksheet(pathx, wbkn, sheetn)
path = pathx & ".csv"
Workbooks(wbkn).Sheets(sheetn).Activate
Workbooks(wbkn).SaveAs Filename:=path, _
FileFormat:=xlCSV, Local:=True, CreateBackup:=False
End Sub
If I don't use Activate
I get an error. Is there any way to avoid it when saving files?
edit: Some info on why Activate
is considered bad:
How to avoid using Select in Excel VBA macros
Excel Select vs Activate
edit2: After some research and testing, I found out that the problem is not the save method itself, but rather the context of multiple workbooks being involved. I do not think you can prevent Activate
when saving a file from another workbook or also when running a macro on another workbook, while doing also stuff on the 'original' base-workbook. You can only prevent its use when you are working on the same (ie 'original') workbook all the time.
Please correct me if I am wrong.