Ga naar hoofdinhoud

 Hoe een macro uitvoeren op basis van de celwaarde in Excel?

Stel dat ik meerdere macrocodes in mijn werkmap heb en nu wil ik deze codes uitvoeren op basis van de celwaarde. In dit artikel zal ik ingaan op verschillende situaties die u kunt tegenkomen in uw dagelijkse werk bij het gebruik van Excel.

Voer de macro uit of activeer deze als de celwaarde groter of kleiner is dan een specifieke waarde met VBA-code

Voer de macro uit of activeer deze als de celwaarde gelijk is aan specifieke tekst met VBA-code


pijl blauw rechts bel Voer de macro uit of activeer deze als de celwaarde groter of kleiner is dan een specifieke waarde met VBA-code

Als de waarde in cel A1 bijvoorbeeld tussen 10 en 50 ligt, voert u macro1 uit en als de waarde groter is dan 50, voert u macro2 uit. Om deze klus in Excel op te lossen, past u de volgende VBA-code toe.

1. Klik met de rechtermuisknop op de bladtab waarop u de macro wilt uitvoeren op basis van een celwaarde en kies vervolgens Bekijk code vanuit het contextmenu en in het geopende Microsoft Visual Basic voor applicaties venster, kopieer en plak de volgende code in de lege module:

VBA-code: voer een macro uit als de celwaarde groter of kleiner is dan:

Private Sub Worksheet_Change(ByVal Target As Excel.Range)
    If Target.Cells.Count > 1 Then Exit Sub
    If IsNumeric(Target) And Target.Address = "$A$1" Then
        Select Case Target.Value
        Case 10 To 50: Macro1
        Case Is > 50: Macro2
        End Select
    End If
End Sub

doc voer macro uit op basis van celwaarde 1

Note: In de bovenstaande code:

A1 is de cel die de specifieke waarde bevat waarop u de macro wilt uitvoeren;

Geval 10 tot 50: Macro1: het betekent dat als de waarde tussen 10 en 50 ligt, Macro1 wordt uitgevoerd;

Geval is> 50: Macro2: dit betekent dat als de waarde groter is dan 50, Macro2 wordt uitgevoerd.

Wijzig deze macronamen en criteria naar uw behoefte, en u kunt ook meer criteria toevoegen volgens de SITUATIE scripts.

2. Sla vervolgens dit codevenster op en sluit het. Als de waarde die u invoert tussen 10 en 50 ligt in cel A1, wordt Macro1 geactiveerd, als de ingevoerde waarde groter is dan 50, wordt Macro2 uitgevoerd.


pijl blauw rechts bel Voer de macro uit of activeer deze als de celwaarde gelijk is aan specifieke tekst met VBA-code

Als u de macro wilt activeren op basis van specifieke tekst in een cel, bijvoorbeeld om macro1 uit te voeren als de tekst "Verwijderen" is ingevoerd, en macro2 uit te voeren als de tekst "Invoegen" is getypt. De volgende code kan je een plezier doen.

1. Klik met de rechtermuisknop op het blad waarop u de macro wilt uitvoeren op basis van de celwaarde en kies vervolgens Bekijk code vanuit het contextmenu en in het geopende Microsoft Visual Basic voor applicaties venster, kopieer en plak de volgende code in de lege module:

VBA-code: voer macro uit als celwaarde een specifieke tekst is

Sub worksheet_change(ByVal target As Range)
Set target = Range("A1")
If target.Value = "Delete" Then
 Call Macro1
End If
If target.Value = "Insert" Then
Call Macro2
End If
End Sub 

doc voer macro uit op basis van celwaarde 2

Note: In de bovenstaande code, "Verwijder"En"Invoegen鈥漐ijn de celteksten waarop u macro's wilt uitvoeren op basis van, en Macro1 en Macro2 zijn de macro's die u wilt uitvoeren op basis van tekst. Wijzig ze naar uw behoefte.

