Przejdź do głównej zawartości

 Jak automatycznie uruchamiać makro, gdy zmienia się wynik formuły komórki?

Przypuśćmy, że mam listę formuł opartych na danych w kolumnie A i kolumnie B, jak pokazano na poniższym zrzucie ekranu, teraz chcę automatycznie uruchomić określony kod makra, gdy wynik formuły zmieni się wraz ze zmianą jej względnych komórek. Czy jest jakiś dobry pomysł na rozwiązanie tego zadania w programie Excel?

Automatycznie uruchamiaj makro, gdy wynik formuły komórki zmienia się wraz z kodem VBA


Automatycznie uruchamiaj makro, gdy wynik formuły komórki zmienia się wraz z kodem VBA

Poniższy kod VBA może pomóc w automatycznym wykonaniu określonego kodu, gdy zmieni się komórka wynikowa formuły, wykonaj następujące czynności:

1. Kliknij prawym przyciskiem myszy kartę arkusza, której chcesz użyć, a następnie wybierz Wyświetl kod z menu kontekstowego w otwartym pliku Microsoft Visual Basic dla aplikacji w oknie, skopiuj i wklej następujący kod do pustego modułu:

Kod VBA: automatycznie uruchamiaj makro, gdy zmieni się wynik formuły komórki:

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: W powyższym kodzie, C2: C8 to zakres komórek formuły, których chcesz użyć ,maco1 to nazwa makra, które chcesz uruchomić automatycznie. Zmień to na swoje potrzeby.

2. Następnie zapisz i zamknij to okno kodu, teraz, gdy dane w zakresie A2: B8 ulegną zmianie, powodując zmianę wyniku formuły, Twój określony kod makra zostanie wyzwolony natychmiast.

Najlepsze narzędzia biurowe

🤖 Pomocnik AI Kutools: Zrewolucjonizuj analizę danych w oparciu o: Inteligentne wykonanie   |  Wygeneruj kod  |  Twórz niestandardowe formuły  |  Analizuj dane i generuj wykresy  |  Wywołaj funkcje Kutools...
Popularne funkcje: Znajdź, wyróżnij lub zidentyfikuj duplikaty   |  Usuń puste wiersze   |  Łącz kolumny lub komórki bez utraty danych   |   Okrągły bez wzoru ...
Super wyszukiwanie: Wiele kryteriów VLookup    Wiele wartości VLookup  |   Przeglądanie pionowe na wielu arkuszach   |   Wyszukiwanie rozmyte ....
Zaawansowana lista rozwijana: Szybko twórz listę rozwijaną   |  Zależna lista rozwijana   |  Lista rozwijana wielokrotnego wyboru ....
Menedżer kolumn: Dodaj określoną liczbę kolumn  |  Przesuń kolumny  |  Przełącz stan widoczności ukrytych kolumn  |  Porównaj zakresy i kolumny ...
Polecane funkcje: Fokus siatki   |  Widok projektu   |   Duży pasek formuły    Menedżer skoroszytów i arkuszy   |  Biblioteka zasobów (Automatyczny tekst)   |  Selektor dat   |  Połącz arkusze   |  Szyfruj/odszyfruj komórki    Wysyłaj e-maile według listy   |  Super filtr   |   Specjalny filtr (filtruj pogrubienie/kursywa/przekreślenie...) ...
15 najlepszych zestawów narzędzi12 Tekst Tools (Dodaj tekst, Usuń znaki, ...)   |   50 + Wykres rodzaje (Wykres Gantta, ...)   |   40+ Praktyczne Wzory (Oblicz wiek na podstawie urodzin, ...)   |   19 Wprowadzenie Tools (Wstaw kod QR, Wstaw obraz ze ścieżki, ...)   |   12 Konwersja Tools (Liczby na słowa, Przeliczanie walut, ...)   |   7 Połącz i podziel Tools (Zaawansowane wiersze łączenia, Podział komórki, ...)   |   ... i więcej

Zwiększ swoje umiejętności Excela dzięki Kutools for Excel i doświadcz wydajności jak nigdy dotąd. Kutools dla programu Excel oferuje ponad 300 zaawansowanych funkcji zwiększających produktywność i oszczędzających czas.  Kliknij tutaj, aby uzyskać funkcję, której najbardziej potrzebujesz...

Opis


Karta Office wprowadza interfejs z zakładkami do pakietu Office i znacznie ułatwia pracę

  • Włącz edycję i czytanie na kartach w programach Word, Excel, PowerPoint, Publisher, Access, Visio i Project.
  • Otwieraj i twórz wiele dokumentów w nowych kartach tego samego okna, a nie w nowych oknach.
  • Zwiększa produktywność o 50% i redukuje setki kliknięć myszką każdego dnia!
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