Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 12-02-2021, 02:34 PM
garydp garydp is offline Run VBA code when text box value changes Windows 10 Run VBA code when text box value changes Office 2019
Novice
Run VBA code when text box value changes
 
Join Date: Dec 2021
Posts: 2
garydp is on a distinguished road
Default 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.
Reply With Quote
  #2  
Old 12-02-2021, 04:35 PM
Guessed's Avatar
Guessed Guessed is offline Run VBA code when text box value changes Windows 10 Run VBA code when text box value changes Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,932
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
Reply With Quote
  #3  
Old 12-03-2021, 12:16 AM
garydp garydp is offline Run VBA code when text box value changes Windows 10 Run VBA code when text box value changes Office 2019
Novice
Run VBA code when text box value changes
 
Join Date: Dec 2021
Posts: 2
garydp is on a distinguished road
Default

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
Reply With Quote
  #4  
Old 12-05-2021, 02:38 PM
Guessed's Avatar
Guessed Guessed is offline Run VBA code when text box value changes Windows 10 Run VBA code when text box value changes Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,932
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Run VBA code when text box value changes 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
Run VBA code when text box value changes 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
Run VBA code when text box value changes 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

Other Forums: Access Forums

All times are GMT -7. The time now is 11:35 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft