Affecting different Procedures by changing values - excel

I have this master procedure that calls other procedures. But at two places, I need to check that all values are correct. If they are not, I want this master procedure to quit. What I am trying to do is check the values in my sub procedures and if they are not correct, change exitall to true, which would cause the sub procedure to stop. The thing is, I am fairly certain that if I say in my subprocedure to change the value of of exitall to true, it will not affect my master procedure.
My question is, how do I make the exitall to change in my master procedure, if it is changed in my subprocedure?
Thank you.
Sub Allstepstogether()
Dim r As Integer
Dim exitall As Boolean
Dim answer As Variant
Dim hda As Boolean
Dim wdfh As Variant
hda = False
exitall = False
Call Clean
For Each cell In ThisWorkbook.Sheets("Heyaa").Range("C2:C15")
If Weekday(Date) = vbMonday Then
If CDate(cell.Value) = Date - 3
hda = True
wdfh = cell.Offset(0, 1).Value
End If
Else
If CDate(cell.Value) = Date - 1 Then
hda = True
wdfh = cell.Offset(0, 1).Value
End If
End If
Next cell
Call step4
r = Application.WorksheetFunction.CountA(ThisWorkbook.Sheets("BlaCheck").Range("A1:A150"))
If r <> 100 Then
answer = MsgBox("Data not yet uploaded, try again later.", vbOKOnly)
Exit Sub
Else
Call step5
If exitall = True Then
Exit Sub
Else
Call Step7alloptions
Call step8
Call Timetocheck
If exitall = True Then
Exit Sub
Else
Call Step9
Call Step10
Call Step11
End If
End If
End If
end sub
Part of Step5, which should change exitall to true thus stopping the master procedure to be executed if it is incorrect.
sub Step5
dim exitall as boolean
dim lr as integer
'....
'code
'....
lr = Application.WorksheetFunction.CountA(ThisWorkbook.Sheets("BlaCheck").Range("A1:A500"))
If lr > 100 Then
answer = MsgBox("Ups, It did not run correctly, this code execution will be terminated", vbOKOnly)
exitall = True
Exit Sub
End If
end sub

Option 1 is to declare exitall with global scope:
Dim exitall As Boolean '<--Add this at the top of the module.
Sub Allstepstogether()
Dim r As Integer
'Dim exitall As Boolean '<--Remove this from all your Subs.
Better would be to change your Subs into Functions that return a Boolean for success and just test for that. Note that you also don't have to use Else after an If condition that exits - that should cut down your indentation level dramatically:
Converted Sub:
Function Step5() As Boolean
Dim lr As Integer
'....
'code
'....
lr = Application.WorksheetFunction.CountA(ThisWorkbook.Sheets("BlaCheck").Range("A1:A500"))
If lr > 100 Then
answer = MsgBox("Ups, It did not run correctly, this code execution will be terminated", vbOKOnly)
Exit Function
End If
Step5 = True
End Function
Calling code:
Sub Allstepstogether()
'[Snip]
r = Application.WorksheetFunction.CountA(ThisWorkbook.Sheets("BlaCheck").Range("A1:A150"))
If r <> 100 Then
answer = MsgBox("Data not yet uploaded, try again later.", vbOKOnly)
Exit Sub
End If
If Not Step5 Then Exit Sub
Step7alloptions
step8
If Not Timetocheck Then Exit Sub
Step9
Step10
Step11
End Sub

Related

Excel Worksheet_Change not triggering with cell value update

