Home

VBA Conditional for Form Help Needed

%3CLINGO-SUB%20id%3D%22lingo-sub-504790%22%20slang%3D%22en-US%22%3EVBA%20Conditional%20for%20Form%20Help%20Needed%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-504790%22%20slang%3D%22en-US%22%3E%3CP%3EHello%20everyone%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EManagement%20has%20asked%20me%20to%20create%20a%20form%20on%20excel%20for%20not%20so%20tech-savvy%20people%20to%20fill%20out%20(in%20other%20words%20dumb-proof)%20so%20they%20don't%20accidentally%20mess%20it%20up%20and%20so%20it%20essentially%20holds%20their%20hands%20in%20the%20process%20so%20that%20they%20don't%20skip%20over%20required%20information%20that%20needs%20to%20be%20input.%20I've%20already%20protected%20my%20sheet%20and%20made%20specific%20cells%20editable.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20think%20a%20macro%20can%20help%20me%20out%2C%20but%20I'm%20not%20sure%20if%20macros%20can%20just%20run%20by%20themselves%20like%20conditional%20formatting%20does%20or%20if%20it%20needs%20to%20be%20manually%20ran%20for%20it%20to%20work.%20I'll%20explain%20what%20I%20want%20to%20occur%20so%20you%20can%20better%20understand.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThis%20form%20has%20line%20items%20with%20four%20columns%3A%20General%20Inspection%20Items%2C%20Code%2C%20Inspection%20Comments%20and%20Repair%20Comments.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20want%20the%20macro%20to%20do%20three%20things%20if%20%22X%22%20is%20selected%20in%20the%20Code%20column%3A%3C%2FP%3E%3CP%3E1)%20Highlight%20the%20corresponding%20Repair%20Comments%20cell%20for%20that%20line%20item%20as%20red.%3C%2FP%3E%3CP%3E2)%20A%20message%20box%20to%20appear%20letting%20the%20user%20know%20that%20the%20Repair%20Comments%20cell%20needs%20to%20be%20filled%20in%20with%20a%20brief%20description%20of%20some%20kind.%3C%2FP%3E%3CP%3E3)%20Making%20the%20corresponding%20Repair%20Comments%20cell%20required%20to%20be%20filled%20out%3B%20making%20the%20file%20unable%20to%20be%20saved%20or%20printed%20until%20it%20is%20filled%20(this%20might%20be%20the%20most%20important%20of%20the%20three).%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EOther%20things%20it%20should%20be%20able%20to%20do%20is%20remove%20the%20red%20highlight%20if%20they%20meet%20the%20requirement%20(e.g.%20fill%20in%20the%20Repair%20Comments%20cell%20with%20a%20brief%20description)%20so%20that%20it%20returns%20to%20white%20and%20I%20want%20the%20message%20box%20to%20specify%20which%20line%20item%20to%20fill.%20For%20example%2C%20line%20item%209%20is%20%22Vandalism%3A%20Damage%2C%20graffiti%2C%20glass%2C%20trash%2C%20etc.%22%2C%20so%20if%20it%20said%20the%20number%20and%20name%20of%20the%20line%20item%20that%20would%20be%20great.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EStep%20one%20is%20not%20so%20important%20because%20I%20can%20just%20use%20conditional%20formatting%20to%20do%20this%20(which%20I%20have)%20and%20it%20even%20removes%20the%20highlight%20if%20its%20filled%2C%20but%20I%20figure%20why%20not%20just%20have%20everything%20in%20the%20macro%20if%20I%20can.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20tried%20to%20make%20a%20macro%20for%20the%20message%20box%20and%20succeeded%2C%20but%20I%20don't%20know%20how%20I%20can%20specify%20line%20items.%20Do%20I%20need%20to%20specify%20the%20range%20for%20the%20line%20items%20and%20list%20out%20each%20line%20item%20within%20the%20range%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ENote%3A%20I%20believe%20I%20created%20a%20loop%20macro%2C%20but%20I%20noticed%20that%20the%20macro%20only%20works%20if%20I%20manually%20run%20it.%20In%20my%20test%20I%20filled%20the%20Code%20cell%20with%20an%20X%20but%20I%20did%20not%20receive%20the%20message%20box.%20I%20then%20ran%20the%20macro%20and%20then%20received%20the%20message%20box.%20Is%20there%20a%20way%20to%20automate%20the%20message%20box%20without%20having%20to%20manually%20run%20the%20macro%3F%20I%20don't%20expect%20other%20users%20who%20will%20actually%20fill%20out%20this%20form%20to%20run%20the%20macro%20themselves.%20If%20not%20then%20creating%20the%20macro%20will%20be%20rendered%20useless%20and%20I'll%20just%20have%20to%20stick%20with%20only%20conditional%20formatting%20as%20a%20guide.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThis%20is%20my%20macro%20so%20far%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ESub%20Repair_Comments()%3C%2FP%3E%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3BDim%20Code%20As%20Range%3CBR%20%2F%3E%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3BFor%20Each%20Code%20In%20Range(%22%24H%2415%3A%24I%2432%22)%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20If%20Code.Value%20%3D%20%22X%22%20Then%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3BMsgBox%20(%22Please%20include%20a%20repair%20comment.%22)%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20End%20If%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3BNext%20Code%3CBR%20%2F%3E%3CBR%20%2F%3EEnd%20Sub%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20have%20included%20an%20PNG%20attachment%20of%20the%20sheet%20so%20you%20can%20see%20it%20for%20yourself.%20Note%20that%20the%20cells%20have%20been%20merged%20together.%20B15%3AG15%20makes%20General%20Inspection%20Item%209%20Vandalism%20for%20example.%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20919px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F111167i43A4BDE4B5B0F5E3%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22Daily%20Playground%20Observation.PNG%22%20title%3D%22Daily%20Playground%20Observation.PNG%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAny%20advice%20or%20tips%20will%20help.%20Thank%20you%20to%20anyone%20who%20takes%20the%20time%20to%20read%20and%20analyze%20the%20situation%20and%20gives%20a%20response.%20It%20will%20be%20greatly%20appreciated.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-504790%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EMacros%20and%20VBA%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
ACPNR
Occasional Contributor

