แบบสอบถาม (Questionnaire) คือชุดคำถามพร้อมตัวเลือก ใช้เก็บข้อมูลจากผู้ตอบ Excel สร้างได้ง่าย บทความนี้สอน 2 วิธี
2 วิธีสร้างแบบสอบถามใน Excel
1. สร้างแบบสอบถามด้วยตนเอง (Manual)
ใช้คีย์เวิร์ดคำถามเป็นหัวคอลัมน์ แล้วใช้ฟีเจอร์ Form เพื่อกรอกข้อมูล
ขั้นตอนที่ 1: ใส่คีย์เวิร์ดคำถามเป็นหัวข้อ
- คำถาม "ชื่ออะไร?" → หัวคอลัมน์ Name
- "รหัสพนักงาน?" → ID
- "แผนก?" → Department
- "ต้องการวันหยุดปีนี้?" → Need Vacation (Y/N)
ขั้นตอนที่ 2: สร้างตาราง
- เลือกหัวข้อ > กด Ctrl+T > ติ๊ก My table has headers > OK
ขั้นตอนที่ 3: สร้างแท็บใหม่สำหรับแบบสอบถาม
- เลือกหัวข้อตาราง > File > Options > Customize Ribbon > New Tab
- คลิกขวา New Group > Rename > ตั้งชื่อ Questionnaire 1
- ใน Choose commands from: เลือก Commands Not in the Ribbon > เลือก Form > Add
- OK เพื่อปิด
ขั้นตอนที่ 4: เปิด Form เพื่อกรอกข้อมูล
- ไปแท็บใหม่ที่สร้าง > คลิก Form
- จะได้ฟอร์มมีช่องกรอกตามหัวข้อ
ขั้นตอนที่ 5: บันทึกข้อมูล
- กรอกข้อมูลผู้ตอบ > กด New เพื่อบันทึกลงแผ่นงาน
- ทำซ้ำสำหรับผู้ตอบคนถัดไป
2. สร้างแบบสอบถามด้วย VBA
สร้างแบบสอบถามแบบมีตัวเลือก: Not Sure, Agree, Disagree, Maybe (ค่า 0-3)
ขั้นตอน:
- แท็บ Developer > Visual Basic (หรือ Alt+F11)
- Insert > Module > วางโค้ดด้านล่าง
Option Explicit
Sub Create_Questionnaire()
Dim GrupBx As GroupBox, OptnBtn As OptionButton
Dim iMxBtns As Long, icell As Range, iRng As Range
Dim iWks As Worksheet, xCtr As Long
Dim xFrstOptnBtnCel As Range, xNumOfQ As Long
Dim iBorder As Variant
iBorder = Array(xlEdgeLeft, xlEdgeTop, xlEdgeBottom, xlEdgeRight, xlInsideVertical, xlInsideHorizontal)
iMxBtns = 4
xNumOfQ = InputBox("Set the numbers of questions", "Questions", 8)
Set iWks = ActiveSheet
With iWks
Set xFrstOptnBtnCel = .Range("E2")
.Range("A:D").Clear
With xFrstOptnBtnCel.Offset(-1, -1).Resize(1, iMxBtns + 1)
.Value = Array("Questions", "Option1", "Option2", "Option3", "Option4")
.Orientation = 90
.HorizontalAlignment = xlCenter
End With
Set iRng = xFrstOptnBtnCel.Resize(xNumOfQ, 1)
With iRng.Offset(0, -1)
.Formula = "=ROW()-" & iRng.Row - 1
.Value = .Value
End With
iRng.Offset(0, -3).Value = 1
With iRng.Offset(0, -4)
.FormulaR1C1 = "=IF(RC[2]="""","""",IF(RC[2]=6,""N/A"",RC[1]*(RC[2]-1)))"
End With
.Range("A1").Formula = "=SUM(A2:A" & xNumOfQ + 1 & ")"
With iRng.Offset(0, -4).Resize(, 4)
For xCtr = LBound(iBorder) To UBound(iBorder)
With .Borders(iBorder(xCtr))
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
Next xCtr
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
End With
iRng.EntireRow.RowHeight = 20
iRng.Resize(, iMxBtns).EntireColumn.ColumnWidth = 9
.GroupBoxes.Delete
.OptionButtons.Delete
End With
For Each icell In iRng
With icell.Resize(1, iMxBtns)
Set GrupBx = iWks.GroupBoxes.Add(Top:=.Top, Left:=.Left, Height:=.Height, Width:=.Width)
With GrupBx: .Caption = "": .Visible = True: End With
End With
For xCtr = 0 To iMxBtns - 1
With icell.Offset(0, xCtr)
Set OptnBtn = iWks.OptionButtons.Add(Top:=.Top, Left:=.Left, Height:=.Height, Width:=.Width)
OptnBtn.Caption = ""
If xCtr = 0 Then
With icell.Offset(0, -2): OptnBtn.LinkedCell = .Address(external:=True): End With
End If
End With
Next xCtr
Next icell
End Sub
- Ctrl+S บันทึก > Developer > Macros > เลือก Create_Questionnaire > Run
- ใส่จำนวนคำถาม (เช่น 5) > OK
- จัดรูปแบบ เพิ่มคำถาม ตั้งค่าตัวเลือกตามต้องการ
สรุป
วิธีแรก: ใช้ Form built-in เรียบง่าย วิธีที่สอง: VBA อัตโนมัติ สร้างแบบสอบถามแบบมีตัวเลือกได้เร็วขึ้น ดาวน์โหลดไฟล์ฝึกหัดได้จากลิงก์ด้านบน เยี่ยมชม ExcelDemy สำหรับบทความเพิ่มเติม