Excel Userform Iframe Autoscroll - excel

What I have is a user form with an image a iframe with multiple input fields. I have created a scrollable region as all the input fields do not fit on the userform. What happens is when i click a certain part of the image the iframe will scroll downwards to the input box applicable an set foceu. This works great using the script below, Now my question is how do i set it to go horizonatal instead of vertical now
Sub scrollFrame(topVisibleControl As Object)
' Declares sub named scrollFrame and (procedure scoped) variable named topVisibleControl
Frame10.ScrollLeft = ScrollTop = topVisibleControl.Top
End Sub
I call the function with
Call scrollFrame(ComboBoxga37)

Trial An Error I figured out if anyone sels interested here is the script below
Sub scrollFrame(leftVisibleControl As Object)
' Declares sub named scrollFrame and (procedure scoped) variable named topVisibleControl
Frame10.ScrollLeft = leftVisibleControl.Left
End Sub

Related

Hide/unhide multiple shapes Excel VBA

I am very new to excel VBA & macros and I am trying to do something that I think is very simple, but I cannot for the life of me figure it out.
I have a shape ("shape 1") that when clicked should show/unhide two shapes ("shape 2" and "shape 3").
By default "shape 2" and "shape 3" should be hidden and only appear when "shape 1" is selected.
Any help will be much appreciated, remembering I am a complete novice here!
Edit:
I have managed to use the below, essentially a copy paste from here, I dont know what it means but it works for the single button. I cant figure out how to extend the code to include multiple objects being shown/hidden. An example of a second object to be shown/hidden at the same time as "july_2022" is "august_2022".
Public HIDE As Boolean
Sub fy ()
ActiveSheet.Shapes("july_2022").Visible = HIDE
If ActiveSheet.Shapes("july_2022").Visible = False Then
HIDE = True
Else
HIDE = False
End If
End Sub
ActiveSheet.Shapes("july_2022").Visible = HIDE is the part that sets the visibility of
a shape (july_2022). Another identical line but with something other than july_2022 would affect a second shape. The rest of the code (If.. Then.. Else.. End If) could be replaced with HIDE=Not(HIDE).
For example, the following code when run will 'toggle' the visibility of two shapes on the Active Sheet called 'Shape2' and 'Shape3'.
Public HIDE As Boolean
Sub fy()
ActiveSheet.Shapes("Shape2").Visible = HIDE
ActiveSheet.Shapes("Shape3").Visible = HIDE
HIDE = Not (HIDE)
End Sub

How can I get a reference to a TextBox itself instead of to its default value in Excel vba?

