VBA Using Range Expression from other sub - excel

I have 2 different modules. One is triggering the other. Like that;
Private Sub Worksheet_Change(ByVal TarGet As Range)
If Not Intersect(TarGet, Range("o7:o1000")) Is Nothing Then
Call dograma
End If
End Sub
And the other one is starting like that:
Sub dograma()
Txt = TarGet.Value
I get an error because "TarGet" expression not defined in this sub. But I would like to use the same variable for both subs. Can I synchronize them?

In second sub i changed name like that
Sub dograma(ByVal TarGet As Range)
and
In the first module I changed
Call Dograma
with
Call Dograma (TarGet)
It works well.

Related

VBA if a specific columns and rows are selected, Type Mismatch error

I am trying to write a code where sub activates when I select a specific column and specific row but gives me a type mismatch error on the following code:
If Selection.Worksheet.Columns("E:L") And Selection.Worksheet.Rows("16:99999") Then
You will see that the keyword Target always returns the Range Object of the cell that fired the Worksheet_Change Event. So, armed with this knowledge we can now use the Intersect Method to take some action when/if specified cell is changed. For example;
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("E12:L54787")) Is Nothing Then
MsgBox "Hello"
End If
End Sub
Write the code in the corresponding worksheet:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If (Not Intersect(Target, Columns("E:L")) Is Nothing) And _
(Not Intersect(Target, Rows("16:99999")) Is Nothing) Then
Debug.Print Target.Address
End If
End Sub
Selection.Change documentation
Application.Intersect method documentation

Event triggers and Range Function - Run-time error

I have the following worksheet function saved in the Sheet1 (DataInput) module of VBA
Sub worksheet_change(ByVal target As Range)
Set target = Range("F67")
If target.Value = "YES" Then
Call ThisWorkbook.Red_Macro_Text
End If
End Sub
This code calls the following code in ThisWorkbook
Public Sub Red_Macro_Text()
ThisWorkbook.Worksheets("DataInput").Range("B68").Value2 = ThisWorkbook.Worksheets("Text").Range("B3").Value2
End Sub
The purpose of the code is as follows:
The code uses an event trigger to watch for changes to worksheet ‘datainput’ cell F67.
Whenever the user selects ‘YES’ from a drop down menu in cell F67 it calls the subroutine in ThisWorkbook.
The code uses the range function to assign the value of 'text' worksheet B3 into 'datainput' worksheet B68 cell.
The trigger event does call the code and the content of B3 does get transferred to B68 however following this I then get the error message:
Run-time error ‘-2147417848 (80010108)’: Automation error The object
invoked has disconnected from its clients.
Please note I do not want to use an IF statement in excel to achieve this as I am trying to automate the process in VBA.
I have been searching for a solution to this problem for some time without success.
Does anyone know how to solve this problem as I have reached the limit of my understanding!
All help is appreciated
It doesn't make sense to Set target = Range("F67") because it is already retrieved as parameter ByVal target As Range
If you want to run the code only if F67 changes use the following instead:
Sub worksheet_change(ByVal Target As Range)
If Not Intersect(Target, Target.Parent.Range("F67")) Is Nothing Then
If Target.Parent.Range("F67").Value = "YES" Then
ThisWorkbook.Red_Macro_Text
End If
End If
End Sub
Note that Call is not needed to call a macro.
Try
Sub worksheet_change(ByVal Target As Range)
If Target.Address = Range("F67").Address Then
If Target.Value = "YES" Then
Call ThisWorkbook.Red_Macro_Text
End If
End If
End Sub

VBA Worksheet Change is not working properly?

I wrote the following code in my worksheet and all ranges are in this same sheet.
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("DistMatrix")) Is Nothing Then
Dim out1() As Double
out1 = OutStat(bucket(Target), Range("RegScale"))
FwdOut = outright(bucket(Target), Range("RegScale"))
Call NewScatter(FwdOut, out1)
End If
End Sub
I want to run a called sub if I choose a cell in the range DistMatrix.
This is partly working. I have to click on a cell in the range as if I want to write in it and then afterwards pick another one for the called sub to run.
I want however the sub to run as soon as I pick the cell. I don't want to go have to double click it as if it were to edit it and then pick another one for it to run.
You can use Worksheet_SelectionChange instead.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Target, Range("DistMatrix")) Is Nothing Then
Dim out1() As Double
out1 = OutStat(bucket(Target), Range("RegScale"))
FwdOut = outright(bucket(Target), Range("RegScale"))
Call NewScatter(FwdOut, out1)
End If
End Sub

How to pass on the Target to another macro in Excel events?

How to pass on the Target to another macro in Excel events? For just the elegant purpose, I would like to use the following structure, which does not work now.
Private Sub Worksheet_Change(ByVal Target As Range)
GoToAnotherMacro(Target)
End Sub
Sub GoToAnotherMacro(Target)
'here my code where I can use Target i.e:
MsgBox "value: " & Target.Value
End Sub
I would suggest the use of a global variable:
1) Define a global variable:
Dim RangeSupp As Range
2) In your procedure, pass the local Target to your global variable
Private Sub Worksheet_Change(ByVal Target As Range)
Set RangeSupp = Target
GoToAnotherMacro RangeSupp
End Sub
3) Enjoy your target in your other macro
Sub GoToAnotherMacro(Target As Range)
MsgBox Target.Value
End Sub
NOTE: Global variables are put on top of all the code, outside any other Function or Sub. The global variable is preserved in the stack even outside the single scope of the macro. That is the reason why you will be able to pass a global variable from a macro to the other. On the other hand, now you're not able (you get "Object required error") because the Target object, once outside its scope in Worksheet_Change, is removed from the stack so it gets back to its default value (Nothing), causing the problem you have in your above code.

Passing through variable to sub in VBA

This is probably very basic, but I have tried so many things and the always give errors.
Basically what I'm trying to do is run a procedure every time a cell gets changed in Excel. So I'm using Sub Worksheet_Change(ByVal Target As Range).
All works well, but in this procedure, I'm calling another sub procedure several times.
I want to reuse the 'Target' value in this procedure, but for some reason, I can't find how to do it. I tried placing 'Public rTarget As Range' and do a 'rTarget = Target' at the beginning of the procedure. But the rTarget stays empty when I call the sub procedure.
How do I make this work?
I'm now adding Target as one of the variables to to subroutine, but that just looks stupid.
Thanks!
Private Sub Worksheet_Change(ByVal Target As Range)
MySub Target
End Sub
Sub MySub(ByVal Target As Range)
' Your sub code goes here and can work with the Target Range from the Worksheet_Change Event
End Sub
Passing Target as a variable is how you should do it. You want to keep your variables in the tightest scope possible and passing them as arguments is the tightest.
If you're passing the variable to a bunch of different procedures in the same module, it may make sense to have a module-level variable. That looks like this
Private mrTarget As Range
Private Sub Worksheet_Change(ByVal Target As Range)
Set mrTarget = Target
MySub
End Sub
Private Sub MySub()
Debug.Print mrTarget.Address
End Sub
Based on your questions, it's just one sub that gets called repeatedly. In that case, I would stick with passing it as an argument.

Resources