#1
|
|||
|
|||
Help with if then statements
Hey Guys,
I have a user form with a pull-down menu that i would like to insert a new form field with certain pull-down results. Example, if pull-down "DD1" = "1000", then insert form field (text box). Suggestions? Thanks |
#2
|
|||
|
|||
If Me.DD1.Value = "1000" then
ActiveDocument.FormFields.Add Selection.Range, wdFieldFormTextInput End if |
#3
|
|||
|
|||
Thanks Greg for your reply. I get compile error-"invalid use of the keyword Me." I looked that one up and Me is only supposed to used in "class" modules. What can I substitute for it? Thanks-
|
#4
|
|||
|
|||
Me is the userform class object. The code could go in the DD click event or a command button click event. You don't really have to use Me.
If DD1.Value = Should work as well |
#5
|
|||
|
|||
Still stalling on the If line. This is an exit macro when the drop-down is selected. Here's the code, hope I'm posting correctly-if not correct me. Thanks
Code:
Sub LockDD1() ' ' Lock DD1 Macro ' ' Application.ScreenUpdating = False If ActiveWindow.View.Type = wdPageView Then ActiveWindow.ActivePane.View.Type = wdNormalView Else ActiveWindow.View.Type = wdPageView End If If ActiveWindow.ActivePane.View.Type = wdNormalView Then ActiveWindow.ActivePane.View.Type = wdPageView Else ActiveWindow.ActivePane.View.Type = wdNormalView End If If DD1.Value = "1790" Then 'Column box ActiveDocument.FormFields.Add Selection.Range, wdFieldFormTextInput Selection.PreviousField.Select With Selection.FormFields(1) .Name = "Column1790" .Enabled = True With .TextInput .EditType Type:=wdRegularText, Default:="X", Format:= _ "Lower case" .Width = 0 End With End With Selection.MoveRight Unit:=wdCharacter, Count:=1 Selection.TypeText Text:=", " End If End Sub |
#6
|
|||
|
|||
Again, the code I provided would go in the UserForm DD1_Click event or a command button click event of the userform. If you want to use DD1 in an external procedure as you've done then you would need to passed a form object as an argument to the that procedure e.g.,
In a UserForm command button click event procefure LockDD1 Me In an external procedure: Sub LockDD1(oFrm as Object) Msgbox oFrm.DD1.Value End Sub |
#7
|
|||
|
|||
I'm a novice, you just lost me.
|
#8
|
|||
|
|||
You said you had a userform with a dropdown DD1. A userform is a class module with properties, methods and events. If you have a dropdown in your Userform then there is a click event for that dropdown. If you have a command button in the userform then there is a click event for that command button.
Put the code I sent to you in one of those events. Look at the create and employ a userform tips page on my website. |
#9
|
|||
|
|||
I looked up the definition for user form and it appears that I misspoke. I do not have a user form but a Word doc with form fields in it-my bad. Now it makes sense that I was looking for a class module and other things that i don't have.
So I have a Word doc with form fields, and I want a macro to insert an additional form field if one of the items in a pull-down menu is chosen. Thanks for your help, I have a lot to learn- |
#10
|
||||
|
||||
Inserting the new item is easy enough, but you also need to be able to delete it again if the user changes the dropdown selection. The following code should suffice. See attached demo.
Code:
Sub OptTxtFmFld() Dim Prot As Variant, BmkRng As Range Const Pwd As String = "" 'Insert password here Const BmkNm As String = "BkMk" ' The bookmark name With ActiveDocument Prot = .ProtectionType If .ProtectionType <> wdNoProtection Then Prot = .ProtectionType .Unprotect Password:=Pwd End If If .Bookmarks.Exists(BmkNm) Then Set BmkRng = .Bookmarks(BmkNm).Range If .FormFields("Dropdown1").Result = "1000" Then .FormFields.Add BmkRng, wdFieldFormTextInput Else BmkRng.Delete End If .Bookmarks.Add BmkNm, BmkRng Else MsgBox "Bookmark: " & BmkNm & " not found." End If .Protect Type:=Prot, Password:=Pwd, NoReset:=True End With Set BmkRng = Nothing End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#11
|
|||
|
|||
So what you are saying is that this code should be added to the exit macro of the drop-down menu?
|
#12
|
||||
|
||||
From the dropdown formfield's properties, choose the 'OptTxtFmFld' macro for the 'On Exit' property. You will also need a bookmark named 'BkMk' where you want the conditional field to go.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#13
|
|||
|
|||
I need a little explanation on the bookmarks-is it one that exists in the document or the one that will be created by the new form field? I have tried both and I get the message box that they do not exist-Thanks
|
#14
|
|||
|
|||
Just a thought, would it be easier to insert all of the form fields and delete the ones not used when the drop-down is chosen?
|
#15
|
||||
|
||||
The bookmark named 'BkMk' is one you create in the document, via Insert|Bookmark. You can use another name, of course, but you need to use the same name in both the document and the macro.
Quote:
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Multiple If statements, | Jamal NUMAN | Excel | 16 | 11-15-2022 11:59 PM |
Countif statements | Alaska1 | Excel | 5 | 02-05-2015 07:55 PM |
Using IF statements | stuwoolf | Excel | 2 | 01-10-2015 01:58 PM |
count if statements | Alaska1 | Excel | 1 | 05-14-2014 08:21 AM |
IF statements that shifts to right. | kent | Excel | 0 | 01-19-2006 02:23 PM |