You are on page 1of 11

General Declarations

Force variable declaration Option Explicit Create Arrays for EACH data worksheet column Dim QuestionOne (5 to 140) as String Dim QuestionTwo (5 to 140) as String Dim QuestionThree (5 to 140) as String Dim QuestionFour (5 to 140) as String Arrays and variables for whole numbers should be Long NOT Integer Dim QuestionFive (5 to 140) as Long Dim Answer (5 to 140) as String Dim Picture (5 to 140) as String Create variables for EACH ONE of the arrays above Dim strQuestionOne as String Dim strQuestionTwo as String Dim strQuestionThree as String Dim strQuestionFour as String Dim lngQuestionFive as Long Dim strAnswer as String Dim strPicture as String Create variable for current workbook path Dim strPath as String Create variables for Questions (if necessary for translation questions or questions that use checkboxes Create variable for Question Five to use when saving combobox text string Dim strQuestionFive

START Button
Load Arrays with Data from Worksheet Dim X as Integer create a loop to search arrays for an answer(s) For X= 5 to 140 Answer(X)=Worksheets(Data).Range(A & X).Value QuestionOne(X)=Worksheets(Data).Range(B & X).Value QuestionTwo(X)=Worksheets(Data).Range(C & X).Value QuestionThree(X)=Worksheets(Data).Range(D & X).Value QuestionFour(X)=Worksheets(Data).Range(E & X).Value QuestionFive(X)=Worksheets(Data).Range(F & X).Value Picture(X)=Worksheets(Data).Range(G & X).Value

Next Clear all controls that are used for questions or displaying answers cboQuestionOne.Clear cboQuestionTwo.Clear cboQuestionThree.Clear cboQuestionFour.Clear cboQuestionFive.Clear txtAnswer.text= imgAnswer.Picture = LoadPicture("") Disable all controls that are used for questions or answers cboQuestionOne.Enabled=False cboQuestionTwo.Enabled=False cboQuestionThree.Enabled=False cboQuestionFour.Enabled=False cboQuestionFive.Enabled=False txtAnswer.Enabled=False imgAnswer.Enabled=False Setup First Question cboQuestionOne.Enabled=True NOTE: Add a clear command EACH time you AddItems to a combo or list box. cboQuestionOne.Clear cboQuestionOne.AddItem First Choice cboQuestionOne.AddItem Second Choice cboQuestionOne.AddItem Third Choice

FIRST QUESTION
Save Chosen Answer strQuestionOne = cboQuestionOne.Value Disable Current Question cboQuestionOne.Enabled=False Setup Next (Second) Question cboQuestionTwo.Enabled=True Text choice example cboQuestionTwo.Clear cboQuestionTwo.AddItem Choice A cboQuestionTwo.AddItem Choice B cboQuestionTwo.AddItem Choice C

Numeric choice example cboQuestionTwo.Clear cboQuestionTwo.AddItem 1 cboQuestionTwo.AddItem 2 cboQuestionTwo.AddItem 3

Note: If you are using a numeric variable to save the answer to a combobox, whenever that control is cleared, it will try to save the value and result in an error. The solution to this is to use the following IF..Then statement when saving to a non-text variable: Private Sub cboQuestionOne_Change() If cboQuestionOne.Value <> Then lngQuestionOne = cboQuestionOne.Value Add anything else you plan to do when the question is answered here so it only happens when the user selects an answer End If End Sub

SELECT CASE Examples Example IF Choices Depend on an Answer to Previous Question


Select Case strQuestionTwo Case "First Choice" cboQuestionThree.Clear cboQuestionThree.AddItem "Choice A" cboQuestionThree.AddItem "Choice C" Case "Second Choice" cboQuestionThree.Clear cboQuestionThree.AddItem "Choice B" cboQuestionThree.AddItem "Choice C" Case "Third Choice" cboQuestionThree.Clear cboQuestionThree.AddItem "Choice A" cboQuestionThree.AddItem "Choice B" End Select

Example IF Choices Depend on Answers to Two Previous Questions


Select Case strQuestionOne Case "Option A" Select Case strQuestionTwo

Case "First Choice" cboQuestionThree.Clear cboQuestionThree.AddItem "Choice A" cboQuestionThree.AddItem "Choice C" Case "Second Choice" cboQuestionThree.Clear cboQuestionThree.AddItem "Choice B" cboQuestionThree.AddItem "Choice C" Case "Third Choice" cboQuestionThree.Clear cboQuestionThree.AddItem "Choice A" cboQuestionThree.AddItem "Choice B" End Select Case "Option B" Select Case strQuestionTwo Case "First Choice" cboQuestionThree.Clear cboQuestionThree.AddItem "Choice D" cboQuestionThree.AddItem "Choice F" Case "Second Choice" cboQuestionThree.Clear cboQuestionThree.AddItem "Choice E" cboQuestionThree.AddItem "Choice F" Case "Third Choice" cboQuestionThree.Clear cboQuestionThree.AddItem "Choice D" cboQuestionThree.AddItem "Choice E" End Select End Select IF you want to add some variety, you can use option buttons and/or check boxes an example of each is below OPTION BUTTON/CHECKBOX Examples Enable the controls optChoiceA.Enabled = True chkChoiceOne.Enabled = True Clear the controls optChoiceA.Value = False chkChoiceOne.Value = False

Unique Issues: Multiple checkboxes may be selected by the user; therefore, EACH Checkbox must have a different variable in which to store the users choice(s) Only one option button can be selected at a time; therefore, there is only one variable needed for a question with option buttons. Note: If you want option buttons for more than one question, you have to group them by specifying a Group Name in properties then each option button in that group will be independent of option buttons in other groups. The value of either a checkbox or an option button is Boolean either TRUE or FALSE. You can put these values in a String variable the result will be the words True or False depending on the stored value.

QUESTION SEQUENCING OPTIONS


Setup Next Question after Two (or more) Option Buttons Private Sub optQuestionOneA_Click() Capture Option Button Value in a Single Variable If optQuestionOneA.Value = True Then strQuestionOne = "A" End If Then enable and populate Next question as above End Sub

Private Sub optQuestionOneB_Click() Capture Option Button Value in a Single Variable If optQuestionOneB.Value = True Then strQuestionOne = "B" End If Then enable and populate Next question as above End Sub Setup Next Question after Two (or more) CheckBoxes Note: if possible, make your check boxes your LAST question. This will mean you will need to create a FIND button instead of automatically looking for an answer after the users answer your last question. Private Sub chkQuestionFiveA_Click() If Checked, Capture Check Button Value in a Single Variable; otherwise, if unchecked Clear the Variable If chkQuestionFiveA.Value = True Then strQuestionFiveA = "A"

Else: strQuestionfiveA = "" End If End Sub Private Sub chkQuestionFiveB_Click() If Checked, Capture Check Button Value in a Single Variable; otherwise, if unchecked Clear the Variable If chkQuestionFiveB.Value = True Then strQuestionFiveA = "B" Else: strQuestionfiveA = "" End If End Sub

TRANSLATING ANSWERS
Text answer translated into a Yes/No search on multiple mutually exclusive columns Capture the Answer into a string variable strQuestionOne=cboQuestionOne.Value Translate the answer into Three Yes/No variables Select Case strQuestionOne Case Answer A strColumnA=Yes strColumnB=No strColumnC=No Case Answer B strColumnA=No strColumnB=Yes strColumnC=No Case Answer C strColumnA=No strColumnB=No strColumnC=Yes End Select Price Range (text answer) translated into a number(s) to allow for mathematical search on a single column Capture the Answer into a string variable strQuestionOne=cboQuestionOne.Value Translate the answer into a long variable using the highest number in the answer Select Case strQuestionOne Case Answer 1-10 lngColumnA=10 Case Answer 11-20 lngColumnA=20 Case Answer 21-30 lngColumnA=30 End Select

Translate the answer into a two long variables for the high and low value of the range Select Case strQuestionOne Case Answer 1-10 lngColumnAH=10 lngColumnAL=1 Case Answer 11-20 lngColumnAH=20 lngColumnAL=11 Case Answer 21-30 lngColumnAH=30 lngColumnAL=21 End Select Two Answers where one is exclusive and the other means BOTH Private Sub optQuestionOneA_Click() Capture Exclusive Option Button Value by saving the opposite value If optQuestionOneA.Value = True Then strQuestionOne = "B" End If End Sub Private Sub optQuestionOneB_Click() Capture Inclusive Option Button Value by saving a blank text value If optQuestionOneA.Value = True Then strQuestionOne = "" End If End Sub In the Find Button IfThen test, use a does not equal test If strQuestionOne <> QuestionOne(X) by doing this, if the choice was A and the variable has B and the only Choices are A or B in the data, then A will be returned by the above equation. On the other hand, if the choice was B and that would mean both answers were correct matches, by having a in the variable, either option would be returned. Three (or More) Text Answers where there is a cumulative relationship is best done by adding an additional column in the data that has simple numeric values. In the following example, the second column has numeric values such that a 3 corresponds to A in the original column, 2 corresponds to B, and 1 corresponds to C. Capture the Answer into a string variable strQuestionOne=cboQuestionOne.Value Translate the answer into a long variable using the highest number in the answer Select Case strQuestionOne Case A lngQuestionOne=3 Case B

lngQuestionOne=2 Case C lngQuestionOne=1 End Select In the Find Button IfThen test, use a less than or equal to test If lngQuestionOne <= QuestionOne(X) By doing this test, C means only C is a valid match, B means C or B are valid matches, and A means any one of the three are valid matches.

FIND Button (single answer)


Enable Answer Text Box txtAnswer.Enabled = True Enable Answer Picture Control imgAnswer.Visible=True Create a temp variable to use in your loop Dim X As Integer Create a loop to search arrays for an answer(s) For X = 5 to 140 Note: the following command is all on ONE line If strQuestionOne = QuestionOne(X) And strQuestionTwo = QuestionTwo(X) And strQuestionThree = QuestionThree(X) And strQuestionFour = QuestionFour(X) And strQuestionFive = QuestionFive(X) Then Put Answer in a string variable strAnswer=Answer(X) Put value of string variable into text box txtAnswer.text=Your choice is & strAnswer & . Put Picture in a string variable strPicture=Picture(X) Find Current Workbook Path strPath=ActiveWorkbook.Path Use value of string variable to load correct picture imgAnswer.Picture=LoadPicture(strPath & \ & strPicture & .jpg) End If Next

FIND Button (multiple answers) then use Listbox below


Enable list box lstFinalAnswer.Enabled=True Create a temp variable to use in your loop Dim X As Integer Create a loop to search arrays for an answer(s) For X = 5 to 140 Note: the following command is all on ONE line

If strQuestionOne = QuestionOne(X) And strQuestionTwo = QuestionTwo(X) And strQuestionThree = QuestionThree(X) And strQuestionFour =QuestionFour(X) And strQuestionFive = QuestionFive(X) Then Put Answers in the Listbox lstAnswer.Additem Answer(X) End If Next LISTBOX (if used after FIND Button) Save Listbox Answer strFinalAnswer = lstAnswer.Value Enable Answer Text Box txtAnswer.Enabled = True Enable Answer Picture Control imgAnswer.Visible=True create a temp variable to use in your loop Dim X As Integer create a loop to search arrays for an answer(s) For X = 5 to 140 Note: the following command is all on ONE line If strFinalAnswer = Answer(X) Then Put value of string variable into text box txtAnswer.text=Your choice is & strFinalAnswer & . Put Picture in a string variable strPicture=Picture(X) Find Current Workbook Path strPath=ActiveWorkbook.Path Use value of string variable to load correct picture imgAnswer.Picture=LoadPicture(strPath & \ & strPicture & .jpg) End If Next

FIND Button Example for Using Both AND and OR tests


For instance, if you have Yes/No data in multiple columns and the data in each column is not mutually exclusive of the other related columns, then you need to use a nested IFThen statement inside the first IFThen statement. Enable Answer Text Box txtAnswer.Enabled = True Enable Answer Picture Control imgAnswer.Visible=True Create a temp variable to use in your loop

Dim X As Integer Create a loop to search arrays for an answer(s) For X = 5 to 140 Note: the following command is all on ONE line If strQuestionOne = QuestionOne(X) And strQuestionTwo = QuestionTwo(X) And strQuestionThree = QuestionThree(X) And strQuestionFour = QuestionFour(X) Then if all of the above is True and need to use an OR test with multiple possibilities where AT LEAST one must be True, then add a nested IF Statement If strQuestionFiveColumnA = QuestionFiveColumnA(X) Or strQuestionFiveColumnB = QuestionFiveColumnB(X) Or strQuestionFiveColumnC = QuestionFiveColumnC(X) Or strQuestionFiveColumnD = QuestionFiveColumnD(X) Then Put Answer in a string variable strAnswer=Answer(X) Put value of string variable into text box txtAnswer.text=Your choice is & strAnswer & . Put Picture in a string variable strPicture=Picture(X) Find Current Workbook Path strPath=ActiveWorkbook.Path Use value of string variable to load correct picture imgAnswer.Picture=LoadPicture(strPath & \ & strPicture & .jpg) End If End IF Next

TROUBLESHOOTING
Problem with Next Question setup Capture question answer into a variable strQuestionOne=QuestionOne.Value Create a Msgbox to display the questions variable immediately after selection by User Msgbox strQuestionOne Problem with Getting an Answer
IF there are no Error Messages AND you do not get an Answer, you can use the following troubleshooting procedure to double-check the values being stored in your variables and the translated value(s) you are using to search your data. You can then compare these to the actual data on your data sheet.

Msgbox for each Question Answer Capture question answer into a variable strQuestionOne=QuestionOne.Value Create a Msgbox to display the questions variable immediately after selection by User Msgbox strQuestionOne Textbox for Search Variables (these will only be different for translated answers) Create a TextBox for EACH search variable and place them on your sheet near the question to which they correspond (you will delete these later, so just put them in close proximity to the question and dont worry about the aesthetics). Then, add a command BEFORE you search for an answer that puts the variable(s) that correspond to EACH column of data in your data sheet into these textboxes. NOTE: For any translation question, you MAY have more than one column to search in your data; therefore, you will have more than one textbox for that question. txtQuestionOne.text=strQuestionOne OR txtQuestionOne.text=lngQuestionFive Compare these results to find and fix any discrepancies

You might also like