Windows 10 Pro 64, Office 365 Excel vba
ClassCounter is a class that operates on a Long value it stores internally and then displays that value in a TextBox in an active UserForm. I want to be able to assign the TextBox to the ClassCounter object dynamically, so that I can use the same class to instantiate a number of objects, each of which references its own TextBox in the same UserForm.
It has the following private members declared at the class level:
Private mctrLinked As ClassCounter
Private mnCount As Long
Private mtbxDisplay As TextBox
The Initialize subroutine makes the connection between displayTextBox (the textbox used to display the value) and the object. linkCounter provides the opportunity to link to another object of the same class and fire off the same operation on it in a daisychain fashion.
Public Sub Initialize(ByRef displayTextBox As msforms.TextBox, Optional linkCounter As ClassCounter = Nothing)
Const kstrMethodName As String = "Initialize"
Set mtbxDisplay = displayTextBox
Set mctrLinked = linkCounter
Clear
End Sub ' Initialize
The class is instantiated in another class, as follows:
Private mobjAllBlankCounter As New ClassCounter
Private mobjAllEnteredCounter As New ClassCounter
Private mobjAllFoundCounter As New ClassCounter
Private mobjAllIssuesCounter As New ClassCounter
...
And the connection between the TextBox and the ClassCounter object is established by calling the Initialize subroutine in this way:
Public Sub InitializeAllCounters()
With mufMCP
mobjAllBlankCounter.Initialize .tbxBlankCountAll
mobjAllEnteredCounter.Initialize .tbxEnteredCountAll
mobjAllFoundCounter.Initialize .tbxFoundCountAll
mobjAllIssuesCounter.Initialize .tbxIssuesCountAll
End With
End Sub ' InitializeAllCounters
where mufMCP is the UserForm in which the TextBoxes are defined.
Ultimately, the Increment function (and others like it) will operate on the stored variable and then display it in the referenced TextBox as follows:
Public Sub Increment()
Const kstrMethodName As String = "Increment"
If (mtbxDisplay Is Nothing) Then
Err.Raise gknErrNoControlForCounter, mkstrModuleName & "." & kstrMethodName, "Attempt to Increment Counter with no associated control."
Else
mnCount = mnCount + 1
mtbxDisplay.Text = CStr(mnCount)
If (Not (mctrLinked Is Nothing)) Then
mctrLinked.Increment
End If
End If
End Sub ' Increment
The problem I'm having is in the Initialize subroutine where I attempt to assign the value of the TextBox argument to the local variable. Instead of assigning a reference to the TextBox itself, the right side of the assignment is evaluating to the TextBox's default value, which is its Text property. As a result, I get a type mismatch error.
How can I get it to evaluate to a reference to the TextBox itself? I've spent a couple of days searching for the answer and found several sources that said that using ByRef displayTextBox As msforms.TextBox to define the parameter would do the trick, but I'm still getting the control's default value.
As FaneDuru writes, TextBox and MsForms.TextBox are two different object types. A TextBox is a textbox (Form Control, not Active X Control) placed on a sheet. A MsForms.TextBox is a textbox places on a user form.
Bad thing (1): The name "Form Control" related to sheet controls is misleading, it is not the same as a control placed on a user form.
Bad thing (2): As the Form Control Textbox is no longer available from the Developer menu, it is not easy to proof. If you are interested: You can still create them using VBA.
Dim tb1 As TextBox
Set tb1 = ActiveSheet.TextBoxes.Add(255, 243, 73.5, 22.5)
Dim tb2 As msforms.TextBox
UserForm1.Show False
Set tb2 = UserForm1.TextBox1
Checking both objects in the Locals window of the VBA editor, type for both is displayed as "Textbox/Textbox". However, when you look at the properties of the objects, you see that they are different.
So declare all your (userform) controls with the suffix msforms to be sure that you are dealing with the right object types.

Calling a VBA form from a button causes UserForm_Initialize to run twice, breaking my code?

