Run macro on cell change - based on it´s value

alexbat

New Member
Joined
Dec 12, 2013
Messages
32
Hi,

want to check if a cell changes, and based on it´s new value perform an action.

Tried with

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   If Not Intersect(Target, Target.Worksheet.Range("D1")) Is Nothing Then
  
    If Range("D1").Value = "1" Then
    
    Range("B4").Select
    
    ActiveSheet.PivotTables("PivotTable1").PivotFields("Global Ultimate").AutoSort _
       xlAscending, "Sum of NFR Total", ActiveSheet.PivotTables("PivotTable1"). _
      PivotColumnAxis.PivotLines(1), 1
    
    ElseIf Range("B4").Value = "2" Then
    
    ActiveSheet.PivotTables("PivotTable1").PivotFields("Global Ultimate").AutoSort _
       xlDescending, "Sum of NFR Total", ActiveSheet.PivotTables("PivotTable1"). _
      PivotColumnAxis.PivotLines(1), 1
    
    End If
End Sub

but nothing happens.

Any ideas?
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
hey bud, I didnt really change much, but i made it to where when range D1 = anything it runs the macro and left b4 alone, you forgot an end if at the end of your statement.

Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Target.Worksheet.Range("D1")) Is Nothing Then

If Range("D1").Value <> "" Then

Range("B4").Select

ActiveSheet.PivotTables("PivotTable1").PivotFields("Global Ultimate").AutoSort _
xlAscending, "Sum of NFR Total", ActiveSheet.PivotTables("PivotTable1"). _
PivotColumnAxis.PivotLines(1), 1

ElseIf Range("B4").Value = "2" Then

ActiveSheet.PivotTables("PivotTable1").PivotFields("Global Ultimate").AutoSort _
xlDescending, "Sum of NFR Total", ActiveSheet.PivotTables("PivotTable1"). _
PivotColumnAxis.PivotLines(1), 1

End If
End If

End Sub

make sure you put this macro on the sheet you are wanting those cells to run the macro on.
 
Upvote 0
Change line 2 to the below - Does that help?

If Not Intersect(Target, Range("D1")) Is Nothing Then
 
Upvote 0
Sorry, code messed up in previous post, should be

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   If Not Intersect(Target, Target.Worksheet.Range("D1")) Is Nothing Then
  
    If Range("D1").Value = "1" Then
    
    Range("B4").Select
    
    ActiveSheet.PivotTables("PivotTable1").PivotFields("Global Ultimate").AutoSort _
       xlAscending, "Sum of NFR Total", ActiveSheet.PivotTables("PivotTable1"). _
      PivotColumnAxis.PivotLines(1), 1
    
    ElseIf Range("D1").Value = "2" Then
    
    Range("B4").Select
    
    ActiveSheet.PivotTables("PivotTable1").PivotFields("Global Ultimate").AutoSort _
       xlDescending, "Sum of NFR Total", ActiveSheet.PivotTables("PivotTable1"). _
      PivotColumnAxis.PivotLines(1), 1
    
    End If
End Sub
 
Upvote 0
Note this line:
Code:
If Range("D1").Value = "1" Then
Since you enclosed the 1 in quotes, it is checking for a string value of 1, not a numeric value of 1 (1 does not equal "1").
So if you are actually checking for a numeric value of 1, drop the double-quotes that surround it.

Same thing with checking for 2 in B4.
 
Upvote 0
Corrected the code according to your suggestions guys, but still nothing happening to, in this case, the Pivot table.

The code looks like this currently:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   If Not Intersect(Target, Target.Worksheet.Range("D1")) Is Nothing Then
  
    If Range("D1").Value = 1 Then
    
    Range("B4").Select
    
    ActiveSheet.PivotTables("PivotTable1").PivotFields("Global Ultimate").AutoSort _
       xlAscending, "Sum of NFR Total", ActiveSheet.PivotTables("PivotTable1"). _
      PivotColumnAxis.PivotLines(1), 1
    
    ElseIf Range("D1").Value = 2 Then
    
    Range("B4").Select
    
    ActiveSheet.PivotTables("PivotTable1").PivotFields("Global Ultimate").AutoSort _
       xlDescending, "Sum of NFR Total", ActiveSheet.PivotTables("PivotTable1"). _
      PivotColumnAxis.PivotLines(1), 1
    
    End If
    End If
End Sub
 
Upvote 0
How is the value in D1 changing?
Is someone physically changing it, or is it a formula. If it is a formula, Worksheet_Change won't catch changes in values brought about by formulas.
You would probably need to use the Worksheet_Calculate event instead.
 
Upvote 0
Joe4,

D1 is changed by a Option Button form control. I have 2 options, "Top 20" and "Bottom 20", and I want to sort the Pivot based on
the choice done....
 
Upvote 0
Private Sub Worksheet_Change(ByVal Target As Range)


If Not Intersect(Target, Target.Worksheet.Range("D1")) Is Nothing Then

If Range("D1").Value = "Top 20" Then
Range("B4").Select
ActiveSheet.PivotTables("PivotTable1").PivotFields("Global Ultimate").AutoSort xlAscending, "Sum of NFR Total", ActiveSheet.PivotTables("PivotTable1").PivotColumnAxis.PivotLines(1), 1

ElseIf Range("D1").Value = "Bottom 20" Then

Range("B4").Select
ActiveSheet.PivotTables("PivotTable1").PivotFields("Global Ultimate").AutoSort xlDescending, "Sum of NFR Total", ActiveSheet.PivotTables("PivotTable1").PivotColumnAxis.PivotLines(1), 1

End If
End If
End Sub
 
Upvote 0
Dustins... Sorry for being unclear, the "Top 20" and "Bottom 20" are just button descriptions, they render 1 or 2 when chosen...
 
Upvote 0

Forum statistics

Threads
1,216,077
Messages
6,128,679
Members
449,463
Latest member
Jojomen56

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