Solved

Need help with modifying VBA (in Excel)

Posted on 2014-11-04
6
330 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I comeā€¦
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

832 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