2. Sla vervolgens deze code op en sluit het venster. Als u nu de tekst "Verwijderen" invoert in cel A1, wordt macro1 geactiveerd, als de tekst "Invoegen" wordt ingevoerd, wordt macro2 uitgevoerd.


Gerelateerde artikelen:

Hoe een macro uitvoeren wanneer de celwaarde verandert in Excel?

Hoe macro automatisch uitvoeren voordat u afdrukt in Excel?

Hoe een macro uitvoeren op basis van de geselecteerde waarde in de vervolgkeuzelijst in Excel?

Hoe voer ik een macro uit door op hyperlinks in Excel te klikken?

Hoe voer ik een macro uit wanneer het blad is geselecteerd uit een werkmap?

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 (20)
Rated 5 out of 5 1 ratings
This comment was minimized by the moderator on the site
I need the following help
How to disable assigned macro button autometically based on cell value in excel?
This comment was minimized by the moderator on the site
Hallo,

ich br盲uchte daf眉r ein Makro,

ich kann dieses Problem nicht alleine l枚sen, w盲re super wenn mir jemand helfen k枚nnte.

In M1 steht die Zahl 5, jetzt soll der Bereich von A83 bis A683 auf die Zahl 5 gepr眉ft werden, sollte z.B. A111 die Zahl 5 enthalten, dann soll C111 + 1 ( wenn in C111 23 steht dann soll da 24 stehen). Genau so wenn in A444 eine 5 steht, dann soll C444 + 1 ( wenn C444 = 99 dann 100).
Immer wenn der Wert mit M1 眉bereinstimmt, dann soll diese Zelle in Spalte C immer wieder + 1 z盲hlen. Also 23 +1 dann 24 + 1 dann 25 +1 usw usw.


Vielleicht kann mir da jemand helfen,

Vielen Dank im vorraus.

LG Stfan
This comment was minimized by the moderator on the site
Buongiorno,
vorrei eseguire una macro quando una in una cella viene inserito un controllo "if" o se viene inserita una data e non un numero.

Ad esempio se nella cella A1 inserisco: If(B2=0;vero;falso) e la macro leggendo vero mi nasconde lo sheet2 altrimenti mi scopre la sheet 2.

Come posso fare?
Riesco a far funzionare il tutto se inserisco in A1 manualmente un valore (in questo caso vero o falso).

Grazie
This comment was minimized by the moderator on the site
Hello, Carlo,
In fact the second code in this article can solve your problem.
Note: You just need to change the text to True and False into the code, and change the code name to your own.
Sub worksheet_change(ByVal target As Range)
Set target = Range("A1")
If target.Value = "True" Then
 Call Macro1
End If
If target.Value = "False" Then
Call Macro2
End If
End Sub 


Please try again, thank you!
This comment was minimized by the moderator on the site
Buongiorno,
ho provato il listato che permette di richiamare 2 macro al cambio del valore di una cella, se quel cambio deriva da una funzione "se" non funziona, se digito i valori (vero o falso) funziona.

Come posso ovviare?
Eventualmente come potrei evitare di utilizzare il condizionale sulla cella e far verificare al listato se la cella che deve far avviare le macro 猫 compilata o meno?

grazie
This comment was minimized by the moderator on the site
Ciao skyyang. Spero tu possa aiutarmi. Ho un programmino in vba che basandosi su variazioni di prezzo di una cella (E1), collegata in DDE con una piattaforma di trading di borsa, fissa i prezzi: Massimo, Minimo, Apertura, Chiusura, per poi passare alla riga successiva in base a un intervallo temporale impostato all'apertura del foglio elettronico. L'algoritmo da me sviluppato fa si che nelle colonne: "BH" e "BI" vengano visualizzati i prezzi di acquisto e di vendita, ma solo quando soddisfatte le condizioni date, altrimenti le celle non restituiscono nessun valore. Quello di cui avrei bisogno 猫 di un avviso sonoro .wav che mi avvisi quando viene restituito un valore, di acquisto o di vendita, in modo di non dover fissare lo schermo per 14 ore al giorno. Ho provato a inserire un codice "SoundMe()" trovato on-line, ma suona ogni volta che c'猫 un nuovo massimo o un nuovo minimo nella riga in cui il programma sta aggiornando i prezzi. Pensi che il problema si possa risolvere? Grazie per l'attenzione
Stefano
This comment was minimized by the moderator on the site
Hi,
How to modify this code base on cell A1 formula calculation result
Sub Worksheet_Calculate()
Please help

