Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

save data after time out has gone

0

hi

I have a vba code that has a time out (demo)

what code do I beed to do this

1....after a message box comes up saying you reached your time limit

2....when you click okay it will go and save all the data that they have put in while
       in demo mode

3....it will the save the code and shut down the workbook

Hope I have explained my self to what I want

Thank You

Answer
Discuss

Discussion

@zacrock
In the original files I offered up in your original post (original post) any data entered is saved unless the user clicks "Don't save" when closing the workbook.
WillieD24 (rep: 557) Jul 12, '23 at 9:50 am
@Willie - fair point but I guess the demo author is concerned about data loss being blamed on him.

I'm away from my PC and don't have time to look at your solution again but assume after the lock (if they save), the user just sees a locked file. If the main demo didn't rely on macros (apart from doing the lock), could a user open it with them disabled and avoid the lock?
John_Ru (rep: 6142) Jul 13, '23 at 8:39 am
Add to Discussion

Answers

0
Selected Answer

Zacrock

I haven't done the check for expiry but the attached file will be saved and closed the first time you open it (and tell you). When re-opened, it will tell you the demo has expired.

It looks like there's only one worksheet but there's a second, Sheet2 ("Locked") which is VeryHidden (so can't be revealed if the user right-clicks on a tab and chooses Unhide). It can only be revealed using VBA.

As it leaves me, cell A1 of that contains FALSE- this gets tested in the open procedure below (I've added comments to help):

Private Sub Workbook_Open()

    Dim Resp As Long

    ' check if the file has been locked already (in very hidden sheet2)
    If Sheet2.Cells(1, 1) = False Then
        ' if not, set the locked flag cell
        Sheet2.Cells(1, 1) = True
        Resp = MsgBox("Demo expired; this file will be locked, saved and closed", vbExclamation)
        If Resp = 1 Then
            ' do the lock procedure
            ' << call another procedure>>

            ' save the workbook
            ThisWorkbook.Close Savechanges:=True
        End If

        Else
        MsgBox "File locked- the demo period has expired"

    End If

End Sub

When the file is re-opened (provided macros are enabled*), you'll get the locked message.

You can reset it by changing A1 back to FALSE then in VBA Explorer's Immediate window pasting:

Sheet2.Visible=True

and return. Change A1 then use:

Sheet2.Visible=xlSheetVeryHidden

to hide it again. You'll need to test then add your lock procedure where I've indicated (or before you tell the user!).

(* Look in the Tutorials section, I'm pretty sure there's a lesson on how to force a file to be opened with macros-enabled).

Hope this helps. If so, please remember to mark this Answer as Selected.

Discuss

Discussion

Minor thing for next time - please limit your use of bold to emphasise only any key points (your whole question is in bold so it feels like you're shouting at us!) 
John_Ru (rep: 6142) Jul 16, '23 at 6:13 pm
Thanks for selecting my Answer, Zacrock.

This will need integrating with a working implementation of the answer to your previous question but I suspect you won't do it without my help... 
John_Ru (rep: 6142) Jul 18, '23 at 6:51 pm
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login