Solved

Need help with modifying VBA (in Excel)

Posted on 2014-11-04
6
320 Views
Last Modified: 2014-11-04
Hello Experts:

I need some help with modifying an IF THEN ELSE (VBA) routine in MS Excel which validates that "required survey questions" have been answered.

Please see attached XLS... all details (e.g., problem and envisioned process flow) are included in the XLS.

I'd welcome any feedback on how the validation for "required survey question" routine can be modified so that the thrown dialogue boxes won't be a nuisance.

P.S. Please note that the actual survey has approximately 50 questions.    If "nested IFs" are required, this could become an issue.

Thank you in advance!
Tom
Testing.xlsm
0
Comment
Question by:ExpExchHelp
  • 4
  • 2
6 Comments
 
LVL 27

Accepted Solution

by:
Glenn Ray earned 500 total points
ID: 40422975
Change your BeforeClose event to this code (uses a For...Next loop with an Exit Sub clause if a question is left blank):
Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Dim x As Integer
    'Utilizes system username to by-pass function for validating question completion
    Dim objNet As Object
    On Error Resume Next
    Set objNet = CreateObject("WScript.NetWork")
    
    'Throws message box (for development/testing only)
    'Change name in "If objNet.UserName = "YourName" Then" to bypass Workbook_BeforeClose function
    MsgBox "My network username is: " & objNet.UserName
        
    'Change name in double quotes (next line) from, e.g., "YourName" to, e.g., "JamesSmith" in order to ...
    ' ...get into "Else" clause
    If objNet.UserName = "Tom" Then
        'Only system administrator should have these permissions
        'Do nothing... workbook will close even when required questions are not answered
    Else
        'Q1 through Q5 (rows x columns)
        'Question #6 is NOT tested (optional demographics question)
        For x = 1 To 5
            If Cells(x, 2).Value = "" Then
               MsgBox "Please enter a value for question #" & x & ".", vbInformation, "Missing information"
               Cancel = True
               Exit Sub
            End If
        Next x
  End If
    
End Sub

Open in new window


Regards,
-Glenn
0
 

Author Comment

by:ExpExchHelp
ID: 40422984
Glenn:

Absolutely perfect!!!    Thousand thanks for providing me such an elegant solution!

Cheers,
Tom
0
 
LVL 27

Expert Comment

by:Glenn Ray
ID: 40422999
You're welcome.  Pretty cool control you've got there; took me forever to exit my copy! :-P

-Glenn
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:ExpExchHelp
ID: 40423012
Glenn:

Quick follow-up... I forgot to consider one scenario.  

Not all of the question are in consecutive order... there will be blank rows in between (for comment boxes).

Also, due to other constraints, the drop-downs may be offset by a column or two.

Please see attached XLS to illustrate the setup.   My question... given that I have 3 groups of questions, how can I ensure the numbering for all of these are the same?   That is, any dialogue boxes for the 2nd group would start with "Q#6" and Q#9 for the 3rd group.

Again, thank you for your help!!!
Tom

P.S.  I wasn't able to attach the file... I'll open another post and cross-reference postings.
0
 

Author Comment

by:ExpExchHelp
ID: 40423036
Glenn:

In in a new post, I'm unable to attach any files (XLS, TXT, JPT).... totally odd.

I gladly open up a new post... but again, I'm just unable to attach any files in EE right now.

Either way, allow me to further describe the follow-up:
Question Range #1:   Q1 through Q5 (cells B1:B5)
Question Range #2:   Q6 through Q8 (cells E7:E9)
Question Range #3:   Q9 through Q11 (cells E12:E14)

Given that I have 3 groups of questions, how can I ensure the numbering for all of these are the same?   That is, any dialogue boxes for the 2nd group would start with "Q#6" and Q#9 for the 3rd group.

Please let me know if you'd like me to open up a new post in order to award you points.   I would have already done it but, again, I'm unable to attach any files in ExpertsExchange right now.

Thank you in advance!
Tom
0
 

Author Comment

by:ExpExchHelp
ID: 40423052
Glenn:

I actually figured it out... below is the attached code... I had to use "y - 1" and "z - 3" in order to adjust the actual question numbering.

Again, thanks!
Tom
    If objNet.UserName = "James" Then
        'Only system administrator should have these permissions
        'Do nothing... workbook will close even when required questions are not answered
    Else
        'Q1 through Q5 (rows x columns)
        'Question #6 is NOT tested (optional demographics question)
        For x = 1 To 5
            If Cells(x, 2).Value = "" Then
               MsgBox "Please enter a value for question #" & x & ".", vbInformation, "Missing information"
               Cancel = True
               Exit Sub
            End If
        Next x
    End If
    
    
    If objNet.UserName = "James" Then
        'Only system administrator should have these permissions
        'Do nothing... workbook will close even when required questions are not answered
    Else
        'Q1 through Q5 (rows x columns)
        'Question #6 is NOT tested (optional demographics question)
        For y = 7 To 9
            If Cells(y, 5).Value = "" Then
               MsgBox "Please enter a value for question #" & y - 1 & ".", vbInformation, "Missing information"
               Cancel = True
               Exit Sub
            End If
        Next y
    End If
    
    
    If objNet.UserName = "James" Then
        'Only system administrator should have these permissions
        'Do nothing... workbook will close even when required questions are not answered
    Else
        'Q1 through Q5 (rows x columns)
        'Question #6 is NOT tested (optional demographics question)
        For z = 12 To 14
            If Cells(z, 5).Value = "" Then
               MsgBox "Please enter a value for question #" & z - 3 & ".", vbInformation, "Missing information"
               Cancel = True
               Exit Sub
            End If
        Next z
    End If
        

Open in new window

0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now