assign value of a cell when a user is inside of a cell - vba

I tried creating a while loop to make sure it was set in case the user was typing but it seems like the macro magically stops when it tries to assign a value and the user is already typing something.
Basically how do you stop a macro from ending from this unexpected situation and how can I detect if the user is blocking?

from your comments you have a misconception of how VBA works.
first, a macro run from a module is taking control of the workbook, you can't detect user input in a while or for loop like that, what you want is to use an event listener like in this tutorial: a good site for vba basics
basiclly use the "Private Sub Worksheet_Change" option for a sheet/workbook.
also, if you're monitoring just one cell, check out this how to use worksheet change

Function IsEditing() As Boolean
If Application.Interactive = False Then
IsEditing = False
Exit Function
End If
On Error GoTo err:
Application.Interactive = False
Application.Interactive = True
IsEditing = False
Exit Function
err:
IsEditing = True
Exit Function
End Function

Related

Where name created with "Application.Names.Add" go?

For learning and work purposes I needed to create mesage box that pop-ups first only first time opening workbook. For that task I found elegant solution in one of the old treads (old thread).
Working principle of sugested code is more or less clear to me exept part where named created with Application.Names.Add go? I checked under Forlulas -> name manager and did not find created name, I gues it went somwhere else?
Used Code:
Sub RunOnce()
Dim Flag As Boolean: Flag = False
For Each Item In Application.Names
If Item.Name = "FunctionHasRun" Then Flag = True
Next
If Flag = False Then
Call Application.Names.Add("FunctionHasRun", True, False)
Application.DisplayAlerts = False
ActiveWorkbook.Save
Application.DisplayAlerts = True
Call RunOnceFunction
End If
End Sub
Formulas -> Names only shows you the ThisWorkbook.Names and Worksheet.Names (depending on the scope of your Name).
If you insert a breakpoint and look at View > Locals Window you can find the Application.Names in Me > Application > Names.
My preferred method of adding an "opened" flag is to add to the CustomDocumentProperties. I've never seen anyone use Application.Names for this.
If what you want is for a messagebox to pop up when the workbook is opened then the following code will do that for you, simply place it under ThisWorkbook.
Also I'm pretty sure the syntax in your code is incorrect, if you wanted to add a Named Range you would do it as follows: Sheet1.Range("A1:Z10").Name = "MyNamedRange" there would be no need for the Call before it, as it isn't calling another subroutine/function:
Private Sub Workbook_Open()
MsgBox "This will pop up when the Workbook is opened!", vbInformation, "My MessageBox Title"
End Sub
UPDATE:
If you only want it to run the very first time the workbook is opened and then never again, the following will achieve that for you:
Sub RunOnce()
Dim Flag As Boolean: Flag = False
For Each Item In Application.Names
If Item.Name = "FunctionHasRun" Then Flag = True
Next
If Flag = False Then
Sheet1.Range("$A$1:$A$1").Name = "FunctionHasRun"
Application.DisplayAlerts = False
ActiveWorkbook.Save
Application.DisplayAlerts = True
Call RunOnceFunction
End If
End Sub
You cannot see it in the Name Manager because your third argument, Visible:=False, is set False.
Visible - True specifies that the name is defined as visible. False specifies that the name is defined as hidden. A hidden name does not appear in the Define Name, Paste Name, or Goto dialog box. The default value is True .
See Names.Add Method for more information.

Prevent VBA code from other workbook working with objects

