?
Solved

Need help with modifying VBA (in Excel)

Posted on 2014-11-04
6
Medium Priority
?
339 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 27

Accepted Solution

by:
Glenn Ray earned 2000 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This article describes a serious pitfall that can happen when deleting shapes using VBA.
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

770 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