Check whether a changed cell in a named range excel vba - vba

I just need to monitor changes in a worksheet, not in the whole worksheet, just inside a named range. So if any kind of change occurs in any cell it should check the change occurs in that named range and if so some function has to do.
Here is my code
Private Sub Worksheet_Change(ByVal Target As Range)
Dim ir As Boolean
ir = Application.Intersect(Target.Address, Range("bd_main"))
If ir = True Then
MsgBox "change"
End If
End Sub
But it triggers an error saying Type Mismatch in the Application.Intersect function for Target.Address
What I did wrong there which causes such error.

With slight modification, use the code below:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Application.Intersect(Target, Range("bd_main")) Is Nothing Then
MsgBox "change"
End If
End Sub

Related

Update the name of Excel Worksheet dynamically- VBA excel

I am trying to write a macro where i can equate the name of a worksheet to a cell value.
So far i have only been able to extract name of the worksheet and put it to a cell value.
Is there a way i can achieve the above?
Thanks
There is already a solution to this,
Follow this link
Credit to folks at Extendoffice.com
Pasting code here for reference, just follow the link you'll find details
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Set Target = Range("A1")
If Target = "" Then Exit Sub
Application.ActiveSheet.Name = VBA.Left(Target, 31)
Exit Sub
End Sub
You can easily achieve this with the Worksheet_Change event of the worksheet.
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
'note that A1 here is the cell that contains the sheet name. Adjust it to your needs.
If Target.Address(False, False) = "A1" Then
Target.Parent.Name = Target.Text
End If
End Sub
Every time the value of A1 changes, the worksheet name changes accordingly.
Note this procedure has to be in a worksheet scope not within a module.
It can be useful to implement an error handling for not allowed or empty sheet names.
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
'note that A1 here is the cell that contains the sheet name. Adjust it to your needs.
If Target.Address(False, False) = "A1" And Target.Text <> vbNullString Then
On Error GoTo ERR_NO_RENAME
Target.Parent.Name = Target.Text
On Error GoTo 0
End If
Exit Sub
ERR_NO_RENAME:
If Err Then MsgBox Err.Description, vbCritical, Err.Number, Err.HelpFile, Err.HelpContext
End Sub

excel on change not working if cell value is changed by another module

i have a range whose value is changed realtime but the onchange module does nothing if value is changed by other module for that range. however if i change value manually it works.
code :-
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng As Range
Dim lastRow As Long
Dim cell As Range
If Not Intersect(Target, Range("J10:J43")) Is Nothing Then
Application.EnableEvents = False
For Each cell In Target
If cell.Value < cell.Offset(0, 4).Value Then
cell.Offset(0, 7).Value = cell.Offset(0, 1).Value
'Module1.OnGenOrder
End If
Next cell
End If
Application.EnableEvents = True
End Sub
NOTE:- i think module Private Sub Worksheet_Change(ByVal Target As Range)
is not able to sense changes. The value is changed by a module in another external .xla file. but a change by simple formulas like =a1+b1 works well
update
this is code of cell to monitor
=c:\Excelmacros\updateprice.xla!dataupdate($H12,"price1")
Event handler procedures have a simple naming convention:
Private Sub [EventSource]_[EventName]([args])
Seeing how the event source is Worksheet, it looks like your handler is in some worksheet's code-behind module; that will only ever respond to Change events on that worksheet.
If you want to handle Change events on any worksheet in ThisWorkbook, then handle the SheetChange event of the Workbook class, in the code-behind module for ThisWorkbook:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
End Sub
Notice how the changed sheet is being received as a parameter.
If you want to handle Change worksheet events on any worksheet in another workbook, then you need a class module and a WithEvents field - the ThisWorkbook code-behind can serve (a workbook is a class, after all), for simplicity's sake:
Private WithEvents app As Excel.Application
You'll need to Set that app event source to a valid Excel.Application object reference as appropriate (say, in the Open handler for ThisWorkbook), and then you can handle application-wide events:
Private Sub Workbook_Open()
Set app = Excel.Application
End Sub
Private Sub app_SheetChange(ByVal Sh As Object, ByVal Target As Range)
MsgBox "Cell " & Target.Address(External:=True) & " was changed."
End Sub

How to trigger Worksheet_Change from OLEOjects?

I want the macro to run automatically when the combo box value in worksheet change but I did not find any code that can fix my code.
Here is the code
Private Sub Worksheet_Change(ByVal Target As Range)
With Me.CBbox
If Not Intersect(Target, ActiveSheet.Range(OLEObjects.LinkedCell)) Is Nothing Then
Call Macro
End If
End With
End sub

Run-Time error '1004' with my VBA for hiding and unhiding rows

I have code for hiding and unhiding rows in my sheet based on changing the value in my dropdown. Every time I change the dropdown I get Run-Time error of '1004'. I had a private Sub before and changed it to a Sub but that doesn't seem to be the solution.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng As Range
Set rng = Target.Parent.Range("L6")
If Target.Count > 1 Then Exit Sub
If Intersect(Target, rng) Is Nothing Then Exit Sub
Application.Run "dynamic_hide"
End Sub
Sub dynamic_hide()
If Target.Range = "$S$9:$S$51" Then
If Target.Range = 0 Then Rows("F9:T51").EntireRow.Hidden = True
If Target.Value <> 0 Then Rows("F9:T51").EntireRow.Hidden = False
End If
End Sub
You have a few problems going on here:
First, the default property of a Range object is Value, so Target.Range = "$S$9:$S$51" will always be false. Use Target.Address instead.
Second, don't use Application.Run to call Subs from the same VBProject. Use Call instead.
Third, you've not let the sub dynamic_hide know what Target is since Target is only a parameter of the Worksheet_Change event subroutine. You can solve this by declaring your sub like Sub dynamic_hide(ByVal Target As Range) And then you can use it: Call dynamic_hide(Target)
Lastly, since Target is a range you don't need to use Target.Range since Target is a range so you can simply omit every .Range from Target.Range Target.Parent.Range is fine.

Changing a PivotTable filter by entering a value into a cell

I have looked around for an answer and in fact the code that I am using is from this site. I have the VBA for changing PivotTable filters by inputing a value into a seperate cell like so:
Sub Worksheet_Change(ByVal Target As Range)
If Not Application.Intersect(Target, Sheets("Dashboard").Range("Lane1")) Is Nothing Then
Sheets("Dashboard").PivotTables("PivotTable1").PivotFields("TLEG"). _
ClearAllFilters
Sheets("Dashboard").PivotTables("PivotTable1").PivotFields("TLEG").CurrentPage _
= Sheets("Dashboard").Range("Lane1").Value
End If
End SUb
The code works fine. It lets me enter the value and filters accordingly, but when I delete the value in the cell it does not filter "all". Instead, the problem I run into is that a runtime error '1004' is thrown when I delete the value in the cell. I hit end and it gives me the answer. However the error keeps popping up.
I am pretty much a newborn when it comes VBA, so it might be so that I have missed something glaringly obvious.
Sub Worksheet_Change(ByVal Target As Range)
Dim rng As Range
Set rng=Me.Range("Lane1")
If Not Application.Intersect(Target, rng) Is Nothing Then
With Sheets("Dashboard").PivotTables("PivotTable1").PivotFields("TLEG")
.ClearAllFilters
If Len(rng.Value)>0 Then .CurrentPage = rng.Value
End With
End If
End SUb