I need to disable a control (list box in this case), so user cannot get data from other departments. I do have a macro that can enable/disable it, and ask for password.
What I need is to prevent semidecent user to write a macro for enabling this list box in another workbook and unlocking it this way. It is possible in some way prevent VBA code from other modules to work with this control? So only code written in that one sheet can enable or disable it.
Thank you
What you can do is lock your VBA project and change your "subs" into "function", this way, a more advanced user have no access to the macro you've written without unlocking the project.
(Functions are not visible from the "assign a macro" button in the developer ribbon where Subs are)
I tried UserInterFaceOnly - and I'm not sure at this moment what it is supposed to do but it is not doing what I wanted
To be more clear, I have 2 list boxes and several comboboxes on Sheet1. When I'm sending file to users, I want to lock ListBoxes, or rather .enabled = False
What I'm trying to do is to prevent other user to write in some other workbook
Workbooks("opexRequest").Sheets("Report").ListBoxes("lstDepartment").Enabled = True
this line woudl enable the list box, and user can change values, and macro will genarate new SQL string for Departments user is not supposed to have access to. I need to have comboboxes enabled, so user can view different dates, and versions of plan for his department.
By accident I found a way I guess.
Within Sheet1 I have this code
Sub lstDepartment_change()
'checks first if lock checkbox is true/false, clears selection and disables listbox, _
if checkbox is locked and yet listbox is enabled and allows change
If Me.chkLock = True Then
Me.ListBoxes("lstDepartment").Enabled = False
Me.ListBoxes("lstdirector").Enabled = False
Call clearlistbox
End
ElseIf Me.chkLock = False Then
Call ControlsM.directorpopulation
End If
End Sub
Sub lstDirector_change()
'checks first if lock checkbox is true/false, clears selection and disables listbox, _
if checkbox is locked and yet listbox is enabled and allows change
If Me.chkLock = True Then
Me.ListBoxes("lstDepartment").Enabled = False
Me.ListBoxes("lstdirector").Enabled = False
Call clearlistbox
End
ElseIf Me.chkLock = False Then
End If
End Sub
Private Sub chkLock_Click()
'locking/unlocking with password inbox call
If Me.chkLock.Value = True Then
Me.ListBoxes("lstDepartment").Enabled = False
Me.ListBoxes("lstDirector").Enabled = False
ElseIf Me.chkLock.Value = False Then
If PasswordInputBoxM.InputBoxPassword("Enter password", "Password Required") = "****" Then
Me.ListBoxes("lstDepartment").Enabled = True
Me.ListBoxes("lstDirector").Enabled = True
Else
Me.chkLock.Value = True
MsgBox "Wrong Password"
Exit Sub
End If
End If
End Sub
If someone tries to unlock just listbox, and tries to change value, it will check the checkbox value, and clears the selected list and locks the listbox
If someone tries to unlock listbox and uncheck lock checkbox, it will automatically ask for password
Problem is, that someone can change selected value from other workbook. I do not know how to handle that
Workbooks("opexRequest").Sheets("Report").ListBoxes("lstDepartment").Enabled = True 'handled
Workbooks("opexRequest").Sheets("Report").chkLock.value = False 'handled
Workbooks("opexRequest").Sheets("Report").ListBoxes("lstDepartment").Selected(1) = True 'do not know how to prevent this

Run VBA function with a button

I defined a VBA function that returns a filesize. Now I want to invoke it with a button that's calling a different macro. My expectation is that after running the macro it'll invoke my function at the very end. My problem is that when I put a formula into a cell it will return a current filesize only the moment I enter the formula. When I edit the file, save it and reopen, the =wbksize() will still display the filesize from before my edits.
So the purpose of this macro run by a button is to refresh the filesize value. Here's my attempt to do it.
function:
Function wbksize()
myWbk = Application.ThisWorkbook.FullName
wbksize = FileLen(myWbk)
End Function
refresh:
Worksheets("Sheet2").Range("K1").Calculate
The above doesn't seem to work :/
Function works fine, but refreshing should call function.
Function wbksize() As String
myWbk = Application.ThisWorkbook.FullName
wbksize = Str(FileLen(myWbk))
End Function
Sub Refresh()
Worksheets("Sheet2").Range("K1") = wbksize
End Sub
This may or may not help you in your situation....LINK
I have never needed to use this on excel but it maybe what your looking for, you can set custom functions as 'VOLATILE' which forces excel to run them whenever ANYTHING get calculated, again i have never needed to use this so i cannot comment on any drawbacks or anything but it looks like it may work in your case.
I've tested these, and they both work fine. It depends on what you want your trigger to be: Changing the worksheet, or performing a Calculate on the worksheet.
Put either of these in your Worksheet. The first will trigger on Calculate, the second on Change.
Private Sub Worksheet_Calculate()
Dim lFileLength As Long
Application.EnableEvents = False 'to prevent endless loop
lFileLength = FileLen("\\MyFile\Path\AndName.XLS.XLS")
ThisWorkbook.Sheets("Sheet1").Range("A1").Value = CStr(lFileLength)
MsgBox "You changed THE CELL!"
Application.EnableEvents = True
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
Dim lFileLength As Long
Application.EnableEvents = False 'to prevent endless loop
lFileLength = FileLen("\\MyFile\Path\AndName.XLS")
ThisWorkbook.Sheets("Sheet1").Range("B1").Value = CStr(lFileLength)
MsgBox "You changed THE CELL!"
Application.EnableEvents = True
End Sub

