Simplest solution would be to use of Worksheet.Copy:
If you don't specify either Before or After, Microsoft Excel creates a new workbook that contains the copied sheet object that contains the copied Worksheet object. The newly created workbook holds the Application.ActiveWorkbook Property (Excel) property and contains a single worksheet.
The single worksheet retains the Worksheet.Name Property (Excel) and Worksheet.CodeName Property (Excel) properties of the source worksheet.
If the copied worksheet held a worksheet code sheet in a VBA project, that is also carried into the new workbook.
Try:
Option Explicit
Sub Main()
' Call your Sub
CopySheet2CSV ThisWorkbook.Sheets("Tabelle1")
End Sub
Sub CopySheet2CSV(ws As Worksheet)
ws.Copy
ActiveWorkbook.SaveAs Filename:="C:\Users\b036081\NoBackupData\" & ws.Name & ".csv", _
FileFormat:=xlCSV, CreateBackup:=False
ActiveWindow.Close Savechanges:=False
End Sub
Capitalize on the fact that .Copy
conveniently sets ActiveWorkbook
to the newly created workbook that contains your single sheet.
Save it as csv, close it afterwards, thereby returning to your source worksheet.
Note: Don't explicitly turn all prompts off. Instead omit the two standard prompts you get when doing this manually by setting CreateBackup:=False
when saving and Savechanges:=False
when closing the csv-workbook.
That way you are still prompted in cases where the file exists or there is an error.