Ga naar hoofdinhoud

 Hoe automatisch een macro uitvoeren wanneer het resultaat van de celformule verandert?

Stel dat ik een lijst met formules heb op basis van de gegevens in kolom A en kolom B zoals in de volgende schermafbeelding, nu wil ik automatisch een specifieke macrocode uitvoeren wanneer het formuleresultaat verandert met de relatieve cellen veranderen. Is er een goed idee om deze taak in Excel op te lossen?

Voer automatisch een macro uit wanneer het resultaat van de celformule verandert met VBA-code


Voer automatisch een macro uit wanneer het resultaat van de celformule verandert met VBA-code

De volgende VBA-code kan u helpen om een ​​specifieke code automatisch uit te voeren wanneer de cel van het formule-resultaat verandert, doe dit als volgt:

1. Klik met de rechtermuisknop op de bladtab die u wilt gebruiken en kies vervolgens Bekijk code vanuit het contextmenu, in het geopende Microsoft Visual Basic voor applicaties venster, kopieer en plak de volgende code in de lege module:

VBA-code: voer de macro automatisch uit wanneer het resultaat van de celformule verandert:

Private Sub Worksheet_Calculate()
'Updateby Extendoffice
    Dim Xrg As Range
    Set Xrg = Range("C2:C8")
    If Not Intersect(Xrg, Range("C2:C8")) Is Nothing Then
    Macro1
    End If
End Sub

Note: In de bovenstaande code, C2: C8 is het bereik van formulecellen dat u wilt gebruiken,maco1 is de naam van de macro die u automatisch wilt uitvoeren. Wijzig het naar uw behoefte.

2. Sla vervolgens dit codevenster op en sluit het, nu, wanneer de gegevens in het bereik A2: B8 veranderen om ervoor te zorgen dat het formuleresultaat verandert, wordt uw specifieke macrocode onmiddellijk geactiveerd.

Beste Office-productiviteitstools

🤖 Kutools AI-assistent: Een revolutie teweegbrengen in de data-analyse op basis van: Intelligente uitvoering   |  Genereer code  |  Aangepaste formules maken  |  Analyseer gegevens en genereer grafieken  |  Roep Kutools-functies aan...
Populaire functies: Zoek, markeer of identificeer duplicaten   |  Verwijder lege rijen   |  Combineer kolommen of cellen zonder gegevens te verliezen   |   Ronde zonder formule ...
Super opzoeken: Meerdere criteria VLookup    VLookup met meerdere waarden  |   VOpzoeken over meerdere bladen   |   Fuzzy opzoeken ....
Geavanceerde vervolgkeuzelijst: Maak snel een vervolgkeuzelijst   |  Afhankelijke vervolgkeuzelijst   |  Multi-select vervolgkeuzelijst ....
Kolom Beheerder: Voeg een specifiek aantal kolommen toe  |  Kolommen verplaatsen  |  Schakel de zichtbaarheidsstatus van verborgen kolommen in  |  Vergelijk bereiken en kolommen ...
Uitgelichte functies: Raster focus   |  Ontwerpweergave   |   Grote formulebalk    Werkmap- en bladbeheer   |  resource Library (Auto-tekst)   |  Datumkiezer   |  Combineer werkbladen   |  Cellen coderen/decoderen    Stuur e-mails per lijst   |  Super filter   |   Speciaal filter (filter vet/cursief/doorhalen...) ...
Top 15 gereedschapsets12 Tekst Tools (toe te voegen tekst, Tekens verwijderen, ...)   |   50+ tabel Types (Gantt Chart, ...)   |   40+ Praktisch Formules (Bereken leeftijd op basis van verjaardag, ...)   |   19 Invoeging Tools (QR-code invoegen, Afbeelding invoegen vanaf pad, ...)   |   12 Camper ombouw Tools (Getallen naar woorden, Currency Conversion, ...)   |   7 Samenvoegen en splitsen Tools (Geavanceerd Combineer rijen, Gespleten cellen, ...)   |   ... en meer

Geef uw Excel-vaardigheden een boost met Kutools voor Excel en ervaar efficiëntie als nooit tevoren. Kutools voor Excel biedt meer dan 300 geavanceerde functies om de productiviteit te verhogen en tijd te besparen.  Klik hier om de functie te krijgen die u het meest nodig heeft...

Omschrijving