VBA Macro to skip a macro that has already run

I have a text form field in a protected word 2011 for mac document, that triggers a macro to run upon exit. The macro populates the form with various information but is not dependent on the test that is entered into this particular form field. I have two ideas to solve this problem. The first option would be best if it can be done, otherwise the second option would be a reasonable work-around. Can someone please help find a macro that does one of the following?
a. Will prevent the macro from running a second time if the form field is entered into again and edited?
b. Checks to see upon entry of the form field, if there is text in the field already, and if there is, prevents editing and moves to the next form field without running the upon exit macro again?
I am very new to VBA, but I think I have a handle on the basics. Here is what i have come up with for the b. solution but it does not work.
A macro that checks to see if there is text in form field names "text9", if there is, then unprotect from, go to bookmark "text10" and protect form. else, allow the user to fill in the form field and run the macro upon exit.
Sub TestSkipField()
'
' TestSkipField Macro
'
'
With GetCurrentFF
If Len(.Result) = Null Then
End If
Else
If ActiveDocument.ProtectionType <> wdNoProtection Then
ActiveDocument.UnProtect
End If
Selection.GoTo What:=wdGoToBookmark, Name:="Text10"
With ActiveDocument.Bookmarks
.DefaultSorting = wdSortByName
.ShowHidden = False
End With
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True
End If
End With
End Sub
I think the easiest approach to your problem is to use a global variable that answers the question "has the macro already run?".
Let's say that you have a macro like follows:
Sub myMacro()
'your stuff here
End Sub
You don't want this macro to run twice. Then, you can define a global variable on the top of your module:
Dim hasRun As Boolean 'this will be False by default
Sub myMacro()
'your stuff here
End Sub
Sub myOtherMacro()
'some other stuff here
End Sub
'etc.
And then embed a check into your macro:
Sub myMacro()
If hasRun = False Then 'if hasRun is still False, it means we never ran this code yet
'do your stuff here
hasRun = True 'set hasRun = True, so from now we know that this code has already been executed once
End If
End Sub
This should allow you not to change the structure of your code or executing check on the data, but at the same time executing the macro only once. You can follow this direction to elaborate more the execution conditions: execute it only twice, only if something happened etc.
PLEASE NOTE: you better set hasRun = True at the very end of your macro's code, in order to make sure that the value hasRun will not be True if the execution didn't arrive until the end of your desired code.

Application.Interactive = True fails

In the worksheet SelectionChange event I set :
Application.Interactive = False
.... do work
Application.Interactive = True
For some reason Interactive is not getting set back to true, even though I know the code is being executed.
Excel 2013
Any ideas?
Sub Main()
Debug.Print Application.Interactive
Application.Interactive = False
Debug.Print Application.Interactive
Application.Interactive = True
Debug.Print Application.Interactive
End Sub
Doesn't fail for me... Try that and see more here
Since you've discovered the reason for failing the conclusion could be that the Application.Volatile needs to be set to false on a function that uses it because the Interactive mode blocks the user interface and the Volatile calculates things based on user input. While the user input is blocked you can't expect a function that evaluates the user input to work. One excludes the other - hope that makes sense.
Additionally, check your on error resume next or on error goto <label> statements as the would cause skipping some of the code therefore the Application.Interactive = True would have never get executed.