Combine subs in Excel VBA

PapaApe21

New Member
Joined
Aug 4, 2021
Messages
14
Office Version
  1. 2010
Platform
  1. Windows
Good Afternoon,

I want to create an Excel Spreadsheet which automatically closes after a certain time of being idle as multiple users need access to it.

I have found coding for that but unfortunately for it to work Macros need to be enabled.

I have found what I need which works separately but I'm not sure how to combine the two and Excel keeps throwing up errors


1) Enable Macro (took from the below website)


[ICODE]Private Sub Workbook_Open()[/ICODE]

[ICODE]'worksheets to show when macro is enabled[/ICODE]

[ICODE]Sheets("Data").Visible = True[/ICODE]

[ICODE]'worksheet that shows reminder to enable macro[/ICODE]

[ICODE]Sheets("Reminder").Visible = xlVeryHidden[/ICODE]

[ICODE]End Sub[/ICODE]

[ICODE]Private Sub Workbook_BeforeClose(Cancel As Boolean)[/ICODE]

[ICODE]'worksheet with reminder[/ICODE]

[ICODE]Sheets("Reminder").Visible = True[/ICODE]

[ICODE]'worksheets to show when macro is enabled[/ICODE]

[ICODE]Sheets("Data").Visible = xlVeryHidden[/ICODE]

[ICODE]ThisWorkbook.Save[/ICODE]

[ICODE]End Sub[/ICODE]




Close Excel after a certain time in macro (took from below)
[ICODE]https://answers.microsoft.com/en-us/msoffice/forum/all/a-simple-macro-to-save-and-close-a-excel/7cdbbf10-3e83-4d2e-990c-811cec8eda55[/ICODE]

ALT+F11 to open VB editor. Double click 'ThisWorkbook' and paste the first 2 modules in on the right.



Private Sub Workbook_Open()
StartTimer
End Sub

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
StartTimer
End Sub





Now right click 'ThisWorkbook' insert module and paste this code in. Save close and re-open the workbook and after 15 minutes of idle time it will save and close automatically.



Const idleTime = 900 'seconds
Dim Start
Sub StartTimer()
Start = Timer
Do While Timer < Start + idleTime
DoEvents
Loop
Application.DisplayAlerts = False
ActiveWorkbook.Close True
Application.DisplayAlerts = True
End Sub




I would appreciate some help how I can combine the two please to get the spreadsheet working.

Thank you,
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.

Forum statistics

Threads
1,223,099
Messages
6,170,107
Members
452,302
Latest member
TaMere

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top