Private Sub Worksheet_Change(ByVal Target As Excel.Range)
If Target.Cells.Count > 1 Then Exit Sub
If IsNumeric(Target) And Target.Address = "$A$1" Then
Select Case Target.Value
Case 10 To 50: Macro1
Case Is > 50: Macro2
End Select
End If
End Sub
Rated 5 out of 5
This comment was minimized by the moderator on the site
Hello, FG,
To make the code work in formula cells, please apply the below code:
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
    On Error Resume Next
    If Target.Cells.Count > 1 Then Exit Sub
    If IsNumeric(Target) And Target.Address = "$A$1" Then
        Select Case Target.Value
        Case 10 To 50: macro1
        Case Is > 50: macro2
        End Select
    ElseIf (Not Intersect(Range("$A$1"), Target.Dependents) Is Nothing) Then
        Set Rg = Intersect(Range("$A$1"), Target.Dependents)(1)
        Select Case Rg.Value
        Case 10 To 50: macro1
        Case Is > 50: macro2
        End Select
    End If
End Sub

Please have a try, hope it can help you!
This comment was minimized by the moderator on the site
Thank you very much! It now works :)
This comment was minimized by the moderator on the site
Hi!

Im very new to VBA and I tried this solution for an excel-sheet I have. I basically just have a long list in excel, where you in column T should type Yes or No, and I would like it to, If typed Yes, run a macro... I tried a lot of different ways of defining the range but nothing works.

Sub worksheet_change(ByVal target As Range)

Set target = Range("T:T")
If target.Value = "Yes" Then
Call Macro1
End If

End Sub

I highlights this problem(runtime error- type mismatch): If target.Value = "Yes" Then

Can anybody help?

Best regards, Isabella
This comment was minimized by the moderator on the site
Hello, Westergaard
May be the following VBA code can help you: (Note: Please change the name of the Macro1 to your own code name)
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("T:T")) Is Nothing Then
If Target.Value = "Yes" Then
      Call Macro1
    End If
    End If
End Sub

Please try, hope it can help you!
This comment was minimized by the moderator on the site
Hello, Gilles,
You should insert a Spin Button (ActiveX Control) first, and then right clcik it, choose View code, then, copy and paste the below code between the existing scripts,
Dim xWSh As Worksheet
Dim xOL As OLEObject
Dim xRg As Range
Set xWSh = Application.ActiveSheet
Set xOL = xWSh.OLEObjects("SpinButton1") 'The name of the spin button
Set xRg = xWSh.Range(xOL.LinkedCell)
If IsNumeric(xRg) And xRg.Address = "$A$1" Then
        Select Case xRg.Value
        Case 10 To 50: Macro1
        Case Is > 50: Macro2
        End Select
End If

https://www.extendoffice.com/images/stories/comments/comment-skyyang/DOC-RUN-CODE.png

Please try, hope it can help you!
This comment was minimized by the moderator on the site
Bonjour,
je souhaiterais appliquer cette macro 脿 mon code. Le probl猫me est que la cellule s'incr茅mente via une toupie et le code ne reconnais pas le changement de valeur de la cellule.
Quand je saisie la valeur manuellement cel脿 fonctionne correctement.

Merci d'avance
There are no comments posted here yet
Load More
Please leave your comments in English
Posting as Guest
Rate this post:
0   Characters
Suggested Locations