Save UserForm and reopen later semi-complete and resume

samuelle

New Member
Joined
Aug 19, 2015
Messages
3
Hi,

I have quite a long form that the users have stated they want to pause halfway and resume.

I currently have the following code to save and close:
End With
ActiveWorkbook.SaveAs ([txtStudentLast])
ActiveWorkbook.Close
End Sub

but when I reopen it the data is written to the background sheet but the UserForm is empty ready for another entry. Can anyone help with code that would open the form where it left off? Ideally I'd want one button to "Save and Resume Later" and one button to "Save and Finish" - I suppose what I currently have is Save and Finish.

Thank you
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Hi,
Give this solution a try & see if it does what you want.

In a NEW standard module place ALL the following code:

Code:
 Enum XLUserActionType
xlStoreFormValues
xlReturnFormValues
xlClearStoredValues
End Enum


Sub GetFormControlValues(ByVal Form As Object, ByVal Action As XLUserActionType)


    Dim wsFormSettings As Worksheet
    Dim Foundcell As Range
    Dim r As Long
    
    On Error Resume Next
    Set wsFormSettings = Worksheets(Environ("USERNAME"))
    If Err.Number = 9 Then Set wsFormSettings = WorkSheetAdd(Environ("USERNAME"))
    On Error GoTo 0


    With wsFormSettings
        Set Foundcell = .Columns(1).Find(Form.Name, LookIn:=xlValues, lookat:=xlWhole, SearchOrder:=xlByRows)
        r = .Cells(.Rows.Count, "A").End(xlUp).Row + 1
    End With


    If Not Foundcell Is Nothing Then r = Foundcell.Row


    ControlSettings Form:=Form, ws:=wsFormSettings, RecordRow:=r, Action:=Action


End Sub


Sub ControlSettings(ByVal Form As Object, ByVal ws As Object, ByVal RecordRow As Long, ByVal Action As XLUserActionType)
    Dim ctl As Control
    Dim CtrlType As String


    For Each ctl In Form.Controls
        CtrlType = TypeName(ctl)


        Select Case Action
        
        Case xlStoreFormValues
            Select Case CtrlType
            Case "OptionButton", "CheckBox", "TextBox", "ComboBox", "ListBox", "ToggleButton"
                With ws
                    .Cells(RecordRow, 1).Value = Form.Name
                    .Cells(RecordRow, 2).Value = ctl.Name
                    .Cells(RecordRow, 3).Value = CStr(ctl.Value)
                End With
                RecordRow = RecordRow + 1
            End Select
            
        Case xlReturnFormValues
            Select Case CtrlType
            Case "OptionButton", "CheckBox", "ToggleButton"
                With ws.Cells(RecordRow, 3)
                    ctl.Value = IIf(.Value = "", False, .Value)
                End With
                RecordRow = RecordRow + 1
            Case "TextBox", "ComboBox", "ListBox"
                ctl.Value = ws.Cells(RecordRow, 3).Value
                RecordRow = RecordRow + 1
            End Select
            
        Case xlClearStoredValues
            If ws.Cells(RecordRow, 1).Value = Form.Name Then
                ws.Cells(RecordRow, 3).Value = ""
                RecordRow = RecordRow + 1
            End If
        End Select
    Next ctl
End Sub


Function WorkSheetAdd(ByVal SheetName As String) As Worksheet
    Dim ws As Worksheet
    Application.ScreenUpdating = False
    Set ws = Worksheets.Add
    With ws
        .Range("A1:C1").Value = Array("UserForm Name", "Control", "Value")
        .Name = SheetName
        .Visible = xlSheetVeryHidden
    End With
    Set WorkSheetAdd = ws
    Application.ScreenUpdating = True
End Function

In each form you require to save entry values place following code:

In the forms initialize Event:
Code:
Private Sub UserForm_Initialize()
    GetFormControlValues Form:=Me, Action:=xlReturnFormValues
End Sub

In the forms Save and Resume Later button code:

Code:
Private Sub Save_and_Resume_Later_Click()
    GetFormControlValues Form:=Me, Action:=xlStoreFormValues
End Sub

In the Forms Save and Finish Button code:

Code:
Private Sub Save_And_Finish_Click()
    GetFormControlValues Form:=Me, Action:=xlClearStoredValues
End Sub

Solution is designed to create a holding place for controls (TextBox,CheckBox,OptionButton,ComboBox,ListBox,ToggleButton) values which can be applied to any number of forms in your project as required.

In addition, a table is created for each user so their settings should not be overridden by another logging in to use your application.

Hope Helpful

Dave
 
Upvote 0

Forum statistics

Threads
1,222,753
Messages
6,168,011
Members
452,160
Latest member
Bekerinik

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