Hello everyone,

 

Management has asked me to create a form on excel for not so tech-savvy people to fill out (in other words dumb-proof) so they don't accidentally mess it up and so it essentially holds their hands in the process so that they don't skip over required information that needs to be input. I've already protected my sheet and made specific cells editable.

 

I think a macro can help me out, but I'm not sure if macros can just run by themselves like conditional formatting does or if it needs to be manually ran for it to work. I'll explain what I want to occur so you can better understand.

 

This form has line items with four columns: General Inspection Items, Code, Inspection Comments and Repair Comments.

 

I want the macro to do three things if "X" is selected in the Code column:

1) Highlight the corresponding Repair Comments cell for that line item as red.

2) A message box to appear letting the user know that the Repair Comments cell needs to be filled in with a brief description of some kind.

3) Making the corresponding Repair Comments cell required to be filled out; making the file unable to be saved or printed until it is filled (this might be the most important of the three).

 

Other things it should be able to do is remove the red highlight if they meet the requirement (e.g. fill in the Repair Comments cell with a brief description) so that it returns to white and I want the message box to specify which line item to fill. For example, line item 9 is "Vandalism: Damage, graffiti, glass, trash, etc.", so if it said the number and name of the line item that would be great.

 

Step one is not so important because I can just use conditional formatting to do this (which I have) and it even removes the highlight if its filled, but I figure why not just have everything in the macro if I can.

 

I tried to make a macro for the message box and succeeded, but I don't know how I can specify line items. Do I need to specify the range for the line items and list out each line item within the range?

 

Note: I believe I created a loop macro, but I noticed that the macro only works if I manually run it. In my test I filled the Code cell with an X but I did not receive the message box. I then ran the macro and then received the message box. Is there a way to automate the message box without having to manually run the macro? I don't expect other users who will actually fill out this form to run the macro themselves. If not then creating the macro will be rendered useless and I'll just have to stick with only conditional formatting as a guide.

 

This is my macro so far:

 

Sub Repair_Comments()

     Dim Code As Range

     For Each Code In Range("$H$15:$I$32")
          If Code.Value = "X" Then
               MsgBox ("Please include a repair comment.")
          End If
     Next Code

End Sub

 

I have included an PNG attachment of the sheet so you can see it for yourself. Note that the cells have been merged together. B15:G15 makes General Inspection Item 9 Vandalism for example.Daily Playground Observation.PNG

 

Any advice or tips will help. Thank you to anyone who takes the time to read and analyze the situation and gives a response. It will be greatly appreciated.

Related Conversations