Excel Userform animated dots on loading - excel

There are a lot of tutorials in the Internet. However I was not able to find anything suitable. Is there any way to make animated dots on loading?
The idea is to make a loop of animated dots ..... on userform so they would appear one after another and then would start over after some amount of dots.
So I input a dot to Label1 and move it to left after certain time criteria?
My current code for UserForm:
Private Sub UserForm_Initialize()
HideTitleBar.HideTitleBar Me
Call loadingdots
End Sub
Code for Private Sub Workbook_Open():
Loading.Show (vbModeless)
Dim RngCom As Range
Dim RngTurb As Range
Dim RngGen As Range
Application.Wait (Now + TimeValue("00:00:06"))
ThisWorkbook.Worksheets("MAIN").ScrollArea = "$A$1:$BL$45"
Application.DisplayFormulaBar = False
ActiveWindow.DisplayHeadings = False
ActiveWindow.DisplayGridlines = False
etc...
Unload Loading
'Application.ScreenUpdating = True
End Sub

The most elegant solution would likely to be the OnTime method.
Place a label inside your UF and remove the caption. Next, in a regular module (so not that of the UF), place this subroutine:
'this function ensures the self-activating sub will stop if the UF has been closed
Public Function IsLoaded(form As String) As Boolean
Dim frm As Object
For Each frm In VBA.UserForms
If frm.Name = form Then
IsLoaded = True
Exit Function
End If
Next frm
IsLoaded = False
End Function
Public Sub loadingdots()
If IsLoaded("UserForm1") = True Then
If Len(UserForm1.Label1.Caption = 4) Then
UserForm1.Label1.Caption = "."
Else
UserForm1.Label1.Caption = UserForm1.Label1.Caption & "."
End If
Application.OnTime Now + TimeValue("00:00:01"), "loadingdots"
End If
End Sub
Next, call the self-activating sub when the UF gets initialised
Private Sub UserForm_Initialize()
Call loadingdots
End Sub
Do not forget to change the references to the UF to the right name.

Related

Controlling dynamically created controls on a userform in VBA excel

I have created a multipage user form which dynamically populates with a set of identical frames and each of them has 2 option buttons based on previous user selections. I am trying to check if at least one of the option buttons is selected within each frame but don't seem to access the option buttons in code even through I know what their names will be. I will then be transferring the selection to a worksheet so need to be able to see what they have selected. Any help would be appreciated, I use VBA for excel infrequently so its always a struggle to be honest.
I'm getting closer, I've used this code of another post and changed it slightly while I trial what I am doing. Getting there slowly. :)
I'm not sure what some of the Class modules part is doing but its working.
Forms: Userform1
Option Explicit
Friend Sub OptionButtonSngClick(o As MSForms.OptionButton)
Dim cControlCheck As MSForms.Control
Dim cControlCheck1 As MSForms.Control
Dim cControlFrame As MSForms.Control
Dim strName As String
If Left(o.Name, 2) = "qN" Then
o.BackColor = RGB(256, 0, 0)
ElseIf Left(o.Name, 2) = "qY" Then
o.BackColor = RGB(0, 256, 0)
End If
For Each cControlCheck In UserForm1.Controls
If TypeName(cControlCheck) = "Frame" Then
For Each cControlCheck1 In Me.Controls(cControlCheck.Name).Controls
If TypeName(cControlCheck1) = "OptionButton" Then
If cControlCheck1 = False Then
cControlCheck1.BackColor = RGB(240, 240, 240)
End If
End If
Next
End If
Next
End Sub
Friend Sub cmdCheck_Click()
Dim cControlCheck2 As MSForms.Control
Dim cControlCheck3 As MSForms.Control
Dim cCollection As Collection
Set cCollection = New Collection
For Each cControlCheck2 In UserForm1.Controls
If TypeName(cControlCheck2) = "Frame" Then
For Each cControlCheck3 In Me.Controls(cControlCheck2.Name).Controls
If TypeName(cControlCheck3) = "OptionButton" Then
cCollection.Add cControlCheck3
End If
Next
End If
Next
If cCollection(1).Value = False And cCollection(2).Value = False Then
MsgBox ("Make a selection")
End If
End Sub
Class Module: OPtionButtonEvents
Option Explicit
Private WithEvents ob As MSForms.OptionButton
Private CallBackParent As UserForm1
Private CallBackParent1 As UserForm1
Private Sub ob_Change()
End Sub
Private Sub ob_Click()
Call CallBackParent.OptionButtonSngClick(ob)
End Sub
Private Sub ob_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Call CallBackParent.OptionButtonDblClick(ob)
End Sub
Friend Sub WatchControl(oControl As MSForms.OptionButton, oParent As UserForm1)
Set ob = oControl
Set CallBackParent = oParent
End Sub

