Currently using vba with excel 2007...
I am currently testing the capabilities of functions and am a little stuck using buttons. I have two buttons, named ONE and TWO. Pressing either calls up a function Calc with each button passing the variable of the others name. As follows:
Private Sub ONE_Click()
Calc TWO
End Sub
Private Sub TWO_Click()
Calc ONE
End Sub
Function Calc(B As CommandButton)
B.Enabled = False
End Function
My understanding is that pressing button ONE passes the variable TWO to the Function Calc, and then disables button TWO.
I also have a button labeled Reset which serves as follows:
Private Sub Reset_Click()
ONE.Enabled = True
TWO.Enabled = True
End Sub
The results of this, if I press button ONE, button TWO greys out and appears "disabled". However when I hit the reset button, it stays grey. Investigating the properties of the button reveals that it doesn't actually get disabled. I installed another button that directly disables button two, i.e. TWO.Enabled = False, instead of using a variable to do so.
When the button is directly disabled using the direct button, resetting enables the button as it should, and the properties reflect that the button is disabled.
Would anyone know why using a variable to disable a button like this results in an illusionary disabling?
And better yet, how to overcome the issue?
If you have trouble calling through an indirrect reference, then use a static parameter.
Private Sub ONE_Click()
Calc "TWO"
End Sub
Private Sub TWO_Click()
Calc "ONE"
End Sub
Now you can use that parameter to act on the object.
Function Calc(B As String)
If B = "ONE" Then
ONE.Enabled = False
ElseIf B = "TWO" Then
TWO.Enabled = False
Else
Exit Function
End If
End Function
Related
I have a custom button on a ribbon, and I want to create a keyboard shortcut using a class module. I have managed to start it, but I am not able to take the next step, which would be to perform the action. I give you the code:
`
Option Compare Database
Option Explicit
Private WithEvents Form As Access.Form
Private mPresionarTecla As Boolean
Public Property Get PresionarTecla() As Boolean
PresionarTecla = mPresionarTecla
End Property
Public Property Let PresionarTecla(ByVal vNewValue As Boolean)
mPresionarTecla = vNewValue
End Property
Public Sub InitalizeAutokeys(FName As Form)
Set Form = FName
mPresionarTecla = True
If mPresionarTecla = True Then
FName.OnKeyDown = "[Event Procedure]"
End If
End Sub
Private Sub frmCustomForm_KeyDown(KeyCode As Integer, Shift As Integer)
If mPresionarTecla = True Then
rbPegarSinFormato (Screen.ActiveControl)
End If
End Sub
`
I want to create a keyboard shortcut to execute a button on a ribbon. The ribbon button is a procedure created by me, which applies only to formatted text fields, to paste without formatting.
So that you understand me, in the toolbar you have a button, which can be executed by means of the combination of letters control + v. I want to achieve something similar.
Now if I do this through a macro, it's very limited because I can only do a few things, and with VBA I can do a lot more. That is, if I want to add conditions or, most importantly, execute a procedure, not a function, a macro is not worth it. Also, I can select which fields I want to apply it to, that is, since the function I'm using is to copy without formatting, it doesn't make sense for numeric fields to be applied
Currently implementing toggle buttons that controls the visibility of tabs in a subform. When opening the subform on its own, the toggle buttons work, however when I am on the main form, the toggle buttons don't work anymore.
Private Sub Toggle53_Click()
If Me.Toggle53.Value = True Then
Me.IDD.Visible = True
Me.IDS.Visible = True
Else
Me.IDD.Visible = False
Me.IDS.Visible = False
End If
I also tried implementing this code in
Private Sub Form_Current()
for the subform's current event, but it doesn't work either.
Any help is appreciated thanks!
Don't know the structure of your forms, but to hide a tab using a toggle button, you need to reference the tab by index as shown below. Obviously, if the tab control is located in a subform control, you will need to change the reference to it.
Private Sub Toggle1_Click()
TabCtl.Pages(0).Visible = Toggle1.Value
End Sub
If the tab control is located in a subform control:
Private Sub Toggle1_Click()
SubformControl.Form.TabCtl.Pages(0).Visible = Toggle1.Value
End Sub
If all the forms are on the page you could add condittional If on them with booleans to show them, so on toggle click just set everyones boolean to false except the item you want to be visible.
For example create boolean called IDDbool=false, bind it to the subform and change it on click to true to show it.
Also option is to create method on the sub form that controls the tabs and call in that method from the main one.
I have a userform which contain checkboxes and labels.
My goal is to enable or disable specific labels if checkboxes are true or false.
I have:
a module in which I store my functions and subs
a main module
the userform
I could write in the userform:
Private Sub Checkbox1 ()
If Userform.Checbox1 = true then
Userform.label.enable = true
End if
However I have a few checkboxes and labels and I'd like to create a sub or function to simplify my code.
In the module in which I store my function I wrote:
Sub EnableMyLabels(Checkbox as object , Label as object)
If Userform.Checkbox = true then
Userform.label.enable = true
End If
and in my userform I tried to use it like this:
Call EnableMyLabels (Checkbox1 , Label1)
Your Sub should be like
Sub EnableMyLabels(cb As MSForms.CheckBox, lbl As MSForms.Label)
If cb.Value = True Then
lbl.Enabled = True
End If
End Sub
And the call
EnableMyLabels Checkbox1, Label1
You might want to call it from an Event on the Checkbox, eg
Private Sub CheckBox1_Change()
EnableMyLabels CheckBox1, Label1
End Sub
You should try the same principle but using Events.
On your VBE, at the Userform module select "Checkbox1" on the top dropdown.
Then select the method "Click" on the right top dropdown.
A method called Checkbox1_Click should have been created.
That code will run whenever the user clicks the checkbox.
Include all your "label logic" (or logic for other controls too) there.
When you are directly passing the objects (passed byRef by default), then there is no need of the Userform word in your code. Simply drop that and your code should work perfectly.
Sub EnableMyLabels(Checkbox as object , Label as object)
If Checkbox = true then
label.Enabled = true 'fyi - Enabled not Enable
End If
End if
I am using a form with two combo-boxes and a button. I want button to be disabled initially and it should enable only when the user selects a valid value in both the combo-boxes.
I tried to disable the button using the form initialize sub-routine but the button appears to be active. What can be the issue? Also how to enable the button using if conditions?
Private Sub UserForm1_Initialize()
Me.Shapes("ButtonName1").ControlFormat.Enabled = False
ActiveSheet.Shapes("ButtonName1").Font.ColorIndex = 16
End Sub
You made a couple of mistakes in your code.
UserForm1_Initialize should be UserForm_Initialize
You can use Userform.ButtonName1 to access the properties of your button
Use TextFrame.Characters.Font.ColorIndex to access the text on a button on the worksheet
The code below should work
Private Sub UserForm_Initialize()
UserForm1.ButtonName1.Enabled = False
ActiveSheet.Shapes("ButtonName1").TextFrame.Characters.Font.ColorIndex = 16
End Sub
There's a lot of information and a lot that you can do with userforms but I can't really find a standard way to use them. Let's say I have a userform with a standard dropdown list that asks someone to choose a fruit. In the userform code I will put the below code after adding a combo box called fruitcombo:
Private Sub UserForm_Initialize()
userform1.fruitcombo.AddItem "Peach"
userform1.fruitcombo.AddItem "Pear"
userform1.fruitcombo.AddItem "Grape"
End Sub
I will also add a commandbutton which will be labeled "Submit" and in that event:
Private Sub Submit_Click()
Me.Hide
End Sub
That's where it starts to get hazy. What's the best way to capture the answer that was selected? One way I can think would be to make a global variable called fruitanswer and then instead of the Me.Hide we can skip straight to Unload Me
Ex:
Private Sub Submit_Click()
fruitanswer = fruitcombo.value
Unload Me
End Sub
Or we can have fruitanswer as a private variable in the module where the userform is called and then unload it in there. There are also multiple ways to initialize the userform. I'm also wondering the best way to initialize it. The Show method will automatically initialize it, but the Hide method WON'T automatically de-initialize it. For that, the Unload statement is necessary. So does anyone initialize it before calling the Show method using the Load statement?
Ex:
Load userform1 'Any point to including this?
userform1.show
'user chooses a fruit and clicks submit button
'userform is hidden by commandbutton but not unloaded yet
fruitanswer = fruitcombo.value
Unload userform1
Out of these options, which is the best method? Is there anything to make it more efficient?
You can wrap the whole thing in a single function call:
strFruit = UserForm1.GetFruit()
Then, in your UserForm, have it do the work of displaying and unloading itself like so:
Private bOK As Boolean
Public Function GetFruit() As String
bOK = False
Me.Show vbModal
If bOK Then GetFruit = ComboBox1.Text
Unload Me
End Function
Private Sub cmdOK_Click()
bOK = True
Me.Hide
End Sub
Private Sub cmdCancel_Click()
Me.Hide
End Sub
This assumes you have buttons named cmdOK and cmdCancel and a combobox named ComboBox1.
Since Show() is being called modally, the code after it won't execute until the form is closed or hidden. When either button is clicked (or the form is closed by other means) then the code continues, the selected text is returned (if OK was clicked), and the form unloads itself.
The beauty of doing it this way is that your calling code doesn't need to worry about instantiating and destroying the form each time it's called. It's just a single statement to load the form and get the return value.
Of course, you'll need to add the code to populate the combobox with whatever items you wish to display.