I´ve done some extensive research, but cannot solve my problem. I have an Excel workbook where I constantly pull a value from an OPC server. The value is stored in a set of rows. This works perfectly whenever I introduce the data manually, but when the cell is automatically updated, it does not work.
Private Sub Worksheet_Change(ByVal target As Range)
'MsgBox = Target.Address
If Not Application.Intersect(Range("B1:B2"), Range(target.Address)) Is Nothing Then
Call CopyDataToRecord
End If
End Sub
Sub CopyDataToRecord()
Dim IndexI As Integer
Dim IndexY As Integer
Dim IndexMius As Integer
Dim DufferLength As Integer
DufferLength = 20
IndexY = DufferLength + 5
'Shift Data
For IndexI = i To DufferLength
IndexYMius = IndexY - 1
Cells(IndexY, 1) = Cells(IndexYMius, 1)
Cells(IndexY, 2) = Cells(IndexYMius, 2)
IndexY = IndexYMius
Next IndexI
'Copy The Latest Data
Range("A2:B2").Copy Range("A5:B5")
End Sub
Your code (a bit adapted to declare i and IndexYMius) works well on my Excel sheet.
I would just recommend to prevent recursive Worksheet_Change calls by protecting the changes with Application.EnableEvents:
Private Sub Worksheet_Change(ByVal target As Range)
'MsgBox = Target.Address
If Not Application.Intersect(Range("B1:B2"), Range(target.Address)) Is Nothing Then
Application.EnableEvents = False
Call CopyDataToRecord
Application.EnableEvents = True
End If
End Sub
BTW, if your code is not triggered by the automatic update process, it might be because this process includes somewhere an Application.EnableEvents = False that prevents the Worksheet_Change to be executed.

How do I combine separate macros for the same event in the 'ThisWorkbook' module?

I have a macro in the 'ThisWorkbook' module set to run 'BeforeSave'. I have two other macros that I also need to run 'BeforeSave'. Can I add additional macros to this module?
I have created my macros in the 'standard' module section, and they work with the selection of the 'Run' button. I have attempted to add the 'Macro/Module names' to the bottom of my 'BeforeSave' macro which has done nothing but give me errors.
Option Explicit
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Call HideRows
Call DivAdminApproval
Call ProjNumbrReq
End Sub
Public Sub HideRows()
'When a row begins with X in Travel Expense Codes worksheet, hide the row
Const beginRow As Long = 3
Const endRow As Long = 38
Const chkCol As Long = 14
Dim rowCnt As Long
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Travel Expense Codes")
For rowCnt = endRow To beginRow Step -1
With ws.Cells(rowCnt, chkCol)
.EntireRow.Hidden = (.Value = "X")
End With
Next rowCnt
End Sub
Public Sub ProjNumbrReq()
'Call ProjNumbrReq
With Worksheets("Travel Expense Voucher")
For Each myCell In .Range("U15:U45")
If myCell.Value > 0 And .Cells(myCell.row, "N") = "" Then
MsgBox "Project Number must be provided on each line where reimbursement is being claimed.", vbCritical, "Important:"
Cancel = True
Exit Sub
End If
Next myCell
End Sub
Public Sub DivAdminApproval()
'Call DivAdminApproval
With Worksheets("Travel Expense Voucher")
If Worksheets("Travel Expense Voucher").Cells("F5") = 2 Then
For Each myCell In .Range("O15:O45")
If myCell.Value = 0.58 Then
MsgBox "You have selected reimbursement at the 'HIGH' mileage rate ($.58/mile). To receive reimbursement at this rate, Division Administrator Approval is Required.", vbCritical, "Important:"
Exit Sub
End If
Next myCell
End Sub
The ProjNumbrReq and DivAdminApproval macros have been listed before End Sub, hoping that would call them to work. However, they are not running.
something along these lines, if your macros are not in the 'ThisWorkbook' module then make sure they are defined public (instead of private):
Option Explicit
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Call sMacro1
Call sMacro2
Call sMacro3
End Sub
Private Sub sMacro1()
'do something
End Sub
Private Sub sMacro2()
'do something
End Sub
Private Sub sMacro3()
'do something
End Sub

Is it possible to enable Excel Manual Calculation for specific formulas?