Performance changing OptionButton OleObject backgroud color on click VBA

I have a Sub to change the background color of OptionButton in a group when clicked but i can't get this work fluently without any delay when switch the colorm too slow. Is there better aproach?
There are 50 OptionButtons with a unique caption each
class module
Public WithEvents Opt As MSForms.OptionButton
Private Sub Opt_Click()
Application.ScreenUpdating = False
Call toggleColor(Opt)
Application.ScreenUpdating = True
End Sub
Sub toggleColor(opt As Object)
For Each ctl In Worksheets("controls").OLEObjects
If TypeName(ctl.Object) = "OptionButton" Then
If ctl.Object.Caption <> optCaption.Caption Then
ctl.Object.BackColor = &H80000011
Else
opt.BackColor = &H80000016
End If
End If
Next ctl
End Sub
Seems like you only need to set the color for 2 options, not 50 - you just need to remember between calls which one is the odd one out, reset that one, then set the color for opt.
ToggleColor insn't instance-specific (since you're passing in opt), so that could be in a regular module and re-worked to something like:
Sub toggleColor(opt As Object)
Static optPrev As Object 'previous selection (if any)
If Not optPrev Is Nothing Then
optPrev.BackColor = &H80000011 'un-flag previous
End If
opt.BackColor = &H80000016 'flag the clicked one
Set optPrev = opt 'remember it for next time
End Sub

My Userform keep shrinking after every use

Almost every time I use my user form, it shrinks, and after a few times it gets too small to see and I have to go back into the forms on my project and drag it until the size is large again. Is this a result of my code, or is there something that I can do to fix this?
Option Explicit
' called on click of "OK" button
Private Sub CommandButton1_Click()
MyFile = Me.ComboBox1.Value
Unload Me
End Sub
' called on click of "Cancel" button
Private Sub CommandButton2_Click()
Stopped = True
Unload Me
End Sub
' loads the combo box with the names of all available workbooks
Private Sub UserForm_Initialize()
Dim wkb As Workbook
With Me.ComboBox1
For Each wkb In Application.Workbooks
If wkb.Name <> ActiveWorkbook.Name Then
.AddItem wkb.Name
End If
Next wkb
End With
End Sub
I can't think of a reason for this to happen, especially without any code anywhere tweaking the Height and Width of the form, and with the form Unload-ing itself every time it's shown, even if you're Show-ing the form's default instance it should still be initialized with the design-time values.
You could try forcing a size explicitly in that initialize handler:
Private Sub UserForm_Initialize()
Me.Height = 480
Me.Width = 600
InitializeComponents
End Sub
Private Sub InitializeComponents()
PopulateAvailableInactiveWorkbooks
'...
End Sub
Private Sub PopulateAvailableInactiveWorkbooks()
Dim wkb As Workbook
With Me.ComboBox1
For Each wkb In Application.Workbooks
If wkb.Name <> ActiveWorkbook.Name Then
.AddItem wkb.Name
End If
Next wkb
End With
End Sub
If you have this issue, you may like to check;
https://www.mrexcel.com/board/threads/userforms-shrink-on-successive-openings-of-an-excel-file.1078705/
For me, adding the following to the userform code at least kept the display right.
Private Sub UserForm_Initialize()
With frm40_Overview1
Height = 600
Width = 800
End With
Application.ScreenUpdating = True
End Sub

Excel VBA Userform.Name as Variable to next Modul in case of Button Click

