Define the user type:
Type Questions
User as string
RecordNo as long
SessionDate As Date
Answers() As String
End Type
When the user restarts a session, get his user name and date of original session then get his answer record:
Function Get_AnswerRecord(User As String, sessiondate As Date) As Questions
'When user logs on, retrieve the answer record from the User's last session
'assumse answer records are stored in a worksheet with:
' column A as the user
' column B as the session date
' answers in remaining columns
'Returns record of user answers or user = "" if user and date not found in worksheet records
Dim row As Long, col As Long, lastrow As Long, lastcol As Long, record As Long
Dim sht As Worksheet, ans As Questions
Set sht = Worksheets("records")
'get last used row in worksheet
lastrow = sht.Cells(sht.Rows.count, 1).End(xlUp).row
row = 1: record = 0
'Find the row for the User and session date
Do While row <= lastrow
If User = sht.Cells(row, 1) And sessiondate = sht.Cells(row, 2) Then
record = row
Exit Do
End If
row = row + 1
Loop
'Return user as "" if not found
If record = 0 Then Get_AnswerRecord.User = "": Exit Function
'fill in question structure
ans.User = User
ans.RecordNo = record
ans.sessiondate = sessiondate
lastcol = sht.Cells(record, Columns.count).End(xlToLeft).Column
ReDim ans.Answers(lastcol - 2)
col = 3
Do While col <= lastcol
ans.Answers(col - 3) = sht.Range(Cells(record, col))
col = col + 1
Loop
Get_AnswerRecord = ans
End Function
Update the answer record each time the user answers a question;
Sub UpdateAnswers(User As Questions, questionindex As Integer, answer As String)
'Save answers to to a worksheet
Dim record As Long, i As Long
Dim sht As Worksheet
Set sht = Worksheets("records")
If User.RecordNo <= 0 Then record = 1 Else record = User.RecordNo
sht.Cells(record, 1) = User.User
sht.Cells(record, 2) = User.sessiondate
If questionindex > UBound(User.Answers) Then ReDim Preserve User.Answers(questionindex)
User.Answers(questionindex) = answer
i = 1
Do While i <= UBound(User.Answers)
sht.Cells(record, i + 3) = User.Answers(i)
i = i + 1
Loop
End Sub
This will give you an idea of how to use user defined types.
Bookmarks