I have a formula that makes an API request every time it's executed, which makes it slow. I'd like to prevent Excel from automatically recalculating cells containing this formula but still automatically recalculate other cells.
I've tried setting calculation mode to Manual with:
Application.Calculation = xlCalculationManual
However this prevents other cells without my formula from calculating automatically.
Another idea I've had is to check if a cell has been "frozen" and then return it's current value instead of calling the API for a new value. The issue with this is that Excel doesn't provide a way to exit the function without altering the cell value.
Function MyFormula() As Variant
If CellIsFrozen() Then
MyFormula = Application.Caller.Value 'return current value
Else
MyFormula = GetNewValueFromAPI() 'expensive call to server
End If
End Function
My issue with the above is that Application.Caller.Value returns the cell value by performing a recalculation and results in an infinite recursion.
FYI - the CellIsFrozen method is just an example sub that would somehow check whether the cell was called automatically or manually.
I'm also aware of Application.Caller.Value2 and .text, unfortunately these don't help me. Value2 also causes a recalculation, and text just returns a string representation (which is not useful because it could be "######" if the value is a date and the column is too narrow).
Is there a way to interrupt Excel's recalculation process for specific formulas?
Otherwise, is it possible to extract a value of a cell without performing a recalculation - I'm guessing that Excel stores the value somewhere because it's visible on the worksheet, it makes no sense to insist on recalculating every time.
In the context my previous answer to the post involving single cell, i also want share our old experience involving multiple cells. that days We used the formula in an indexed fashion like =myformula(1)... etc and stored it in a global array. Now today thanks to your great idea of Caller function. I recreated another improvised solution involving multiple cells.
Here again in module1
Global Flag As Boolean, LastValArr(1 To 10, 1 To 2) As Variant, Ws As Worksheet, Rng As Range
Public Function MyFormula() As Variant
Dim Adr As String, X As Integer
If Flag Then
MyFormula = GetNewValueFromAPI() 'expensive call to server
Else
Adr = Application.Caller.Address
For X = 1 To 10
If InStr(1, LastValArr(X, 2), Adr) > 0 Then
MyFormula = LastValArr(X, 1)
Exit For
End If
Next
End If
End Function
Function GetNewValueFromAPI() As Variant
GetNewValueFromAPI = Application.WorksheetFunction.RandBetween(1, 1000)
End Function
Sub CalcA1()
Flag = True
Rng.Dirty
Flag = False
Ws.Range("F1").Value = IIf(Flag, "On", "Off")
End Sub
Sub ToggleFlag()
Flag = Not Flag
Ws.Range("F1").Value = IIf(Flag, "On", "Off")
If Flag Then Rng.Dirty
End Sub
in Workbook_Open event
Private Sub Workbook_Open()
Dim X As Integer
Dim Cell As Range
Set Ws = ThisWorkbook.Sheets("Sheet1")
Set Rng = Ws.Range("A1:A5")
Set Rng = Union(Rng, Ws.Range("C1:C5"))
Flag = True
Rng.Dirty
Flag = False
Ws.Range("F1").Value = IIf(Flag, "On", "Off")
End Sub
in Sheet1 Worksheet_Calculate event
Private Sub Worksheet_Calculate()
Dim X As Integer
Dim Cell As Range
X = 1
For Each Cell In Rng.Cells
LastValArr(X, 1) = Cell.Value
LastValArr(X, 2) = Cell.Address
X = X + 1
Next
End Sub
Edit: On second thought after initial feel good of posting the Demo answer, I found it lacks User friendliness and ease of copy pasting UDF formulas while working in Excel Therefore i tried improvise it further so it could be used by users don't have access to VBA code and could work with copy paste of the UDF.
So 1st I came across a solution to store the Last Values in a temp sheet (may be Very Hidden Sheet). with apprehension that working with cell access may degrade performance of the code, I refrained from posting it and I finally restored to Dictionary Object.
This solution have added with basic advantage of Auto mapping of formula cells (by searching "=myformula" in used range of the Sheet) to enable/disable calculation. This would enable users without access to code modules to work freely with UDF.
Here reference to Microsoft scripting runtime has been added.
Code in module:
Global Flag As Boolean, Ws As Worksheet, Rng As Range, Dict As Dictionary
Public Function MyFormula() As Variant
Dim Adr As String
If Flag Then
MyFormula = GetNewValueFromAPI() 'expensive call to server
Else
Adr = Application.Caller.Address
'Debug.Print Adr
MyFormula = IIf(Dict.Exists(Adr), Dict(Adr), 0)
End If
End Function
Function GetNewValueFromAPI() As Variant
'Delay (2)
GetNewValueFromAPI = Application.WorksheetFunction.RandBetween(1, 1000)
End Function
Sub CalcA1()
Flag = True
If Not Rng Is Nothing Then Rng.Dirty
'Debug.Print "in calA1"
Flag = False
Ws.Range("F1").Value = IIf(Flag, "On", "Off")
End Sub
Sub ToggleFlag()
Flag = Not Flag
Ws.Range("F1").Value = IIf(Flag, "On", "Off")
If Flag And Not Rng Is Nothing Then Rng.Dirty
End Sub
Sub BuildRange()
Application.EnableEvents = False
Dim Cell As Range
CalcCnt = CalcCnt + 1
Set Rng = Nothing
Dict.RemoveAll
For Each Cell In Ws.UsedRange.Cells
If Left(Cell.Formula, 10) = "=myformula" Then
'Debug.Print "From Sht Calc -" & Cell.Address
If Dict.Exists(Cell.Address) = False Then
Dict.Add Cell.Address, Cell.Value
Else
Dict(Cell.Address) = Cell.Value
End If
If Rng Is Nothing Then
Set Rng = Cell
Else
Set Rng = Union(Rng, Cell)
End If
End If
Next
Application.EnableEvents = True
End Sub
In Workbook_Open
Private Sub Workbook_Open()
'Dim X As Integer
Dim Cell As Range
Set Ws = ThisWorkbook.Sheets("Sheet1")
Set Dict = New Dictionary
Flag = True
BuildRange
If Not Rng Is Nothing Then Rng.Dirty
Flag = False
Ws.Range("F1").Value = IIf(Flag, "On", "Off")
End Sub
In Sheet Calculate event
Private Sub Worksheet_Calculate()
BuildRange
End Sub
If you are using an UDF in the cell, I will like to make it like this workaround.
For demo and test, Only used a single cell A1 in "Sheet1" , instead of using any API, I used WorksheetFunction.RandomBetween May use range and array if multiple cells are used.
In "Sheet1" cell A1 used =myFormula()
in a module
Public Flag As Boolean, LastVal As Variant
Public Function MyFormula() As Variant
If Flag Then
MyFormula = GetNewValueFromAPI() 'expensive call to server
Else
MyFormula = LastVal
End If
End Function
Function GetNewValueFromAPI() As Variant
GetNewValueFromAPI = Application.WorksheetFunction.RandBetween(1, 1000)
End Function
Sub CalcA1 in Module1 would be used to recalculate A1 whenever necessary. It could be called from any events also according to actual requirement.
Sub CalcA1()
Flag = True
Worksheets("Sheet1").Range("A1").Dirty
Flag = False
End Sub
In workbook Open event the the LastVal was calculated with Flag as true and then Flag was reset to false to prevent further calling GetNewValueFromAPI
Private Sub Workbook_Open()
Flag = True
Worksheets("Sheet1").Range("A1").Dirty
LastVal = Worksheets("Sheet1").Range("A1").Value
Flag = False
End Sub
In Worksheet_Calculate event of Sheet1 the LastVal is being recorded.
Private Sub Worksheet_Calculate()
LastVal = Worksheets("Sheet1").Range("A1").Value
End Sub
Working Demo
Regret, I came across this post (A Real Good Question) late, since We had already been used something in this line in our workplace. Thanks to #Pawel Czyz for editing the post it came under Active List today only.