i really need some help, im new to VBA programming and just learn all by myself.
Thx for all Help.
What is my problem ?
I have more than 1 Userforms in my Tool and every Userform contains alot of Buttons some Buttons the same on other Userforms and some different.
if i click a button in a Userform, the class cant give my userform.name as a variable to the next module.
in the Code "Class" sUserform is alltime "nothing"
My Code
Userform
Option Explicit
Private myBtn As clsCMD
Private Sub UserForm_Activate()
modUI.ufGETICON Me
End Sub
Private Sub UserForm_Initialize()
modMSG.ufINFO
modAUTOOPEN.Workbook_Open Me
Dim ctrl As Control
For Each ctrl In frmMain.Controls
If TypeOf ctrl Is MSForms.CommandButton Then
Set myBtn = New clsCMD
Set myBtn.Button = ctrl
End If
Next
End Sub
Class
Option Explicit
Public WithEvents Button As MSForms.CommandButton
Private Sub Class_Initialize()
Static collButton As New Collection
collButton.Add Me
End Sub
Private Sub Button_Click()
Dim sButton As String
sButton = Button.Name
CMDTEST *sUserform*, Button
End Sub
Private Sub CMDTEST(*sUserform As Object*, sButton As Object)
If sButton.Name = "cmd_Admin" Then
modCMD.cmd_Admin
End If
If sButton.Name = "cmd_OItem" Then
modCMD.cmd_OItem sUserform
End If
End Sub
Modul
Option Explicit
Public sUserform As Object
Public Sub cmd_Admin()
Dim sAnswer As String
sAnswer = InputBox("Passwort f?r den Zugang zum Adminbereich eingeben:", "Zugang Adminbereich")
If sAnswer = sPASS Then
Unload frmMain
frmMain.Hide
ElseIf sAnswer = "" Then
Exit Sub
Else
MsgBox "Inkorrektes Passwort eingegeben", vbInformation + vbOKOnly, "Passwortabfrage negativ"
End If
Application.Visible = True
End Sub
Public Sub cmd_OItem(sUseform As Object)
sUserform.Hide
frmOnIt.Show
End Sub
It's difficult to give you a precise answer without seeing how you want to apply this code across your Userforms.
If, for example, you're running the loops on each Userform, then couldn't you just add a Userform property to your class and pass in the appropriate Userform?
For Each ctrl In frmMain.Controls
If TypeOf ctrl Is MSForms.CommandButton Then
Set myBtn = New clsCMD
With myBtn
Set .Owner = frmMain
Set .Button = ctrl
End With
End If
Next
Otherwise, I guess you'd just have to run up the control ladder until you find a Userform:
Private Function GetUserFormName(ctrl As Object) As String
Do
Set ctrl = ctrl.Parent
Loop Until TypeOf ctrl.Parent Is MSForms.UserForm And Not TypeOf ctrl.Parent Is MSForms.Frame
GetUserFormName = ctrl.Parent.Name
End Function
So your routine would contain code something like:
Private Sub CMDTEST(sButton As Object)
Dim n As String
n = GetUserFormName(sButton)
End Sub
I solved the problem now and I'd like to hear your feedback if it's a good way or not. Maybe there are some improvements to do?
You have to know this project is huge and i have more than 20 Userforms and over all Userforms more than 300 buttons.
Most of these buttons are like a Menu (laststep, nextstep, finish, cancel, OItem, Test, and some more). These are all doing the same every time,
and then i have some special buttons they are only 1-5 times on all userforms but these buttons should also doing the same on their Userforms (like refresh, change picture from Item, change something else etc.)
I tested this code on a VBA Project on 3 Userforms
Code starting everytime from modAUTOOPEN, Auto_Open
Userforms are:
ALL Userforms have the same code at this time for this test.
frmMain (starting site)
frmOnIt
frmTeM
.
Option Explicit
Private myBtn As clsCMD
Private myBtnColl As Collection
Private Sub UserForm_Activate()
modUI.ufGETICON Me
End Sub
Private Sub UserForm_Initialize()
modMSG.ufINFO
modAUTOOPEN.Workbook_Open Me
Dim ctrl As Control
Set myBtnColl = New Collection
For Each ctrl In Me.Controls
If TypeOf ctrl Is MSForms.CommandButton Then
Set myBtn = New clsCMD
Set myBtn.UserForm = Me
Set myBtn.button = ctrl
myBtnColl.Add myBtn
End If
Next
End Sub
Modules are:
modARCHIV
(not relevant, just old material - maybe its useful in the future)
modAUTOOPEN
(standard stuff)
.
Option Explicit
Public sUSER As String
Public Const sPASS As String = "12345"
Public Sub Workbook_Open(sUSerform As Object)
modUI.ufPOSITION1 sUSerform
End Sub
Public Sub Auto_Open()
' User basierend auf Exceluser ausw?hlen
sUSER = Application.Username
modData.defDEFAULTS
modData.defWORKSHEETS
Load frmMain
frmMain.Show
End Sub
modCHECKS
(not relevant atm, checking which users are using the tool etc.)
modCMD
(where I program all my CMDs for every UF)
.
Option Explicit
Public Sub cmd_Admin()
Dim sAnswer As String
sAnswer = InputBox("Passwort f?r den Zugang zum Adminbereich eingeben:", "Zugang Adminbereich")
If sAnswer = sPASS Then
Unload frmMain
frmMain.Hide
ElseIf sAnswer = "" Then
Exit Sub
Else
MsgBox "Inkorrektes Passwort eingegeben", vbInformation + vbOKOnly, "Passwortabfrage negativ"
End If
Application.Visible = True
End Sub
Public Sub cmd_OItem(sUfName As Object)
Application.ScreenUpdating = False
sUfName.Hide
frmOnIt.Show
End Sub
Public Sub cmd_Test(sUfName As Object)
Application.ScreenUpdating = False
sUfName.Hide
frmTeM.Show
End Sub
modDATA
(standard stuff)
modFUNC
(not relevant atm, for new functions)
modLOAD
(not relevant atm, load all datas to specific UF)
modMSG
(programming and updating all infoboxes on alle UF)
modSAVE
(not relevant atm, save all data from UF to WS or in new WB)
modUI
(checking how many screens and where to open, adding minimize button and windowstaskbar button, etc.)
ClassModules are:
clsCMD (should the connection betweend buttons on UF and modCMD)
.
Option Explicit
Public WithEvents button As MSForms.CommandButton
Private c_Userform As Object
Public Property Set UserForm(ByVal UfName As Object)
Set c_Userform = UfName
End Property
Private Sub Button_click()
CMDTEST UfName, button
End Sub
Public Sub CMDTEST(UfName As Object, button As Object)
Dim sButton As String
Dim sUfName As String
sButton = button.Name
sUfName = c_Userform.Name
If button.Name = "cmd_Admin" Then
modCMD.cmd_Admin
End If
If button.Name = "cmd_OItem" Then
modCMD.cmd_OItem c_Userform
End If
If button.Name = "cmd_Test" Then
modCMD.cmd_Test c_Userform
End If
'If Button.Name = "cmd_Auftragstool" Then
' modCMD.cmd_Auftragstool c_Userform
'End If
'If Button.Name = "cmd_Beenden" Then
' modCMD.cmd_Beenden c_Userform
'End If
End Sub

