Trapping Userform Events
Jan 9, 2009I'm trying to trap the events of a user form in a class of mine. At the top of my class I have the standard:
View 2 RepliesI'm trying to trap the events of a user form in a class of mine. At the top of my class I have the standard:
View 2 RepliesI have a UserForm with a Text Box, I populate that Text Box with a number (say 5) and then the following code runs:
Code:
Private Sub tbOverrideMokWh_Change()
Application.EnableEvents = False: Application.ScreenUpdating = False
With tbOverrideMokWh
[Code]....
After the Sub is run 1 time, it runs again. Why? I've disabled Events?
I have several similar statements in different events in my UserForm code. Is it possible to separate these statements in a procedure and call it each it when I need it? The similar statements are:
With Sheets("Knowledge")
For Each rw In .Range("A2:A" & .Range("A" & Rows.Count).End(xlUp).Row)
If ListBox5.List(, 4) = Trim(rw.Value) Then
Label1.Caption = rw.Offset(, 1).Value
Label2.Caption = rw.Offset(, 4).Value
Label3.Caption = rw.Offset(, 5).Value
Exit For
End If
Next rw
End With
If it's not possible to make my code shorter with a procedure, can I optimize somehow this repeated code?
I am using table driven forms controls on userforms. E.g.:
Excel 2010ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAG1UI NameMultiPage ParentPage
ParentControlNameTopLeftHeightWidthCaptionTagControlTipTextSpecialEffectWordWrap
MultiLineBorderStyleBorderColorBackColorBackStyleForeColorColumnCountColumnWidthsListStyle
[Code] .....
I use the following (work in progress) function to add the controls to the userform (usually added to page or frame).
Code:
Public Function AddControls(ByVal objTarget As Object, ByVal strUiName As String)
Dim rngControls As Excel.Range, rngProperties As Excel.Range
Dim rngControl As Excel.Range, rngProperty As Excel.Range
Dim objControl As Object
With shtFormUI
Set rngControls = .Range("D2:D" & .Range("D" & .Rows.Count).End(xlUp).Row)
[Code] .....
Now I need a means of trapping the controls events. I thought I could use a class, e.g.:
cFormEvents
Code:
Option Explicit
Public WithEvents lblLabel As MSForms.Label
Public WithEvents tbxTextBox As MSForms.TextBox
Public WithEvents cbxComboBox As MSForms.ComboBox
Public WithEvents lbxListBox As MSForms.ListBox
Public WithEvents cbtCommandButton As MSForms.CommandButton
Private Sub lblLabel_Click()
[Code] .....
It seems I cannot reference the controls because I they are added at runtime. For the given example, I want to run whatever procedure name appears for lblPrimaryContact in column AC (click event). So in my userform module I instantiate the class, but I get an error when I try and reference the control:
Code:
Set m_clsFormEvents.lblLabel = Me.lblPrimaryContact
Error is "method or data member not found".
Any alternative method to grab the click event for the control added at runtime?
Since I practically never deliberately hit Control-2 through Control-5 -- and do hit them "more than never," I thought I'd grab them with the KeyPress event (or KeyDown?). Then it hit me that I'm not sure where it would go.
This is for every workbook so this would generically reside in personal.xls. ThisWorkbook doesn't seem like the right place; I want its scope to be ActiveWorkbook. Would it require a class module (actually, I already looked, and don't see any keyboard events under "App")?
I only want to do a "macro" in VBA that must do two things
record the keys pressed (during only 5 minutes)
and save the results in a text file
It is the only code in VBA I try to write
I found public documentation (legal stuff I mean)
about GetKeyState
I have snippets of code for save data as txt file
But I don't succeed in finishing the all code.
My purpose is to show to my daughter in law that if she doesn't respect rules
on the net, I'm able (but I don't want to really do it, it's just a threat) to know what she's doing.
If I want a real keylooger, I've downloaded one on the net.
I haven't done that and I don't want to.
I don't think a vba code with
the function getkeystate and Open file (an only that)
is a danger.
The below code validates values in a combined range. Values can be a 0 or a 3-digit combo of values 0 through 4. The problem is that whenever the Delete key is pressed to delete an acceptable value, the msgbox appears. How can the msgbox appear ONLY if a user tries to enter a wrong value--the cells can be, and are, left null regularly, so the msgbox is annoying.
View 3 Replies View Relatedhow I could amend the following code so that my target worksheet includes file versions? The worksheet range "Folder" contains a string specifying the target folder and the worksheet range "IncludeSubFolders" is either TRUE or FALSE. I can trap FilePath, FileName, FileSize and DateModified fine but not File Version (I'm working with DLL libraries).
Code:
Dim iRow As Long
Sub ListFiles()
iRow = Range("DatabaseStart").Offset(1, 0).Row
Call ListMyFiles(Range("Folder"), Range("IncludeSubFolders"))
End Sub
[Code]......
IS there any code where I can use to error trap worksheet so that if any error occurs like runtime error 9 then it can be traped.
View 2 Replies View RelatedI'm using VBA to open several files and pull information from them into a workbook with the hopes of eventually outputting the information to a formatted text file that can be uploaded to a data transfer system that will fill out a form for me. The problem I am finding is that the workbook that I need to access has faulty VBA code that executes everytime the workbook is opened. I realize the easiest solution would be to fix the code that is faulty however the workbook that I am accessing is automatically generated for each product with the faulty code embedded requiring me to handle the error on the back end. Attached is my code and I attempted using error trapping to handle this issue but I've concluded that I either lack understanding of error trapping or the fact that the error is in a cascaded workbook complicates the error trapping code.
View 5 Replies View RelatedI suspect I need a class module for this...
How do I trap the event when a user chooses to expand / collapse outlines (columns in this instance) on a worksheet?
Basicallly, when attempting to expand I want to prompt the user to enter a password and then unprotect the sheet. And if collapsing to prompt again and then protect the sheet. I have this bit covered, just not sure how to trap the event.
How can we "read" information from the VBE Error Trapping options (Break on All Errors/ Break in Class Module/ Break in Unhandled Errors) programmatically?
Background: I am developping a spreadsheet that needs to be sent around to distant users. Some of them might not have the default "Break on Unhandled Errors" setting), which would just make my programme fail or risk to fail...
This issue has been covered in the thread below, but the proposed solution doesn't work on my side (always returning the same output!): [URL] .....
Hint: it has been suggested to look into the registry, which the linked post does.
Ideally, I would love to be able to change the setting programatically, but that seem near to impossible in excel 2007 VBA (unless calling an external programme that closes and restarts excel after having changed the option?...)
I am aware that I can use single changing events in worksheet change events. For instance, if column 1, or A is changed, do something. This is only a single If statement, i.e. either the condition is true, or not. What I am not sure is if I can use two changing events, i.e. two conditions. For e.g. I would like if Column A value is X and Column B is "Active", action it, but only if two conditions are true.
For.e,g. The below syntax does not work. If it is only column A, it does work, but I want both A and B to be true, then copy and paste the target does not anything.
VB:
If Target.Column = 1 Then
If Target.Column = 2 Then
If Not Intersect(Target, Range("A2:A" & Rows.Count)) Is Nothing Then
If Not Intersect(Target, Range("B2:B" & Rows.Count)) Is Nothing Then
If Target.Value = "X" And Target.Value = "Active" Then
some times i choice (Initialize , active, open, click
Workbook_BeforeSave)
and so on
tell me as the differences and usage of these strings in order to improve our(my) programming skills.
I am trying to write a code where user has to respond to input box option depending on yes or no selection. There are two types of responses to different questions. One needs to respond between 1 and 100 (if yes) and for some other questions needs between 1 and 4 (if yes). A "no" will enter zeo value. But it is not working. I am using two named ranges "VALIDCELLS" (for 1 to 100) and "FREQVALIDCELLSS" (for 1 to 4). Here is the code;
View 6 Replies View RelatedI have early made a macro that will change some of the cells before printing and then changing back again to initional state after the print out.
http://www.excelforum.com/excel-misc...-printing.html
But now I have to do let people also print a copy a regular way, without the macro I made before. But if they go through the regular way iof printing I would like to insert at header in red color, to notify them that this is just a preview and not the way to print. And here we come to the problem ....
why am I unable to create events on my worksheets? They were usually available to me when I selected "Worksheet" on the dropdown in vba.
When I open Excel, only "General" comes up in the drop down menu. What am I missing?
I've created an addin to reformat spreadsheets that I receive in a particular format. What I would like to do is enable events so that whenever a spreadsheet is opened the reformatting procedure is run (this also validates whether the spreadsheet is of the correct format).
I have created a class module with the following code (exactly as the Excel help):
Public WithEvents App As Application
Dim X As New EventClassModule
Sub InitializeApp()
Set X.App = Application
End Sub
Private Sub App_WorkbookOpen(ByVal Wb As Workbook)
SortE1Output 'This is my procedure that determines whether the
'spreadsheet is of the correct format and then reformats it
End Sub
"After you run the InitializeApp procedure, the App object in the class module points to the Microsoft Excel Application object, and the event procedures in the class module will run when the events occur."
When I try to make a call to the InitializeApp procedure in the auto_open procedure (in a different non-class module) I get a "compile error: sub or function not defined".
I have a spreadsheet,3 columns are shown in the attached.The first column gives the date of the event,the second column the time and the third a rating.I want to put the average rating for each event in each cell in column 4.
View 9 Replies View RelatedIn both instances I want to put a date stamp of the column immediately after the defined columns below.
The first issue is that when it runs the 1st change event, it only puts the date stamp in the top row of the column. The change in the "Inportedshapeall" column would almost always be brought about from range being copied from another column, however, I would still like a date stamp in every cell of the column.
The second issue comes about when I try to run the second change event. It simply seems to get stuck. I have tried Else, Else if, removing End ifs. I am not sure how best to define the events in order to ensure it loops through both.
VB:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("IMPORTEDSHAPEALL")) Is Nothing Then
With Target(1, 2)
[Code] ......
I am trying to add the total time of 643 events. I have the start time subtracted from the end time to give me each individual events total time, at the bottom I am trying to put a total time for all events but it keeps displaying 14 hours and 13 minutes even though it should be somewhere around 500 hours.
View 4 Replies View RelatedI have a worksheet that I use on a track and field day to enter times and distances and then can allot points based on performance. That is not the problem.
My question is: Is there a function or formula to then sort through the data and rank the participants in order of their finish in a different worksheet? I imagine I would somehow need to link the data to each name. I have attached a copy of the actual worksheet.
How can I get EXCEL to display recurring events on one line? In the example below, I want all events of type PLAN to be on the same line.
Phase
Time
Duration
End
[Code].....
I have been working for 8-1/2 hours to get the focus to move to a specified control on my user form if criteria are met in two other controls on the same user form. It's not responding as I expect. I think I know what is happening; I just don't know why.
Situation: I have a user form with the following relevant fields in this tab order - LMonthDay, tbMonthDay, SBMonthDay, LYear, tbYear, SBYear, LTime, and tbTime. I used the designations L, tb, and SB in my control names to represent label, text box, and spin button respectively.
Problem: Once the SBMonthDay control has the focus, if I press TAB without changing the control's value, it does not execute the Exit event statement the way I anticipated. I want the focus to move to the tbTime control when the tbMonthDay & tbYear controls are already properly populated. (They will already be populated for 99% of the entries in this case.)
Private Sub SBMonthDay_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Debug.Print "SBMonthDay_Exit Begin"
If ISLIKE(tbMonthDay.text, "####") _
And ISLIKE(tbYear.text, "####") Then
tbTime.SetFocus
Else: tbYear.SetFocus
End If
Debug.Print "SBMonthDay_Exit End"
End Sub ...........................................
2 Woksheet Change Events? I have a question similar to one that was posted about 2 worksheet change events which I found out is not possible and so I have to merge the codes together and I don't know how. I'm facing a problem in the advanced filter when I remove the last item to be filtered and takes forever to calculate.
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Count > 1 Then Exit Sub
If Target.Address = "$F$2" Or _
Target.Address = "$F$3" Or _
Target.Address = "$F$4" Or _
Target.Address = "$F$5" Or _
Target.Address = "$F$6" Or _
Target.Address = "$F$7" Or _
Target.Address = "$F$8" Or _
Target.Address = "$F$9" Then
Range("Database").AdvancedFilter _
Action:=xlFilterInPlace, _
CriteriaRange:=Range("Criteria"), Unique:=False
Exit Sub
End If
If Target.Column = 3 Then
If Target.Value = "YES" Then........................
I have a "Private Sub Worksheet_SelectionChange(ByVal Target As Range)" in a worksheetsheet code.
I would like to turn this procedure off while I run another routine and then turn it back on after the routine has run.
Can you create user-defined events in excel? I ask because the worksheet_change event does not capture pastes or undo/redo.
View 4 Replies View RelatedIs it possible, that while running code that the code can say initiate the click event on a command button on another sheet.
Say that I have a button on Sheet1 called "wkscmd_DisplayDEI"
Behind that button is obviously some code. I want to know is it possible that while some code is running ( code does not reside on the module page for Sheet1 that it can send a pseudo click to the button?
The boolean Cancel suddenly stopped working for me in all workbook and worksheet events. Consider a simple example:
Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)
Cancel = True
End Sub
does not cancel right click, like it has before. Same with BeforeDoubleClick. I can only assume it is some kind of a setting or similar, as there is absolutely no change in the code.
Is there a way to set up a cell selection event trigger through an addin when it is installed so that I can respond to selections made on user's sheets?
View 3 Replies View Related