How to exit from the loop when user clicks X button on userform?

I have vba code below to edit every row one by one on userform interface:
Private Sub CommandButton1_Click()
Dim MyRange As Range
Dim cell As Range
Set MyRange = Range("A2", Range("A2").End(xlDown))
For Each cell In MyRange
cell.Select
'Build form
MyForm.Title.Caption = cell.Offset(0, 2)
If cell.Offset(0, 2) = 1 Then
MyForm.Checked.Value = True
Else
MyForm.Checked.Value = False
End If
'Show form
MyForm.Show
Next cell
End Sub
I have thousand of rows and I need to give user a some break!
I want this process to pause if user clicks X button, but I couldn't find a way to exit from the loop.
My userform code is like below:
Private Sub Checked_Click()
If MyForm.Checked.Value = True Then
ActiveCell.Offset(0, 1).Value = 1
Else
ActiveCell.Offset(0, 1).Value = ""
End If
End Sub
Private Sub DoneButton_Click()
ActiveCell.Offset(0, 2).Value = 1
Unload MyForm
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = vbFormControlMenu Then
MsgBox "paused"
End If
End Sub
What I'm trying to accomplish?
I need to stop for each loop at first block of code when users click X button to close the userform. But I don't know how to listen if UserForm_QueryClose called to exit loop from first block of code.
Sadly I don't think this is possible, given vba doesn't support multi-threading.
If you really insist on asking the user whether to pause, it would be better to simply ask an If conditional during the run-time.
eg.
For i = 1 to myRange.Cells.Count
' your code here...
If i = myRange.Cells.Count / 2 Then
If MsgBox("Continue to the for loop?", vbYesNo) = vbNo Then
UnLoad MyForm
Exit For
End If
End If
Next i
You need to add DoEvents which will check for a user interaction. However, simply closing the form will not stop the code from running so you will need to add another controller.
For example
Option Explicit
Private stopCode As Boolean
Private Sub UserForm_Click()
Dim i As Long
stopCode = False
For i = 1 To 1000000
Debug.Print i
DoEvents
If stopCode = True Then Exit For
Next
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If MsgBox("Do you want to stop", vbQuestion + vbYesNo) <> vbYes Then Cancel = True
stopCode = True
End Sub
When the user closes the form they will be asked if they want to stop the code. If they say yes it closes the form but the stopCode 'controller' also makes sure the running code is stopped.

