#1
|
|||
|
|||
Run VBA code when text box value changes
I am trying to run some VBA code within an active document. I have placed two text boxes on the document and want one to be populated with a specific value when the other is populated. this is just a trial and two boxes wont be used.
i have tried If textbox1.value > 100 then textbox2.value = "In spec" elseif textbox1.value < 100 then textbox2.value = "Out of spec" end if i dont know how to get this to run when textbox1 is populated on the active word document. when i select this document in project then document i dont know what decleration will make the code run. i tried contentcontrolonenter but that didnt work any help will be appreciated. |
#2
|
||||
|
||||
Are you using a vba userform or are the text boxes in the document itself?
When you say this is just a trial and you won't be using two boxes, what is the point of wanting code that uses two boxes? ContentControlOnExit would make a lot more sense if this is in the document itself (and it doesn't require any text boxes).
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#3
|
|||
|
|||
i just want to be able to populate a text box when a specific value is entered. currently the document has a combo box with two options and an area where a value is entered. the user chooses if the value is in spec or not. sometimes this part is forgotten so i want to be able automatically change the result if the value entered is within spec. i don't know how to do it if it's on the document itself. i have placed two boxes onto the document to see it working. if there is a way of doing it without using any text boxes then that would be better
|
#4
|
||||
|
||||
TextBox has a specific meaning which doesn't appear to be what you are looking for in this instance.
Try this: Set up your document with two Plain Text Content Controls. Set the title of one to "Input Number", set the other title to "Status" Add this macro to your ThisDocument module Code:
Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean) Dim aCC As ContentControl, sMsg As String If ContentControl.Title = "Input Number" Then If IsNumeric(ContentControl.Range.Text) Then If CLng(Trim(ContentControl.Range.Text)) > 100 Then sMsg = "In Spec" Else sMsg = "Out of Spec" End If Else sMsg = "Needs a number greater than 100" End If For Each aCC In ActiveDocument.SelectContentControlsByTitle("Status") aCC.Range.Text = sMsg Next aCC End If End Sub
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
Thread Tools | |
Display Modes | |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
AutoOpen code to change date code to date text | Legal Learning Center | Word VBA | 6 | 02-22-2020 02:24 PM |
Wrap text code not working | Bluebell | Word VBA | 5 | 03-25-2018 04:26 AM |
VBA Code to search for field codes with certain text before the Field code and to change style | welcometocandyland | Word VBA | 4 | 02-08-2017 06:53 PM |
Help with VBA code to find and highlight text | mpdsal | Word VBA | 8 | 09-11-2014 03:55 PM |
Code Request - Text to Columns | OTPM | Excel Programming | 0 | 02-13-2013 04:44 AM |