Office-tabblad Brengt een interface met tabbladen naar Office en maakt uw werk veel gemakkelijker

  • Schakel bewerken en lezen met tabbladen in Word, Excel, PowerPoint in, Publisher, Access, Visio en Project.
  • Open en maak meerdere documenten in nieuwe tabbladen van hetzelfde venster in plaats van in nieuwe vensters.
  • Verhoogt uw productiviteit met 50% en vermindert honderden muisklikken voor u elke dag!
Comments (10)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
Hello team,

I know this is a old topic but how to have a mix between this calculate method and this: https://www.extendoffice.com/documents/excel/1895-excel-record-date-and-time-when-cell-changes.html?

The goal is to record a date on the next cell every time that the calculated values change for every cell in a column.

Regards,
Tiago
This comment was minimized by the moderator on the site
Hey guys,
Below is the code and I want to lock cells A2 and A3 after cell A1 (A1 = B1+C1) is changing in results change either B1 or C1 or both. But it does not work. Could anyone help with that, please?

Private Sub Worksheet_Calculate()

Dim sPass
sPass = "123"
Dim rng As Range
Set rng = [A2:A3]
If Not Intersect(rng, [A1]) Is Nothing Then
With ActiveSheet
.Unprotect Password:=sPass
.Cells.Locked = False
Static oldValue
If Range("A1") <> oldValue Then
rng.Locked = True
.Protect Password:=sPass
oldValue = Range("A1").Value

End If
End With

End If
End Sub
This comment was minimized by the moderator on the site
"Set Xrg = Range("C2:C8")
If Not Intersect(Xrg, Range("C2:C8")) Is Nothing Then"
this condition is ALWAYS encountered....
Make sense ???
Or is there something I didn't understand ?
This comment was minimized by the moderator on the site
That right, did you solve this problem, and could you share for me, please?
This comment was minimized by the moderator on the site
Hey guys,
I used the following code for this problem, hope it helps someone:

Private Sub Worksheet_Calculate()
Static oldValue
If Range("MyNamedRange") <> oldValue Then
CodeHere
oldValue = Range("MyNamedRange").Value
End If
End Sub
This comment was minimized by the moderator on the site
sorry it dosn't work it works if i put data manually. but i want to work it automatically bcz my data is updating by rand calcaulate
This comment was minimized by the moderator on the site
So, what is my macro name. where can i find my macro name?
This comment was minimized by the moderator on the site
Hello, Cenk,
The macro name is the macro code you have inserted into the Excel file, and you just need to change the Macro1 in the above code to your own.
For example, i insert a code here, and the macro name is: ColorCompanyDuplicates

Sub ColorCompanyDuplicates()
'Updateby Extendoffice 20160704
Dim xRg As Range
Dim xTxt As String
Dim xCell As Range
Dim xChar As String
Dim xCellPre As Range
Dim xCIndex As Long
Dim xCol As Collection
Dim I As Long
On Error Resume Next
If ActiveWindow.RangeSelection.Count > 1 Then
xTxt = ActiveWindow.RangeSelection.AddressLocal
Else
xTxt = ActiveSheet.UsedRange.AddressLocal
End If
Set xRg = Application.InputBox("please select the data range:", "Kutools for Excel", xTxt, , , , , 8)
If xRg Is Nothing Then Exit Sub
xCIndex = 2
Set xCol = New Collection
For Each xCell In xRg
On Error Resume Next
xCol.Add xCell, xCell.Text
If Err.Number = 457 Then
xCIndex = xCIndex + 1
Set xCellPre = xCol(xCell.Text)
If xCellPre.Interior.ColorIndex = xlNone Then xCellPre.Interior.ColorIndex = xCIndex
xCell.Interior.ColorIndex = xCellPre.Interior.ColorIndex
ElseIf Err.Number = 9 Then
MsgBox "Too many duplicate companies!", vbCritical, "Kutools for Excel"
Exit Sub
End If
On Error GoTo 0
Next
End Sub
This comment was minimized by the moderator on the site
What's the point of the condition? It'll always return true...in other words: it'll run without it. This also runs whenever any cells on the sheet change value.
This comment was minimized by the moderator on the site
Thanks a lot! This really helped me out.
Is there any method to retrieve the address of the changed cell (with formula i.e Column C in this example).
Thanks.
There are no comments posted here yet
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations