Note: The other languages of the website are Google-translated. Back to English
Inloggen  \/ 
x
or
x
Registreer  \/ 
x

or

Hoe een macro te activeren of uit te voeren door op een specifieke cel in Excel te klikken?

Als u met Microsoft Excel werkt, weet u wellicht hoe u een bepaalde macro met een opdrachtknop moet uitvoeren. Maar weet u hoe u een macro moet uitvoeren door gewoon op een specifieke cel in een werkblad te klikken? Dit artikel laat je zien hoe je een macro activeert door in detail op een specifieke cel te klikken.

Activeer of voer een macro uit door op een specifieke cel met VBA-code te klikken


Activeer of voer een macro uit door op een specifieke cel met VBA-code te klikken

De volgende VBA-code kan u helpen bij het uitvoeren van een macro door op een specifieke cel in Excel te klikken. Ga als volgt te werk.

1. Op het werkblad met de cel waarop u moet klikken om een ​​macro uit te voeren, klikt u met de rechtermuisknop op de bladtab en klikt u vervolgens op Bekijk code vanuit het contextmenu.

2. In de Microsoft Visual Basic voor toepassingen -venster, kopieer en plak het onderstaande VBA-script in het codevenster.

VBA-code: activeer of voer een macro uit door op een specifieke cel te klikken

Option Explicit

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Selection.Count = 1 Then
        If Not Intersect(Target, Range("D4")) Is Nothing Then
            Call MyMacro
        End If
    End If
End Sub

Notes:

1. In de code is D4 de cel waarop u klikt om Macro uit te voeren;

2. Vervang de codenaam Mijn Macro met de macro die u in het werkblad zult uitvoeren. Zie screenshot:

3. druk de anders + Q toetsen tegelijk om het Microsoft Visual Basic voor toepassingen venster.

Vanaf nu, wanneer u op cel D4 in het huidige werkblad klikt, wordt uw opgegeven macro onmiddellijk geactiveerd.


Gerelateerde artikelen:


De beste tools voor kantoorproductiviteit

Kutools voor Excel lost de meeste van uw problemen op en verhoogt uw productiviteit met 80%

  • visfuik: Snel invoegen complexe formules, grafieken en alles wat je eerder hebt gebruikt; Versleutel cellen met wachtwoord; Maak een mailinglijst en stuur e-mails ...
  • Super Formula-balk (bewerk eenvoudig meerdere regels tekst en formule); Lay-out lezen (gemakkelijk grote aantallen cellen lezen en bewerken); Plakken in gefilterd bereik...
  • Voeg cellen / rijen / kolommen samen zonder gegevens te verliezen; Gespleten cellen inhoud; Combineer dubbele rijen / kolommen... Voorkom dubbele cellen; Vergelijk Ranges...
  • Selecteer Dupliceren of Uniek Rijen; Selecteer lege rijen (alle cellen zijn leeg); Super zoeken en fuzzy zoeken in veel werkboeken; Willekeurige selectie ...
  • Exacte kopie Meerdere cellen zonder de formuleverwijzing te wijzigen; Maak automatisch verwijzingen naar meerdere bladen; Plaats kogels, Selectievakjes en meer ...
  • Extraheer tekst, Tekst toevoegen, Verwijderen op positie, Ruimte verwijderen; Paging-subtotalen maken en afdrukken; Converteren tussen celinhoud en opmerkingen...
  • Super filter (bewaar en pas filterschema's toe op andere bladen); Geavanceerd sorteren per maand / week / dag, frequentie en meer; Speciaal filter door vet, cursief ...
  • Combineer werkmappen en werkbladen; Tabellen samenvoegen op basis van sleutelkolommen; Gegevens splitsen in meerdere bladen; Batch Converteer xls, xlsx en PDF...
  • Meer dan 300 krachtige functies. Ondersteunt Office / Excel 2007-2019 en 365. Ondersteunt alle talen. Eenvoudig te implementeren in uw onderneming of organisatie. Gratis proefperiode van 30 dagen met volledige functies. 60 dagen geld-terug-garantie.
kte tabblad 201905

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 elke dag honderden muisklikken voor u!
officetab onderkant
Say something here...
symbols left.
You are guest
or post as a guest, but your post won't be published automatically.
Loading comment... The comment will be refreshed after 00:00.
  • To post as a guest, your comment is unpublished.
    Houra · 1 years ago
    Hello, Thank you for your interesting subject. What if for example I want to to click on D4 in Sheet1 and see the result of Macro in Sheet2!F3.
  • To post as a guest, your comment is unpublished.
    Justin · 1 years ago
    hello, I was trying to use this code to Run a paste macro but can't seem to figure out how to paste the macro properly.

    here is my original Marco

    Sub Paste()
    '
    ' Paste Macro
    '

    '
    Range("B34").Select
    ActiveSheet.PasteSpecial Format:="HTML", Link:=False, DisplayAsIcon:= _
    False, NoHTMLFormatting:=True
    End Sub


    any help is appreciated
  • To post as a guest, your comment is unpublished.
    M. Symonds · 2 years ago
    Thanks for this code. Is it possible to get this to work by clicking on a cell that is merged with others?
    • To post as a guest, your comment is unpublished.
      crystal · 2 years ago
      Hi M.Symonds,
      The code in this article can do you a favor: https://www.extendoffice.com/documents/excel/4354-excel-click-on-cell-to-run-macro.html
      Thank you for your comment.
  • To post as a guest, your comment is unpublished.
    Shahrokh · 2 years ago
    Thanks, but how about several cells for clicking for running several macros.
    • To post as a guest, your comment is unpublished.
      crystal · 2 years ago
      Hi,
      Try the below VBA code.

      Private Sub Worksheet_SelectionChange(ByVal Target As Range)
      Dim xRgArr As Variant
      Dim xFunArr As Variant
      Dim xFNum As Integer
      Dim xStr As String
      Dim xRg As Range
      xRgArr = Array("A1", "D1", "C1") 'Cells used to trigger macro
      xFunArr = Array("Code name1", "Code name2", "Code name3") 'The corresponding code names
      If Selection.Count = 1 Then
      For xFNum = 0 To UBound(xRgArr)
      Set xRg = ActiveSheet.Range(xRgArr(xFNum))
      If Not Intersect(Target, xRg) Is Nothing Then
      xStr = xFunArr(xFNum)
      Application.Run xStr
      End If
      Next
      End If
      End Sub
  • To post as a guest, your comment is unpublished.
    adamsnow147 · 2 years ago
    Why not just use some newer version it will be more easy
    https://games.lol/racing/
  • To post as a guest, your comment is unpublished.
    Roger · 2 years ago
    Excel 2002 (XP): If a workbook is opened by selecting "File" and pressing "Shift" it disables macros in that sheet, the problem is that if I select "Tools > Macro > Macros..." I can run the macro anyway, how to solve?
    • To post as a guest, your comment is unpublished.
      crystal · 2 years ago
      Hi Roger,
      We haven't tested the code in Excel 2002(XP). Why not use the newer version of Microsoft Office? It will be more easy for your work.
      • To post as a guest, your comment is unpublished.
        Roger · 2 years ago
        Same result in 2010.
  • To post as a guest, your comment is unpublished.
    James Clements · 2 years ago
    My full code in this macro is:


    Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    Dim val As String
    REM val = Range("A2").Value

    If Selection.Count = 1 Then
    If Not Intersect(Target, Range("D24")) Is Nothing Then
    REM Call MyMacro
    val = Range("D24").Value
    Range("B27").Value = val
    End If
    End If
    End Sub
    • To post as a guest, your comment is unpublished.
      goalken · 2 years ago
      This topic is very interesting and I am interested but do not know where to find, thankfully you create this topic, hope everyone will help me http://run-3.online
  • To post as a guest, your comment is unpublished.
    James Clements · 2 years ago
    i'm using OpenOffice and have right clicked on sheet-tab and selected events and then selected this macro from MyMacros.. However I get and error on the following line: If Selection.Count = 1 Then >>> "Basic runtime error, variable not defined...
    • To post as a guest, your comment is unpublished.
      crystal · 2 years ago
      Hi James,
      The code only works for Microsoft Office Excel. Thank you for your comment.
  • To post as a guest, your comment is unpublished.
    Alber · 2 years ago
    Thanks but what about merged cells?
    • To post as a guest, your comment is unpublished.
      crystal · 2 years ago
      Hi Alber,
      The code does not work for merged cells.
  • To post as a guest, your comment is unpublished.
    Phil_11 · 3 years ago
    Hi.

    I have this working well, but would like to add a condition to running the macro. I only want to run the macro if the cell alongside the cell I click into contains a certain value.
    e.g. When I click on cell F6, I want the macro to run if cell E6 contains "x", but if cell E6 is blank, the macro must not run.
    Hope that makes sense.
    Thanks

    here is my original code without the condition:


    Option Explicit

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Selection.Count = 1 Then
    If Not Intersect(Target, Range("F6:F18")) Is Nothing Then
    Call datePick
    End If
    End If
    End Sub
    • To post as a guest, your comment is unpublished.
      crystal · 2 years ago
      Hi,
      The following VBA code can help you solve the problem. Please have a try and thank you for your comment.

      Private Sub Worksheet_SelectionChange(ByVal Target As Range)
      Dim xRg As Range
      If Not Intersect(Target, Range("F6:F18")) Is Nothing Then
      Set xRg = ActiveSheet.Cells(Target.Row, Target.Column - 1)
      If (xRg.Value = "") Or (xRg.Value <> "X") Then Exit Sub
      Call datepick
      End If
      End Sub
  • To post as a guest, your comment is unpublished.
    Medical Coder · 3 years ago
    Great. Its working well... Thank you....
  • To post as a guest, your comment is unpublished.
    Wayne · 4 years ago
    This worked perfectly and will save me a load of time - thank you for sharing your knowledge - much appreciated!
    • To post as a guest, your comment is unpublished.
      crystal · 4 years ago
      I’m glad I could help.
  • To post as a guest, your comment is unpublished.
    Simon Jones · 4 years ago
    If you want to have multiple macros, cells running different macros on the same page - is that possible?
    • To post as a guest, your comment is unpublished.
      crystal · 4 years ago
      Dear Simon,
      The below VBA script can help you to run different macros by clicking on cells on the same page.

      Private Sub Worksheet_SelectionChange(ByVal Target As Range)
      If Target.Count = 1 Then
      If Not Intersect(Target, Range("D4")) Is Nothing Then Call MyMacro1
      If Not Intersect(Target, Range("D8")) Is Nothing Then Call MyMacro2
      If Not Intersect(Target, Range("D10")) Is Nothing Then Call MyMacro3
      End If
      End Sub

      Please add line "If Not Intersect(Target, Range("D10")) Is Nothing Then Call MyMacro" to run more macro by clicking cell. And change the cell and macro names in the code based on your needs.
      • To post as a guest, your comment is unpublished.
        Camila · 2 years ago
        It isn't working on my Excel. The code is correct?
        • To post as a guest, your comment is unpublished.
          crystal · 2 years ago
          Hi Camila,
          Sorry for the inconvenience. Try the below VBA code.

          Private Sub Worksheet_SelectionChange(ByVal Target As Range)
          Dim xRgArr As Variant
          Dim xFunArr As Variant
          Dim xFNum As Integer
          Dim xStr As String
          Dim xRg As Range
          xRgArr = Array("A1", "D1", "C1") 'Cells used to trigger macro
          xFunArr = Array("Code name1", "Code name2", "Code name3") 'The corresponding code names
          If Selection.Count = 1 Then
          For xFNum = 0 To UBound(xRgArr)
          Set xRg = ActiveSheet.Range(xRgArr(xFNum))
          If Not Intersect(Target, xRg) Is Nothing Then
          xStr = xFunArr(xFNum)
          Application.Run xStr
          End If
          Next
          End If
          End Sub
  • To post as a guest, your comment is unpublished.
    Matt · 4 years ago
    Trigger Or Run A Macro By Clicking A Specific Cell With VBA Code, couldn't get this to work. I tried it many different way, but it never acheived what was promised.
    • To post as a guest, your comment is unpublished.
      Peter · 4 years ago
      This code will only work if you place it in the "ThisWorkbook" module. It does not work on a general module.
      workbookname > Microsft Excel Objects > ThisWorkbook.
      • To post as a guest, your comment is unpublished.
        PritishS · 3 years ago
        This code will work on worksheet code module. You need to right click on sheet tab and click on 'View Code'. This will open code module for that worksheet only. Then paste the mentioned code.