Sub tonghop_khaosat()
' Luu y:
' - De don gian code thi STT o cot L phai lien tiep, bat dau tu 1 - du lieu bat dau tu dong 4
' - So phieu nhap o E1
' - trong Sheet2 co dung 10 cot voi tieu de va thu tu nhu hien tai. Ket qua bat dau tu dong 4
- So phieu trong cot L:M co the them bot.
Dim lastRow As Long, k As Long, sophieu As Long, Arr, sp(), sh As Worksheet
Set sh = ThisWorkbook.Worksheets("Sheet1")
With sh
lastRow = .Cells(Rows.Count, "L").End(xlUp).Row
If lastRow < 4 Then
Application.Assistant.DoAlert "Error", "Kh" & ChrW(244) & "ng c" & ChrW(243) & " b" & ChrW(7843) & "ng phi" & ChrW(7871) & "u!", _
msoAlertButtonOK, msoAlertIconCritical, 0, 0, 0
Exit Sub
End If
' mang So phieu
sp = .Range("L4:M" & lastRow).Value
' So phieu nhap
sophieu = .Range("E1").Value
End With
' kiem tra so phieu
If sophieu < 1 Or sophieu > UBound(sp) Then
Application.Assistant.DoAlert "Error", "S" & ChrW(7889) & " phi" & ChrW(7871) & "u kh" & ChrW(244) & "ng h" & ChrW(7907) & "p l" & ChrW(7879) & ".", _
msoAlertButtonOK, msoAlertIconCritical, 0, 0, 0
Exit Sub
End If
' mang 10 phan tu. 8 phan tu cuoi chua chi so cac CheckBox
Arr = Array(0, 0, 4, 5, 9, 10, 15, 16, 22, 23)
' neu CheckBox co chi so Arr(k) duoc chon thi Arr(k) = "x", nguoc lai thi Arr(k) = trong
' O day ta loi dung mang Arr lam mang ket qua
For k = 2 To 9
If sh.Shapes("Check Box " & Arr(k)).ControlFormat.Value = 1 Then
Arr(k) = "x"
Else
Arr(k) = ""
End If
Next k
' kiem tra du lieu nhap
For k = 1 To 4
If Arr(2 * k) = Arr(2 * k + 1) Then
' 2 CheckBox cua cung cau hoi deu duoc chon hoac deu khong duoc chon - khong hop le
Application.Assistant.DoAlert "Error", "C" & ChrW(226) & "u tr" & ChrW(7843) & " l" & ChrW(7901) & "i " & k & _
" kh" & ChrW(244) & "ng h" & ChrW(7907) & "p l" & ChrW(7879), _
msoAlertButtonOK, msoAlertIconCritical, 0, 0, 0
Exit For
End If
Next k
' neu khong co loi thi k > 4. Nhap ket qua xuong Sheet2
If k > 4 Then
With ThisWorkbook.Worksheets("Sheet2")
' dong nhap ket qua
lastRow = .Cells(Rows.Count, "A").End(xlUp).Row + 1
If lastRow <= 4 Then lastRow = 4
' STT
Arr(0) = lastRow - 3
' So phieu
Arr(1) = sp(sophieu, 2)
' nhap ket qua
.Cells(lastRow, "A").Resize(, UBound(Arr) + 1).Value = Arr
End With
End If
End Sub