I am trying to adapt the code found here (the third method, as that is what seems to be deemed best-practice), to suit my needs, but I'm not having any luck - the code I have so far, more or less copy-pastaed from that page is the following:
In the form-module:
Private calling_cell As Range
Property Set range_to_form(ByRef r As Range)
Set calling_cell = r
End Property
Private Sub UserForm_Activate()
Debug.Print calling_cell.Address
End Sub
In a worksheet_change-event:
Dim frm As ufRegLuft
If Not IsUserFormLoaded("ufRegLuft") Then
Set frm = New ufRegLuft
Else
Set frm = VBA.UserForms("ufRegLuft")
End If
Set frm.range_to_form = Target
ufRegLuft.Show
The problem:
This does not work - I get an error on the debug.print-line, saying "Run-time error '91': Object variable or With block variable not set". I must admit I'm pretty stumped at this point, I feel like I have tried every possible combination of set, let, get etc. So, can anyone here please help me figure out if it is possible to pass a Range-object to an userform, and if so, tell me what I'm doing wrong?
Your variable calling_cell is initialized in the form frm and not in ufRegLuft
Change the line ufRegLuft.Show to frm.Show ;)
Here is simple way to test it.
Form Module
Private calling_cell As Range
Property Set range_to_form(ByRef r As Range)
Set calling_cell = r
End Property
Private Sub UserForm_Activate()
MsgBox calling_cell.Address
End Sub
Normal Module
Sub ShowFormProp()
Dim frm As ufRegLuft
Set frm = New ufRegLuft
Set frm.range_to_form = Sheet1.Range("A1")
frm.Show
End Sub
You have to pass the variable to a sub in the form, (I believe user form_Initialize will fire)
I am not sure where r is set but create a public sub in the form that sets the range, call that before you show the form then show the form like below
In the form-module:
Private calling_cell As Range
public sub range_to_form(ByRef r As Range)
Set calling_cell = r
End Property
Private Sub UserForm_Activate()
Debug.Print calling_cell.Address
End Sub
In a worksheet_change-event:
Dim frm As ufRegLuft
If Not IsUserFormLoaded("ufRegLuft") Then
Set frm = New ufRegLuft
Else
Set frm = VBA.UserForms("ufRegLuft")
End If
frm.range_to_form r
'Could be "range_to_form r" not above
frm.Show
Related
I have code on a userform that contains several checkboxes and several DTPickers.
The code looks like so:
Private Sub CheckBox11_Click()
If CheckBox11.Value = True Then
DTPicker22.Enabled = True
Else
DTPicker22.Enabled = False
End If
End Sub
Private Sub CheckBox12_Click()
If CheckBox12.Value = True Then
DTPicker24.Enabled = True
Else
DTPicker24.Enabled = False
End If
End Sub
The Userform contains a lot of checkboxes that have clauses next to them. Upon their completion the DTPicker will enable entering the date of completion.
Whilst this does what I want, it only enables one DTPicker when the checkbox is ticked per private sub. There has to be some way to make this so I wouldn't need to create different private subs for every checkbox click event.
Could you also tell me where to put it, as in, what event?
A "control array" is the typical approach for something like this.
See:
http://www.siddharthrout.com/index.php/2018/01/15/vba-control-arrays/
eg:
Class module clsEvents
Option Explicit
'Handle events for a checkbox and a date control, associated with a worksheet cell
Private WithEvents m_CB As MSForms.CheckBox
Private WithEvents m_DP As DTPicker
Private m_dateCell As Range
'set up the controls and the cell
Public Sub Init(cb As MSForms.CheckBox, dp As DTPicker, rng As Range)
Set m_CB = cb
Set m_DP = dp
Set m_dateCell = rng
If rng.Value > 0 Then
cb.Value = True
m_DP.Value = rng.Value
Else
cb.Value = False
End If
m_DP.CustomFormat = "dd/MM/yyyy"
End Sub
Private Sub m_CB_Change()
m_DP.Enabled = (m_CB.Value = True)
End Sub
Private Sub m_DP_Change()
m_dateCell.Value = m_DP.Value 'update the cell
End Sub
Userform:
Option Explicit
Dim colObj As Collection 'needs to be a Global to stay in scope
Private Sub UserForm_Activate()
Dim obj As clsEvents, i As Long, ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Sheet1")
Set colObj = New Collection
'loop over controls and create a class object for each set
' 3 pairs of controls on my test form...
For i = 1 To 3
Set obj = New clsEvents
obj.Init Me.Controls("CheckBox" & i), _
Me.Controls("DTPicker" & i), _
ws.Cells(i, "B")
colObj.Add obj
Next i
End Sub
The first thing I'd recommend is following a proper naming convention. "CheckBox11" and "DTPciker1" are really vague and once you get further into your code, you'll forget which control is which. I would recommend naming them something that relates the two control together, like "firstDate" and "firstDateDTP". My alternate answer below uses this approach.
You could make a public function that enables the DTPicker based upon the checkbox's value.
Public Function EnableDTPicker(myPicker as String, enableBool as Boolean)
UserFormName.Controls(myPicker).Enabled = enableBool
End Function
Then, you can call the function in your CheckBox123_Click() subs like this:
Private Sub CheckBox123_Click()
EnableDTPicker("thePickerName", CheckBox123.Value)
End Sub
Alternatively, you could make a timer event that runs x number of seconds that just loops through the controls and performs the checks as needed. See this page on how to set up the timer. Using the code in the link shown, You could do something along the lines of:
'Put this in Workbook events
Private Sub Workbook_Open()
alertTime = Now + TimeValue("00:00:01")
Application.OnTime alertTime, "EventMacro"
UserForm1.Show
End Sub
'Put this in a Module
Public Sub EventMacro()
With UserForm1
For each ctrl in .Controls
If TypeName(ctrl) = "CheckBox" Then
'The code below assumes the naming convention outlined above is followed
.Controls(ctrl.Name & "DTP").Enabled = ctrl.Value
End If
Next ctrl
End With
alertTime = Now + TimeValue("00:00:01")
Application.OnTime alertTime, "EventMacro"
End Sub
I have an ActiveX Command Button that when pressed opens a userform to allow data entry into the word document. This button needs to remain visible when working on the document but not visible when printing.
How can I hide/make invisible only when printing?
Unlike in Excel VBA where the properties include an option to 'PrintObject', word VBA does not have this functionality. The best I have been able to do is delete the button after being clicked but this is not really what I want.
'Needs to hide button only on printing, not delete it
UserForm2.Show
CommandButton1.Select
Selection.Delete
I assume you are having ActiveX Command Button in word and using user form entered data gets feed in corresponding fields and you are closing user form and then trying to print document and printed file should not have ActiveX Command Button in it
Paste the following code into CommandButton_Click event
Private Sub CommandButton1_Click()
With ActiveDocument
.Shapes(1).Visible = msoFalse
.PrintOut Background:=False
.Shapes(1).Visible = msoTrue
End With
End Sub
#ille P. - You should post a new question, perhaps with a link to this one.
Try the Shapes collection as well as inlineshape collection.
The following is suggested by Ibby on the Microsoft Word MVP VBA FAQ pages:
Private Sub CommandButton1_Click()
With ActiveDocument
.Shapes(1).Visible = msoFalse
.PrintOut Background:=False
.Shapes(1).Visible = msoTrue
End With
End Sub
So translating to the collection:
Dim oShape as Shape
For Each oShape in ActiveDocument.Shapes
oShape.Visible = False
Next oShape
That would, though hide all shapes, not just your buttons.
You could add bookmarks to your buttons and use them as a filtering Range.
After investigating what happens with the CommandButtons, I detected that in the document they are included (if it has not been edited to be a Shape) in the InLineShape collection by being encapsulated in an InLineShape object.
It would be great if we could directly typecast from CommandButton to InShapeLine but I think Microsoft doesn't allow it, too bad.
The idea is to create two class modules:
One will be an event manager to capture the 'before printing' event and another.
The other will be an encapsulation of an InLineShape object that will have the methods to make the object visible, taking advantage of the Brightness property of the PictureFormat object.
CLASS clsButton
Option Explicit
Private M_ishpButton As InlineShape
Public Property Get button() As InlineShape
Set button = M_ishpButton
End Property
Public Property Set button(oObj As InlineShape)
Set M_ishpButton = oObj
End Property
Public Property Get Visible() As Boolean
Visible = Not bIsHidden
End Property
Public Property Let Visible(bValue As Boolean)
Dim oPictureFormat As PictureFormat
Set oPictureFormat = M_ishpButton.PictureFormat
If bValue Then
Call show
Else
Call hide
End If
End Property
Private Function bIsHidden() As Boolean
Dim oPictureFormat As PictureFormat
Set oPictureFormat = M_ishpButton.PictureFormat
If oPictureFormat.Brightness = 1 Then bIsHidden = True: Exit Function
bIsHidden = False
End Function
Private Sub hide()
Dim oPictureFormat As PictureFormat
Set oPictureFormat = M_ishpButton.PictureFormat
oPictureFormat.Brightness = 1
End Sub
Private Sub show()
Dim oPictureFormat As PictureFormat
Set oPictureFormat = M_ishpButton.PictureFormat
With oPictureFormat
.Brightness = 0.5
.Contrast = 0.5
End With
End Sub
CLASS clsEvents
Option Explicit
Public WithEvents appWord As Word.Application
Public WithEvents docWord As Word.Document
Private m_button As New clsButton
Private Sub appWord_DocumentBeforePrint(ByVal Doc As Document, Cancel As Boolean)
'If process is not cancel and button is not visible
With m_button
If Cancel = False And .Visible = True Then
.Visible = False
End If
End With
End Sub
Private Sub appWord_WindowDeactivate(ByVal Doc As Document, ByVal Wn As Window)
'If button is not visible then set to true
With m_button
If .Visible = False Then
.Visible = True
End If
End With
End Sub
Public Property Set button(oObj As clsButton)
Set m_button = oObj
End Property
CLASS ThisDocument
Now, in ThisDocument class should create objects and linking
Dim oEventsManager As New clsEvents
Dim oEditedButton As New clsButton
Const BUTTON_LINKS As String = "cmdUpdateLinks" 'For example
Dim oInShpDoc As Word.InlineShape, oOleDoc As Word.OLEFormat, oInShapesDoc As Word.InlineShapes
Public Sub Set_Manager_Events()
Set oEventsManager.appWord = Word.Application 'ThisDocument.Application
Set oEventsManager.docWord = ThisDocument
Set oInShpDoc = FNC_oGet_Button_Variable(BUTTON_LINKS)
If Not oInShpDoc Is Nothing Then
Set oEditedButton.button = oInShpDoc
Set oEventsManager.button = oEditedButton
End If
End Sub
'###### EVENTOS OF BUTTON
Private Sub cmdUpdateLinks_Click()
If oEventsManager.appWord Is Nothing Then Call Set_Manager_Events
Call UpdateLinks ' Is one example
End Sub
Public Function FNC_oGet_Button_Variable(sCodeName As String) As InlineShape
Dim oForm As InlineShape, oFormsInLine As InlineShapes, oOLEFormat As OLEFormat
Set oFormsInLine = ThisDocument.InlineShapes
If oFormsInLine .Count < 1 Then GoTo bye
i = 0
For Each oForm In oFormsInLine
With oForm
Set oOLEFormat = .OLEFormat
If Not oOLEFormat Is Nothing Then
If InStr(1, oOLEFormat.ClassType, "CommandButton") > 0 Then
If .OLEFormat.Object.Name = sCodeName Then
Set FNC_oGet_Button_Variable= oForm
Exit Function
End If
End If
End If
End With
Next
bye:
Set FNC_oGet_Button_Variable = Nothing
End Function
With this you will can hide button for printing.
I would like to have a piece of code that runs when any worksheet commandbutton is pressed. I have seen similar things for userforms but can't find something that works across (and limited to) worksheet buttons.
I have got as far as creating a class module called clsCmdButton with the following code:
Private WithEvents cmdButton As MSForms.CommandButton
Private Sub cmdButton_Click()
'code i want to run
End Sub
I then think I need to loop through all worksheets, find the commandbuttons on it and intialise each as a new insance of the class. This could be done in the Workbook_Open event, however I am not sure how to code it. Could anyone help?
Thanks!
I'd amend your class like this:
Private WithEvents m_ctlButton As MSForms.CommandButton
Private Sub Class_Terminate()
Set m_ctlButton = Nothing
End Sub
Private Sub m_ctlButton_Click()
'code i want to run
End Sub
Public Property Set Button(ctlButton As MSForms.CommandButton)
Set m_ctlButton = ctlButton
End Property
Let's assume this is Class1 as I'm lazy.
Now in a new module add this:
Option Explicit
Dim colButtons As Collection
Sub hookButtons(ws As Object)
Dim oBtn As Class1
Dim obj As OLEObject
Set colButtons = New Collection
For Each obj In ws.OLEObjects
If TypeOf obj.Object Is MSForms.CommandButton Then
Set oBtn = New Class1
Set oBtn.Button = obj.Object
colButtons.Add oBtn
End If
Next
End Sub
and finally, in the ThisWorkbook module, add this:
Private Sub Workbook_Open()
hookButtons ActiveSheet
End Sub
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
hookButtons Sh
End Sub
Since you can't click a button without its sheet being active, it seems easier to only hook the active sheet's buttons at any given time. Using the sheetActivate event should also mean that it gets reset more often in case of unhandled errors in other code in the workbook.
I am creating a program which has several UserForms.
At the end of the program I need to clear every Checkbox inside some UserForm. I have created a Function, but it cannot recognise which UserForm it should clear, can you help me there? Here is the code:
Function ClearUserForm(ByVal userf As String)
Dim contr As Control
For Each contr In userf.Controls
If TypeName(contr) = "CheckBox" Then
contr.Value = False
End If
Next
End Function
And I am calling the function like this, for example:
ClearUserForm ("UserForm2")
It seems not to recognize which UserForm it should act upon.
Shai Rado's advice is good and you should have a look at how he creates the object from its 'key'.
I only post this answer to check if you're aware that you could pass the object itself in the call. So your code could be like so:
Option Explicit
Public Sub RunMe()
ClearCBoxes UserForm1
End Sub
Private Sub ClearCBoxes(frm As MSForms.UserForm)
Dim ctrl As Control
For Each ctrl In frm.Controls
If TypeOf ctrl Is MSForms.ComboBox Then
ctrl.Value = False
End If
Next
End Sub
You don't need a Function (since you are not returning any arguments), in your case a Sub will do.
You need to qualify an Object to the User_Form selected by using:
Set objUserForm = UserForms.Add(userf)
Whole code
(Tested)
Option Explicit
Sub ClearUserForm(ByVal userf As String)
Dim contr As Control
Dim objUserForm As Object
Set objUserForm = UserForms.Add(userf)
For Each contr In objUserForm.Controls
If TypeName(contr) = "CheckBox" Then
contr.Value = False
End If
Next
' just to check that all checkboxes are cleared
objUserForm.Show
End Sub
I have a macro code behind Worksheet. When button is clicked on the sheet, new user form is initialised and showed to user. If user closes the windows with red X, or form is closed with "hide" function/method, all global variables that are behind Worksheet loses their values. Is it possible to preserve this values?
Worksheet code behind:
Private MeasurementCollection As Collection
Dim CurrentMeasurement As measurement
Dim NewMeasurement As measurement
Private Sub Worksheet_Activate()
Initialize
End Sub
Public Sub Initialize()
Set NewMeasurement = New measurement
Dim DropDownDataQueries As Collection
Set DropDownDataQueries = DBQueries.GetAllUpdateQueries
For i = 1 To DropDownDataQueries.Count
Dim Values As Collection
Set Values = DataBase.GetData(DropDownDataQueries(i))
With Me.OLEObjects("Combo" & i).Object
For Each value In Values
.AddItem value
Next value
End With
Next i
End Sub
Private Sub UpdateDB_Click()
UpdateGeneralData
If (CurrentMeasurement Is Nothing) Then
MsgBox ("Message text")
Else
Dim form As UpdateComentForm
Set form = New UpdateComentForm
form.Show
End If
End Sub
Private Sub Combo1_Change()
If Application.EnableEvents = True Then
If (Combo1.value <> "") Then
NewMeasurement.DN = Combo1.value
Else
NewMeasurement.DN = 0
End If
End If
End Sub
UserForm code
Private Sub UpdateDBData_Click()
If (Komentar.value <> "") Then
Me.Hide
Else
MsgBox ("Prosimo napiĊĦite vzrok za spremembe podatkov v belo polje!")
End If
End Sub
Private Sub UserForm_Terminate()
Me.Hide
End Sub
Experiments show that the module-level variables are cleared upon exiting a procedure that involves calling = New Form, provided that the form designer window is opened somewhere in the IDE.
Close all user forms designer windows you might have open in the VBA IDE and try again.
NewMeasurement as been declared but never assigned.
You could do something like Dim NewMeasurement As New measurement to create an instance of the object.