"Ok" command box in userform

basically I have a userform which I would like to use to enter 2 data into another macro which I already have. The userform is as below:
Basically, I would like the OK button to be clicked and the data in the two boxes will be entered into another macro that I have. It would also be great if the OK button can help in a sense that it will prompt a warning if one of the boxes is not filled up.
So far, I do not have much of a code for this..
Private Sub UserForm_Click()
TextBox1.SetFocus
Sub Enterval()
End Sub
Private Sub TextBox1_Change()
Dim ID As String
ID = UserForm3.TextBox1.Value
End Sub
Private Sub TextBox2_Change()
Dim ID2 As String
ID2 = UserForm3.TextBox2.Value
End Sub
Private Sub OKay_Click()
Enterval
End Sub
Would appreciate any tips and help. Thanks!
My other macro
Private Sub CommandButton1_Click()
Dim Name As String
Dim Problem As Integer
Dim Source As Worksheet, Target As Worksheet
Dim ItsAMatch As Boolean
Dim i As Integer
Set Source = ThisWorkbook.Worksheets("Sheet1")
Set Target = ThisWorkbook.Worksheets("Sheet2")
Name = Source.Range("A3")
Problem = Source.Range("I13")
Do Until IsEmpty(Target.Cells(4 + i, 6)) ' This will loop down through non empty cells from row 5 of column 2
If Target.Cells(4 + i, 6) = Name Then
ItsAMatch = True
Target.Cells(4 + i, 7) = Problem ' This will overwrite your "Problem" value if the name was already in the column
Exit Do
End If
i = i + 1
Loop
' This will write new records if the name hasn't been already found
If ItsAMatch = False Then
Target.Cells(3, 6).End(xlDown).Offset(1, 0) = Name
Target.Cells(4, 6).End(xlDown).Offset(0, 1) = Problem
End If
Set Source = Nothing
Set Target = Nothing
End Sub
Thats the macro i have. As u said, i change the
othermacro
to CommandButton1_Click()
but it doesn't work
Quoting geoB except for one thing: when you .Show your UserForm from a main Sub, you can also .Hide it at the end and the macro that called it will continue its procedures.
Sub Okay_Click()
Dim sID1 As String, sID2 As String
' A little variation
If Me.TextBox1 = "" Or Me.TextBox2 = "" Then
MsgBox "Please fill all the input fields"
Exit Sub
End If
Me.Hide
End Sub
To address your TextBox, you can write in your main Sub UserForm3.TextBox1 for example
There is no need for an Enterval function. Instead, assume the user can read and follow instructions, then test whether that indeed is the case. Note that in your code ID and ID2 will never be used because they exist only within the scope of the subroutines in which they are declared and receive values.
To get started:
Sub Okay_Click()
Dim sID1 As String, sID2 As String
sID1 = UserForm3.TextBox1.Value
sID2 = UserForm3.TextBox2.Value
If Len(sID1 & vbNullString) = 0 Then
MsgBox "Box A is empty"
Exit Sub
End If
If Len(sID2 & vbNullString) = 0 Then
MsgBox "Box B is empty"
Exit Sub
End If
'Now do something with sID1, sID2
otherMacro(sID1, sID2)
End Sub
For your other macro, declare it like this:
Sub otherMacro(ID1, ID2)
...
End Sub
Also, the SetFocus method should occur in the form open event.

Resources