Hello wonderful VBA community,
I'm still really new to vba and am trying to learn a lot. Thank you in advance for looking through my code and my description of the issue I'm facing.
I have a button on a page that calls a new Userform.
CODE SNIPPET 1:
Sub btnShowDetails_Click()
Call frmShowDeets.ShowDeets
End Sub
... which calls the next bit of code in the 'frmShowDeets' UserForm:
CODE SNIPPET 2:
Public Sub ShowDeets()
Dim frm As frmShowDeets
Set frm = New frmShowDeets 'this line triggers the Userform_Initialize() event below
frm.Show
End Sub
... triggering:
CODE SNIPPET 3:
Private Sub UserForm_Initialize()
Dim comboBoxItem As Range
For Each comboBoxItem In ContactList.Range("tblContactList[CompanyName]")
'^refers to unique values in a named range
With Me.boxCompanySelection
.AddItem comboBoxItem.Value
End With
Next comboBoxItem
End Sub
So at this point, the form I want to display has values loaded in its one combobox for user selection. The user selects a company and the Combobox_Change event triggers other routines that pull information for that company.
CODE SNIPPET 4:
Public Sub boxCompanySelection_Change()
Call frmShowDeets.PullData
End Sub
Sub PullData()
Dim numCompanies As Long
numCompanies = ContactList.Range("B6").Value 'this holds a count of the rows in the named range
Dim FoundCell As Range
Set FoundCell = ContactList.Range("tblContactList[Company Name]").Find(What:=boxCompanySelection.Text, LookIn:=xlValues, LookAt:=xlWhole)
Dim CompanyRow As Long
CompanyRow = FoundCell.Row
With ContactList
'pull a bunch of the company's details
End With
End Sub
Here is where it gets weird... Once the form is shown and the user selects one of the combo box items, triggering the Combobox_Change event the code breaks because the 'What:=boxCompanySelection.Text' part of the Range().Find method reads as "" empty (even though Code Snippet 3 is meant to load in company names and Code Snippet 4 is only triggered when the user selects one of those company names from the combobox) and I shouldn't need to build something to handle 'not found' exceptions since the only possible values should be the ones pulled in from my named range.
From stepping through the code, I have determined that for some reason, Code Snippets 2 and 3 run TWICE before Snippet 4 is run. Does anyone know what about my code is causing this to happen? I'm thinking there's a disconnect between the form that is shown and loaded with combobox values and whatever Code Snippet 4 is reading data from.
What is weirder is that if I run the code starting from Code Snippet 2 (ignoring the button call in Code Snippet 1), the form works as intended and from what I can tell 2 and 3 are only run once.
The problem is probably something simple I'm overlooking but I just cannot figure out what it is. Thanks again!
You have to understand that a form is an object - exactly as any other class module, except a form happens to have a designer and a base class, so UserForm1 inherits the members of the UserForm class.
A form also has a default instance, and a lot of tutorials just happily skip over that very important but rather technical bit, which takes us exactly here on Stack Overflow, with a bug involving global state accidentally stored on the default instance.
Call frmShowDeets.ShowDeets
Assuming frmShowDeets is the name of the form class, and assuming this is the first reference to that form that gets to run, then the UserForm_Initialize handler of the default instance runs when the . dot operator executes and dereferences the object. Then the ShowDeets method runs.
Public Sub ShowDeets()
Dim frm As frmShowDeets
Set frm = New frmShowDeets 'this line triggers the Userform_Initialize() event below
frm.Show
End Sub
That line triggers UserForm_Initialize on the local instance named frm - which is an entirely separate object, of the same class. The Initialize handler runs whenever an instance of a class is, well, initialized, i.e. created. The Terminate handler runs when that instance is destroyed.
So ShowDeets is acting as some kind of "factory method" that creates & shows a new instance of the frmShowDeets class/form - in other words whatever happened on the default instance is irrelevant beyond that point: the object you're working with exists in the ShowDeets scope, is named frm, and gets destroyed as soon as it goes out of scope.
Remove the ShowDeets method altogether. Replace this:
Call frmShowDeets.ShowDeets
With this:
With New frmShowDeets
.Show
End With
Now the Initialize handler no longer runs on the default instance.
What you want, is to avoid using the default instance at all. Replace all frmShowDeets in the form's code-behind, with Me (see Understanding 'Me' (no flowers, no bees)), so that no state ever accidentally gets stored in the default instance.
Call frmShowDeets.PullData
Becomes simply:
Call Me.PullData
Or even:
PullData
Since Call is never required anywhere, and the Me qualifier is always implicit when you make a member call in a class module's code.

VB6 Automation Error when calling Add on previously created MultiPage

