VBA update cell on worksheet change

MartinL

Well-known Member
Joined
Oct 16, 2008
Messages
1,144
Office Version
  1. 365
Platform
  1. Windows
Sorry guys I have to raise this old chestnut

I have a data feed that overwrites my column titles, so my pivot table goes awkward.

All I am trying to do is ensure that Cell "C24" on worksheet "Data"
always has the value "Product" in it. also various other cells that I can populate once ive see what I'm doing wrong.

I have tried
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Range("C24").Select = "" Then _
Range("C24") = "Product"
End Sub

but that don't work.
 
Try

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address(False, False) = "C24" And Target.Value = "" Then
    Application.EnableEvents = False
    Target.Value = "Product"
    Application.EnableEvents = True
End If
End Sub
 
Upvote 0
Hi Peter

This only appears to work after I click on C24 and press delete even if the cell is empty.

Then the text "Product" appears.

I am hoping to get the code to run either when I refresh my web selection or even if I add me.calculate to the VBA code
 
Upvote 0
That suggests that your data feed doesn't trigger the Worksheet_Change event. Perhaps it will trigger a calculation

Code:
Private Sub Worksheet_Calculate()
If Range("D24").Value = "" Then Range("D24").Value = "Product"
End Sub
 
Upvote 0
Eventually I want C24:K24 to contain the values

"Product", "Barcode", "Line", "Sector", "52 Weeks", "26 Weeks", "13 Weeks", "4 Weeks", "1 Week"

I say that because an array may be the way to go, rather than copying the same bit of code 9 times.
 
Upvote 0
Then try

Code:
Private Sub Worksheet_Calculate()
If Range("C24").Value = "" Then Range("C24:K24").Value = Array("Product", "Barcode", "Line", "Sector", "52 Weeks", "26 Weeks", "13 Weeks", "4 Weeks", "1 Week")
End Sub
 
Upvote 0
:biggrin:Just add me.calculate at the top and wow

VoG you are "King of the Lab" :beerchug:
 
Upvote 0

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