ไปยังเนื้อหาหลัก

 วิธีเรียกใช้แมโครตามค่าเซลล์ใน Excel

สมมติว่าฉันมีรหัสมาโครหลายรหัสในสมุดงานของฉันและตอนนี้ฉันต้องการเรียกใช้รหัสเหล่านี้ตามค่าของเซลล์ บทความนี้ฉันจะพูดถึงสถานการณ์ต่างๆที่คุณอาจประสบในการทำงานประจำวันเมื่อใช้ Excel

เรียกใช้หรือทริกเกอร์มาโครหากค่าของเซลล์มากกว่าหรือน้อยกว่าค่าเฉพาะด้วยรหัส VBA

เรียกใช้หรือทริกเกอร์มาโครหากค่าของเซลล์เท่ากับข้อความที่ระบุด้วยรหัส VBA


ลูกศรสีฟ้าฟองขวา เรียกใช้หรือทริกเกอร์มาโครหากค่าของเซลล์มากกว่าหรือน้อยกว่าค่าเฉพาะด้วยรหัส VBA

ตัวอย่างเช่นถ้าค่าในเซลล์ A1 อยู่ระหว่าง 10 ถึง 50 ให้เรียกใช้ macro1 และถ้าค่ามากกว่า 50 ให้เรียกใช้ macro2 หากต้องการแก้ไขงานนี้ใน Excel โปรดใช้รหัส VBA ต่อไปนี้

1. คลิกขวาที่แท็บแผ่นงานที่คุณต้องการเรียกใช้แมโครตามค่าของเซลล์จากนั้นเลือก ดูรหัส จากเมนูบริบทและในเปิด Microsoft Visual Basic สำหรับแอปพลิเคชัน หน้าต่างคัดลอกและวางรหัสต่อไปนี้ลงในโมดูลเปล่า:

รหัส VBA: เรียกใช้แมโครหากค่าของเซลล์มากกว่าหรือน้อยกว่า:

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 เรียกใช้แมโครตามค่าของเซลล์ 1

หมายเหตุ: ในโค้ดด้านบน:

A1 คือเซลล์ที่มีค่าเฉพาะที่คุณต้องการเรียกใช้แมโครตาม;

กรณีที่ 10 ถึง 50: มาโคร 1: หมายความว่าถ้าค่าอยู่ระหว่าง 10 ถึง 50 ให้เรียกใช้ Macro1

กรณีคือ> 50: มาโคร 2: หมายความว่าถ้าค่ามากกว่า 50 ให้เรียกใช้ Macro2

โปรดเปลี่ยนชื่อมาโครและเกณฑ์เหล่านี้ตามความต้องการของคุณและคุณยังสามารถเพิ่มเกณฑ์เพิ่มเติมได้ตาม กรณี ต้นฉบับ

2. จากนั้นบันทึกและปิดหน้าต่างรหัสนี้ตอนนี้เมื่อค่าที่คุณป้อนอยู่ระหว่าง 10 ถึง 50 ในเซลล์ A1 ระบบจะเรียกใช้ Macro1 หากค่าที่ป้อนมากกว่า 50 Macro2 จะดำเนินการ


ลูกศรสีฟ้าฟองขวา เรียกใช้หรือทริกเกอร์มาโครหากค่าของเซลล์เท่ากับข้อความที่ระบุด้วยรหัส VBA

หากคุณต้องการเรียกใช้มาโครตามข้อความที่ระบุในเซลล์ตัวอย่างเช่นเพื่อเรียกใช้ macro1 หากป้อนข้อความ "ลบ" และเรียกใช้ macro2 หากพิมพ์ข้อความ "แทรก" รหัสต่อไปนี้สามารถช่วยคุณได้

1. คลิกขวาที่แผ่นงานที่คุณต้องการเรียกใช้แมโครตามค่าของเซลล์จากนั้นเลือก ดูรหัส จากเมนูบริบทและในเปิด Microsoft Visual Basic สำหรับแอปพลิเคชัน หน้าต่างคัดลอกและวางรหัสต่อไปนี้ลงในโมดูลเปล่า:

รหัส VBA: เรียกใช้แมโครหากค่าของเซลล์เป็นข้อความเฉพาะ

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 เรียกใช้แมโครตามค่าของเซลล์ 2

หมายเหตุ: ในโค้ดด้านบน“ลบ"และ"สิ่งที่ใส่เข้าไป” คือข้อความในเซลล์ที่คุณต้องการเรียกใช้มาโครตามและ Macro1 และ Macro2 คือมาโครที่คุณต้องการเรียกใช้ตามข้อความ โปรดเปลี่ยนตามความต้องการของคุณ

2. จากนั้นบันทึกรหัสนี้และปิดหน้าต่างตอนนี้เมื่อคุณป้อนข้อความ“ ลบ” ในเซลล์ A1 มาโคร1จะถูกเรียกใช้หากป้อนข้อความ“ แทรก” มาโคร2จะถูกเรียกใช้