I want to generate a bunch of MultiPages and create new Pages dynamically in my app, but i'm getting Run-time error '-2147417848 (80010108)': Automation error The object invoked has disconnected from its clients.
Steps to reproduce
In a Class Module named TestClass:
Public WithEvents TestMultiPage As MsForms.MultiPage
Sub createPage()
TestMultiPage.Add
End Sub
In a UserForm named TestForm:
Dim TestInstances as New Collection
Private Sub UserForm_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X as Single, ByVal Y as Single)
If Button = fmButtonRight Then
Dim TestInstance as New TestClass
Set TestInstance.TestMultiPage = Me.Controls.Add("Forms.MultiPage.1")
TestInstances.Add TestInstance
End If
End Sub
Private Sub UserForm_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Dim TestInstance As TestClass: Set TestInstance = TestInstances(1)
TestInstance.createPage
End Sub
When i right-click the UserForm twice, i get two MultiPages. Then i double-click the UserForm, expecting the first MultiPage to have a new Page. But i hit the automation error at TestInstance.createPage -> TestMultiPage.Add, even though all the variables seem present from the Locals window.
What am i missing?
Conclusion
Following #GSerg's answer, i suppose there's no way to do this with MultiPage.
Instead i have to use TabStrip instead and emulate the other behaviour of MultiPage.
Just to add some context, i was trying to create a browser-like UI with windows and tabs (a TabStrip at the bottom representing different windows, each window corresponding to a MultiPage with multiple tabs). I hit the obscure error when switching back to a previous MultiPage and creating a new tab.
There appears to be a problem in MSForms, where it cripples the existing MultiPage controls when a new one is added. To reproduce the problem, you don't need collections, arrays, classes, or even variables:
Sub Reproduce()
Me.Controls.Add "Forms.MultiPage.1", "TestInstance1"
Me.Controls("TestInstance1").Add ' That works
Me.Controls.Add "Forms.MultiPage.1", "TestInstance2"
Me.Controls("TestInstance1").Add ' Now it does not
Me.Controls("TestInstance2").Add ' But the new shiny one does
Me.Controls.Add "Forms.MultiPage.1", "TestInstance3"
Me.Controls("TestInstance2").Add ' Now the instance 2 is also defunct
Me.Controls("TestInstance3").Add ' Only the latest one works
End Sub
I do not know why that is so. It looks like a bug in MSForms.
The controls work fine otherwise, and their properties are accessible, you just can't call Add anymore.

Disable button on a userForm

I'm trying to figure out how to disable a button within my userForm if a certain cell within my spreadsheet equals a certain number. I tried the code stated below, but it isn't working.
Private Sub UserForm_Initialize()
Label2 = Sheets("DATA").Range("AM2").Value
Label4 = Sheets("DATA").Range("AO2").Value
Label7 = Format(Sheets("DATA").Range("R8").Value, "Currency")
If Sheets("DATA").Range("AL10").Value = 10 Then
ActiveSheet.Shapes("CommandButton1").Select
UserFormact_Upgrade.CommandButton1.Enabled = False
Else
End If
End Sub
Your code should be working, as you're on the right path.
To test it, simply create a new form and add this code, you'll see it should work. Maybe you're having problems within the IF clause?
Besides, you don't need to select the shape prior to disabling it; just disable it right away.
Private Sub UserForm_Initialize()
CommandButton1.Enabled = False
End Sub
I know this is old, but got to this thread trying to solve my problem, and found a solution that wasn't mentioned here. So in case someone gets here like I did, and this didn't quite get them where they needed to go, I thought this might help.
I had a userform with a drop down box called cmdADAMFields, and I didn't want my submit button called FieldsSubmitButton to be enabled until I selected something from the dropdown box.
I had to break up my argument into two different private subs vs one larger If-Then-Else statement.
First, I put:
Private Sub UserForm_Activate()
If cmbADAMFields.ListIndex = -1 Then FieldsSubmitButton.Enabled = False
End Sub
Then when for my pulldown's private sub when it's value changed I wrote:
Private Sub cmbADAMFields_Change()
FieldsSubmitButton.Enabled = True
End Sub
The proper place for setting Enabled property is in Activate event (associated with Show method) and not Initialize event (associated with Load instruction).
The below code disable the button CommandButton1 when AL10 cell >= 10.
Private Sub UserForm_Activate()
CommandButton1.Enabled = ( Sheets("DATA").Range("AL10") < 10 )
End Sub
For buttons you can choose between normal buttons (property Enabled=False and property Visible=true), disabled buttons (property Enabled=False and property Visible=true) and invisible buttons (property Enabled=False and property Visible=False), that it is a cleaner interface, in most cases.
Concerning text boxes, besides normal, disabled and invisible status, there is a locked status, that is enabled and visible, but cannot be user edited. (property Locked = True)
A locked control only can be changed by VBA code. For instance, someone can includes date text boxes, that it's filled using a secondary popup date form with Calendar control.

Resources