Filen åbnes,nyt nummer tildeles og filen gemmes, aktive fane kopieres til nyt ark der gemmes, filen lukkes. (Det nye ark kan evt. også lukkes)
Private Sub Workbook_Open()
Dim StartFil As String
StartFil = ActiveWorkbook.Name
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
Worksheets("Rekvisition").Range("H6") = Worksheets("Rekvisition").Range("H6") + 1
ActiveWorkbook.Save
'Working in Excel 97-2016
Dim FileExtStr As String
Dim FileFormatNum As Long
Dim Sourcewb As Workbook
Dim Destwb As Workbook
Dim TempFilePath As String
Dim TempFileName As String
Set Sourcewb = ActiveWorkbook
'Copy the sheet to a new workbook
ActiveSheet.Copy
Set Destwb = ActiveWorkbook
'Determine the Excel version and file extension/format
With Destwb
If Val(Application.Version) < 12 Then
'You use Excel 97-2003
FileExtStr = ".xls": FileFormatNum = -4143
Else
'You use Excel 2007-2016
Select Case Sourcewb.FileFormat
Case 51: FileExtStr = ".xlsx": FileFormatNum = 51
Case 52:
If .HasVBProject Then
FileExtStr = ".xlsm": FileFormatNum = 52
Else
FileExtStr = ".xlsx": FileFormatNum = 51
End If
Case 56: FileExtStr = ".xls": FileFormatNum = 56
Case Else: FileExtStr = ".xlsb": FileFormatNum = 50
End Select
End If
End With
'Save the new workbook and close it
TempFilePath = Application.DefaultFilePath & "\"
TempFileName = "Udstedte_Rekvisitioner " & Worksheets("Rekvisition").Range("H6")
With Destwb
.SaveAs TempFilePath & TempFileName & FileExtStr, FileFormat:=FileFormatNum
'.Close SaveChanges:=False
End With
MsgBox "Du kan finde den nye fil: " & TempFileName & " i " & TempFilePath
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
Workbooks(StartFil).Close SaveChanges:=False
End Sub