บทความที่เกี่ยวข้อง:

วิธีเรียกใช้แมโครเมื่อค่าของเซลล์เปลี่ยนแปลงใน Excel

วิธีเรียกใช้มาโครโดยอัตโนมัติก่อนพิมพ์ใน Excel

วิธีเรียกใช้แมโครตามค่าที่เลือกจากรายการแบบหล่นลงใน Excel

วิธีเรียกใช้แมโครโดยคลิกไฮเปอร์ลิงก์ใน Excel

วิธีเรียกใช้แมโครเมื่อแผ่นงานถูกเลือกจากสมุดงาน

สุดยอดเครื่องมือเพิ่มผลผลิตในสำนักงาน

🤖 Kutools AI ผู้ช่วย: ปฏิวัติการวิเคราะห์ข้อมูลโดยยึดตาม: การดำเนินการที่ชาญฉลาด   |  สร้างรหัส  |  สร้างสูตรที่กำหนดเอง  |  วิเคราะห์ข้อมูลและสร้างแผนภูมิ  |  เรียกใช้ฟังก์ชัน Kutools...
คุณสมบัติยอดนิยม: ค้นหา เน้น หรือระบุรายการที่ซ้ำกัน   |  ลบแถวว่าง   |  รวมคอลัมน์หรือเซลล์โดยไม่สูญเสียข้อมูล   |   รอบโดยไม่มีสูตร ...
การค้นหาขั้นสูง: VLookup หลายเกณฑ์    VLookup หลายค่า  |   VLookup ข้ามหลายแผ่น   |   การค้นหาที่ไม่ชัดเจน ....
รายการแบบเลื่อนลงขั้นสูง: สร้างรายการแบบหล่นลงอย่างรวดเร็ว   |  รายการแบบหล่นลงขึ้นอยู่กับ   |  เลือกหลายรายการแบบหล่นลง ....
ผู้จัดการคอลัมน์: เพิ่มจำนวนคอลัมน์เฉพาะ  |  ย้ายคอลัมน์  |  สลับสถานะการมองเห็นของคอลัมน์ที่ซ่อนอยู่  |  เปรียบเทียบช่วงและคอลัมน์ ...
คุณสมบัติเด่น: กริดโฟกัส   |  มุมมองการออกแบบ   |   บาร์สูตรใหญ่    สมุดงานและตัวจัดการชีต   |  ห้องสมุดทรัพยากร (ข้อความอัตโนมัติ)   |  เลือกวันที่   |  รวมแผ่นงาน   |  เข้ารหัส/ถอดรหัสเซลล์    ส่งอีเมลตามรายการ   |  ซุปเปอร์ฟิลเตอร์   |   ตัวกรองพิเศษ (กรองตัวหนา/ตัวเอียง/ขีดทับ...) ...
ชุดเครื่องมือ 15 อันดับแรก12 ข้อความ เครื่องมือ (เพิ่มข้อความ, ลบอักขระ, ... )   |   50 + แผนภูมิ ประเภท (แผนภูมิ Gantt, ... )   |   40+ ใช้งานได้จริง สูตร (คำนวณอายุตามวันเกิด, ... )   |   19 การแทรก เครื่องมือ (ใส่ QR Code, แทรกรูปภาพจากเส้นทาง, ... )   |   12 การแปลง เครื่องมือ (ตัวเลขเป็นคำ, การแปลงสกุลเงิน, ... )   |   7 ผสานและแยก เครื่องมือ (แถวรวมขั้นสูง, แยกเซลล์, ... )   |   ... และอื่น ๆ

เพิ่มพูนทักษะ Excel ของคุณด้วย Kutools สำหรับ Excel และสัมผัสประสิทธิภาพอย่างที่ไม่เคยมีมาก่อน Kutools สำหรับ Excel เสนอคุณสมบัติขั้นสูงมากกว่า 300 รายการเพื่อเพิ่มประสิทธิภาพและประหยัดเวลา  คลิกที่นี่เพื่อรับคุณสมบัติที่คุณต้องการมากที่สุด...

รายละเอียด


แท็บ Office นำอินเทอร์เฟซแบบแท็บมาที่ Office และทำให้งานของคุณง่ายขึ้นมาก

  • เปิดใช้งานการแก้ไขและอ่านแบบแท็บใน Word, Excel, PowerPoint, ผู้จัดพิมพ์, Access, Visio และโครงการ
  • เปิดและสร้างเอกสารหลายรายการในแท็บใหม่ของหน้าต่างเดียวกันแทนที่จะเป็นในหน้าต่างใหม่
  • เพิ่มประสิทธิภาพการทำงานของคุณ 50% และลดการคลิกเมาส์หลายร้อยครั้งให้คุณทุกวัน!
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