How to use VBA to read the values of a checkbox from an Excel userform

I have created a userform that contains two checkboxes. I would like to be able to do different things depending on whether each box is checked or unchecked. However, it seems like no matter what I do, it will always tell me the original value of the checkboxes (false and false). Here is the code attached to clicking CommandButton1:
Private Sub CommandButton1_Click()
ReadData
End Sub
And here ReadData:
Sub ReadData()
Dim myForm As UserForm
Set myForm = UserForms.Add("ComplaintEntryForm")
Debug.Print (myForm!CheckBox1.Name)
Debug.Print (myForm!CheckBox1.Value)
Debug.Print (myForm!CheckBox2.Name)
Debug.Print (myForm!CheckBox2.Value)
End Sub
No matter how the boxes are checked, the immediate window always shows this:
VBA.UserForms.Add("ComplaintEntryForm").Show
CheckBox1
False
CheckBox2
False
I have a screenshot of the whole operation but it won't let me upload it because I'm a new user.
Try this method to load and show the form (this goes in a normal module):
Sub main()
Dim myForm As ComplaintEntryForm
Set myForm = New ComplaintEntryForm
myForm.Show
Set myForm = Nothing
End Sub
In the UserForm's own module, add the following:
Private Sub CheckBox1_Change()
readData
End Sub
Private Sub CheckBox2_Change()
readData
End Sub
Private Sub UserForm_Initialize()
Me.CheckBox1.Value = True
Me.CheckBox2.Value = False
End Sub
Private Sub readData()
Debug.Print Me.CheckBox1.Name
Debug.Print Me.CheckBox1.Value
Debug.Print Me.CheckBox2.Name
Debug.Print Me.CheckBox2.Value
End Sub
I've initialized the two checkboxes to specific values in the Initialize event. This means we are certain about the state the form will start in

Resources