各位大神好,想請教我在VBA userform上寫了一個考試介面,初始由第1頁開始依序按「下一頁」都能正常顯示,如按「上一頁」,只要沒有回到第1頁,前後頁切換也都正常,但如果有按至第1頁,再依序按到第3頁的時候,學員的答案會被第2頁答案覆蓋,請教大神我是否再哪一段程式碼有疏漏,感謝
Private Sub CommandButton2_Click()
Dim i, x, y As Integer
If sheet4.Range("F2") = 1 Then
'上一頁-當下資料寫入學員作答頁
x = sheet4.Range("F2")
For i = 11 To 20
sheet2.Cells(x + 1, 5) = Me("TextBox" & i).Value
x = x + 1
Next
Response = MsgBox("已經到第一頁囉")
ElseIf sheet4.Range("F2") = 2 Then
'上一頁-當下資料寫入學員作答頁
y = sheet4.Range("F2") * 10
For i = 11 To 20
sheet2.Cells(y + 2, 5) = Me("TextBox" & i).Value
y = y + 1
Next
'上一頁-帶出上一頁考題
y = sheet4.Range("F2") - 1
For i = 1 To 10
Me("TextBox" & i).Value = sheet2.Cells(y + 1, 3)
y = y + 1
Next
'上一頁-帶出上一頁學員作答
y = sheet4.Range("F2") - 1
For i = 11 To 20
Me("TextBox" & i).Value = sheet2.Cells(y + 1, 5)
y = y + 1
Next
'上一頁-當前頁數-1
sheet4.Range("F2") = sheet4.Range("F2") - 1
'顯示頁碼
Label15.Caption = "頁碼:" & sheet4.Range("F2")
Else
'上一頁-當下資料寫入學員作答頁
y = sheet4.Range("F2") - 1
y = y * 10
For i = 11 To 20
sheet2.Cells(y + 2, 5) = Me("TextBox" & i).Value
y = y + 1
Next
'上一頁-帶出上一頁考題
y = sheet4.Range("F2") - 1
y = y * 10 - 10
For i = 1 To 10
Me("TextBox" & i).Value = sheet2.Cells(y + 2, 3)
y = y + 1
Next
'上一頁-帶出上一頁學員作答
y = sheet4.Range("F2") - 1
y = y * 10 - 10
For i = 11 To 20
Me("TextBox" & i).Value = sheet2.Cells(y + 2, 5)
y = y + 1
Next
'上一頁-當前頁數-1
sheet4.Range("F2") = sheet4.Range("F2") - 1
'顯示頁碼
Label15.Caption = "頁碼:" & sheet4.Range("F2")
End If
End Sub
Private Sub CommandButton3_Click()
Dim i, x, y As Integer
If sheet4.Range("F2") = 1 Then
'下一頁-當下資料寫入學員作答頁
x = sheet4.Range("F2")
For i = 11 To 20
sheet2.Cells(x + 1, 5) = Me("TextBox" & i).Value
x = x + 1
Next
'下一頁-帶出下一頁考題
x = 12
For i = 1 To 10
Me("TextBox" & i).Value = sheet2.Cells(x, 3)
x = x + 1
Next
'下一頁-帶出下一頁學員作答
x = 12
For i = 11 To 20
Me("TextBox" & i).Value = sheet2.Cells(x, 5)
x = x + 1
Next
'下一頁-當前頁數+1
sheet4.Range("F2") = sheet4.Range("F2") + 1
'顯示頁碼
Label15.Caption = "頁碼:" & sheet4.Range("F2")
ElseIf sheet4.Range("F2") = sheet4.Range("G2") Then
'下一頁-當下資料寫入學員作答頁
x = sheet4.Range("F2") - 1
x = x * 10
For i = 11 To 20
sheet2.Cells(x + 2, 5) = Me("TextBox" & i).Value
x = x + 1
Next
Response = MsgBox("已經到最後一頁囉")
Else
'下一頁-當下資料寫入學員作答頁
y = sheet4.Range("F2") - 1
y = y * 10
For i = 11 To 20
sheet2.Cells(y + 2, 5) = Me("TextBox" & i).Value
y = y + 1
Next
'下一頁-帶出下一頁考題
y = sheet4.Range("F2") * 10
For i = 1 To 10
Me("TextBox" & i).Value = sheet2.Cells(y + 2, 3)
y = y + 1
Next
'下一頁-帶出下一頁學員作答
y = sheet4.Range("F2") * 10
For i = 11 To 20
Me("TextBox" & i).Value = sheet2.Cells(y + 2, 5)
y = y + 1
Next
'下一頁-當前頁數+1
sheet4.Range("F2") = sheet4.Range("F2") + 1
'顯示頁碼
Label15.Caption = "頁碼:" & sheet4.Range("F2")
End If
End Sub
你寫的有點太複雜
我試著把程式簡化如下供參考
(page 的 0 或是大於最後一頁你再自己加判斷)
Sub FormToSheet(byval page as integer)
'依照「頁數」把表單的「答案」寫到 sheet 中
End Sub
Sub SheetToForm(byval page as integer)
'依照「頁數」把表單的「題目」和「答案」寫到 form 中
End Sub
Sub CommandButton2_Click()
page = Sheet.Range("F2")
call FormToSheet(page)
page = page - 1
call SheetToForm(page)
Sheet.Range("F2") = page
End Sub
Sub CommandButton3_Click()
page = Sheet.Range("F2")
call FormToSheet(page)
page = page + 1
call SheetToForm(page)
Sheet.Range("F2") = page
End Sub
大神另外想請教,我的題目產出後都會放在同1個sheet,但userfrom一次只會秀出10題,程式碼中「FormToSheet」跟「SheetToForm」這裡可以怎麼調整?
我寫 SheetToForm
FormToSheet 你自己寫
Sub SheetToForm(byval page as integer)
'依照「頁數」把表單的「題目」和「答案」寫到 form 中
row = (page - 1) * 10 + 2 '計算起始列數,2,12,22,...
colQuestion = 3 '題目行數
colAnswer = 5 '學生答案行數
For i = 0 To 9
Me("TextBox" & (i+1)).Value = sheet2.Cells(row + i, colQuestion)
Me("TextBox" & (i+11)).Value = sheet2.Cells(row + i, colAnswer)
Next
End Sub
感謝大神指導^^,上次提問也是被您解救,真的學習收穫良多