Worksheet Change Event- Call Macro

davidhall

Board Regular
Joined
Mar 6, 2011
Messages
174
I am trying to have a macro run automatically when the worksheet titled "Index" cell "A1" is changed. A1 references another worksheet that users change so it has a formula in it.


In the View Code of the Index sheet, I have the following code:


Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Address(False, False) = "A1" Then
Application.EnableEvents = False

Call IndexDataExtract

Application.EnableEvents = True

End If


End Sub



The macro that I need to run upon cell A1 changing is called IndexDataExtract.

The Vba for IndexDataExtract is placed in a module.

The code is as follows:



Option Explicit
Option Compare Text
Sub IndexDataExtract()


Dim i As Long
Dim FinalRow As Long
Dim NextRow As Integer

Application.ScreenUpdating = False
FinalRow = Worksheets("IndexImport").Cells(Rows.Count, 2).End(xlUp).Row
NextRow = Worksheets("Index").Cells(Rows.Count, 1).End(xlUp).Row + 1
Worksheets("Index").Range("A3:D500").ClearContents
NextRow = 3
Worksheets("IndexImport").Activate
For i = 1 To FinalRow
If Cells(i, 2).Value = Worksheets("Index").Range("A1").Value Then
Cells(i, 2).Resize(1, 4).Copy Destination:=Worksheets("Index").Cells(NextRow, 1)
NextRow = NextRow + 1
End If
Next i
Worksheets("Index").Activate

'Filtering Column C which is the index date, from the most recent to oldest

Rows("3:3").Select
Selection.AutoFilter
ActiveWorkbook.Worksheets("Index").AutoFilter.Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Index").AutoFilter.Sort.SortFields.Add Key:=Range( _
"C3"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
xlSortNormal
With ActiveWorkbook.Worksheets("Index").AutoFilter.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Selection.AutoFilter

Application.ScreenUpdating = True

End Sub


I have tried placing the IndexDataExtract code in ThisWorkbook and in the view code section with the private sub code but cant seem to get it to work.

Any suggestions?
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
If A1 houses a formula try like this

Code:
Private Sub Worksheet_Calculate()
Static OldVal As Variant
If Range("A1").Value <> OldVal Then
    Application.EnableEvents = False
    OldVal = Range("A1").Value
    Call IndexDataExtract
    Application.EnableEvents = True
End If
End Sub

the IndexDataExtract code should go in a regular module.
 
Upvote 0
Ok. Does the code for the Sub IndexDataExtract need to be placed in a module, in ThisWorkbook, or in the worksheet Index?
 
Upvote 0
When I the value is update an error appears.

It says:

Compile Error
Expected Variable or Procedure, not module.

It is pointing to the following line.

Private Sub Worksheet_Calculate()
Static OldVal As Variant
If Range("A1").Value <> OldVal Then
Application.EnableEvents = False
OldVal = Range("A1").Value
Call IndexDataExtract
Application.EnableEvents = True
End If
End Sub
 
Upvote 0
Have you deleted IndexDataExtract from all the wrong places where you had it?

You haven't named a module
IndexDataExtract?
 
Upvote 0
Yes,
It has been deleted from ThisWorkbook and from the worksheets "Index" view code area.


Currently, I have a module named IndexDataExtract and the vba you provided in the view code area of the Index worksheet
 
Upvote 0
That worked great. It is not functioning properly.

By any chance, do you know of a way to make it run quicker? It seems to take 4-5 seconds. Do I need to mess around with the screenupdating aspect?
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,285
Members
452,902
Latest member
Knuddeluff

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top