Option Explicit
Public Sub Buttons_Add()
Dim oSheet As Worksheet
Dim lLastRow As Long
Dim lColOffset As Long
Dim oCell As Range
Dim oQRange As Range
Dim oAnswerBox As Range
Dim siGroupWidth As Single
Dim siGroupHeight As Single
Dim oFrame As GroupBox
Dim oOption As OptionButton
Set oSheet = ActiveSheet
lLastRow = oSheet.Range("A" & Rows.Count).End(xlUp).Row 'Change "A" to column containing questions
'Question range = A1 to last used cell in col A
Set oQRange = oSheet.Range("A1:A" & lLastRow) 'Adjust range containing question to suit
lColOffset = 4 ' Number of columns to offset option buttons from "questions" column
siGroupWidth = 120 ' Width of frame (Note: option button must fit completely inside)
siGroupHeight = 40 ' Height of frame (Note: option button must fit completely inside)
For Each oCell In oQRange 'If there's a question add a frame and 2 option buttons (offset specified #cols)
If oCell.Value <> "" Then
'Append question cell address to control names for later query if needed
Set oAnswerBox = oCell.Offset(-1, lColOffset)
Set oFrame = oSheet.GroupBoxes.Add(oAnswerBox.Left, oAnswerBox.Top, siGroupWidth, siGroupHeight)
oFrame.Visible = False ' Hide the frame
oFrame.Name = "Frame" & oCell.Address 'Name = "Frame" w/appended cell address of question for later ID
'Insure option button fits inside above frame to insure mutually exclusive toggle
Set oOption = oSheet.OptionButtons.Add(oAnswerBox.Left + 10, oAnswerBox.Top + 10, siGroupWidth / 2, siGroupHeight / 2)
oOption.Name = "Option" & oCell.Address & "Yes" 'Name = "Option" w/appended cell address of question + "Yes" for later ID
oOption.Caption = "Yes"
'Insure option button fits inside above frame to insure mutually exclusive toggle
Set oOption = oSheet.OptionButtons.Add(oAnswerBox.Left + 40, oAnswerBox.Top + 10, siGroupWidth / 2, siGroupHeight / 2)
oOption.Name = "Option" & oCell.Address & "No" 'Name = "Option" w/appended cell address of question + "No" for later ID
oOption.Caption = "No"
End If
Next oCell
End Sub
Public Sub Buttons_Delete()
Dim oShape As Shape
'Delete all shapes having names starting with "Fra" or "Opt"
For Each oShape In ActiveSheet.Shapes
If Left(oShape.Name, 3) = "Fra" Or Left(oShape.Name, 3) = "Opt" Then
oShape.Delete
End If
Next oShape
End Sub
Public Sub Setup()
Buttons_Delete
ActiveSheet.Range("A5").Value = "Question 1?"
ActiveSheet.Range("A9").Value = "Question 2?"
ActiveSheet.Range("A16").Value = "Question 3?"
ActiveSheet.Range("A27").Value = "Question 4?"
ActiveSheet.Range("A33").Value = "Question 5?"
ActiveSheet.Range("A35").Value = "Question 6?"
ActiveSheet.Range("A36").Value = "Question 7?"
ActiveSheet.Range("A38").Value = "Question 8?"
Buttons_Add
'Manually change position of questions then run "Buttons_Delete" & "Buttons_Add" in that order
End Sub
Public Sub Show_Answers()
Dim oSheet As Worksheet
Dim lLastRow As Long
Dim sAnswers As String
Dim oOptYes As Shape
Dim oOptNo As Shape
Dim iQCount As Integer
Dim oCell As Range
Dim oQRange As Range
Set oSheet = ActiveSheet
lLastRow = oSheet.Range("A" & Rows.Count).End(xlUp).Row
Set oQRange = oSheet.Range("A1:A" & lLastRow)
sAnswers = ""
iQCount = 1
For Each oCell In oQRange
If oCell.Value <> "" Then
'Address option buttons by unique name assigned in "Buttons_Add"
Set oOptYes = oSheet.Shapes("Option" & oCell.Address & "Yes")
Set oOptNo = oSheet.Shapes("Option" & oCell.Address & "No")
Select Case oOptYes.ControlFormat.Value & oOptNo.ControlFormat.Value
Case "1-4146"
sAnswers = sAnswers & "Question " & iQCount & vbTab & "Yes" & vbCrLf
Case "-41461"
sAnswers = sAnswers & "Question " & iQCount & vbTab & "No" & vbCrLf
Case Else
MsgBox "You didn't answer all the questions. Please try again!"
Exit Sub
End Select
iQCount = iQCount + 1
End If
Next oCell
MsgBox sAnswers
End Sub
Public Sub Clear_Answers()
Dim oShape As Shape
For Each oShape In ActiveSheet.Shapes
If Left(oShape.Name, 3) = "Opt" Then
oShape.ControlFormat.Value = False
End If
Next oShape
End Sub