메인 컨텐츠로 가기

 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

셀 값 1을 기반으로하는 문서 실행 매크로

주의 사항: 위 코드에서 :

A1 매크로를 실행할 특정 값이 포함 된 셀입니다.

사례 10 ~ 50 : Macro1: 값이 10에서 50 사이이면 Macro1을 실행합니다.

사례가> 50 : Macro2: 값이 50보다 크면 Macro2를 실행합니다.

이러한 매크로 이름과 기준을 필요에 따라 변경하고 다음의 기준을 더 추가 할 수도 있습니다. 케이스 스크립트.

2. 그런 다음이 코드 창을 저장하고 닫으십시오. 이제 입력 한 값이 A10 셀에 50에서 1 사이이면 Macro1이 트리거되고 입력 된 값이 50보다 크면 Macro2가 실행됩니다.


화살표 블루 오른쪽 거품 셀 값이 VBA 코드가있는 특정 텍스트와 같은 경우 매크로 실행 또는 트리거

예를 들어 셀의 특정 텍스트를 기반으로 매크로를 트리거하려면 "삭제"텍스트를 입력하면 macro1을 실행하고 "Insert"텍스트를 입력하면 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 

셀 값 2을 기반으로하는 문서 실행 매크로

주의 사항: 위 코드에서“."및"끼워 넣다”는 매크로를 실행하려는 셀 텍스트입니다. Macro1 Macro2 텍스트를 기반으로 실행하려는 매크로입니다. 필요에 따라 변경하십시오.

2. 그런 다음이 코드를 저장하고 창을 닫으십시오. 이제 A1 셀에 "Delete"텍스트를 입력하면 macro1이 트리거되고 "Insert"텍스트를 입력하면 macro2가 실행됩니다.


관련 기사 :

Excel에서 셀 값이 변경 될 때 매크로를 실행하는 방법은 무엇입니까?

Excel에서 인쇄하기 전에 매크로를 자동으로 실행하는 방법은 무엇입니까?

Excel의 드롭 다운 목록에서 선택한 값을 기반으로 매크로를 실행하는 방법은 무엇입니까?

Excel에서 하이퍼 링크를 클릭하여 매크로를 실행하는 방법은 무엇입니까?

통합 문서에서 시트를 선택할 때 매크로를 실행하는 방법은 무엇입니까?

최고의 사무 생산성 도구

🤖 Kutools AI 보좌관: 다음을 기반으로 데이터 분석을 혁신합니다. 지능형 실행   |  코드 생성  |  사용자 정의 수식 만들기  |  데이터 분석 및 차트 생성  |  Kutools 기능 호출...
인기 기능: 중복 항목 찾기, 강조 표시 또는 식별   |  빈 행 삭제   |  데이터 손실 없이 열이나 셀 결합   |   수식없이 반올림 ...
슈퍼 조회: 다중 기준 VLookup    다중 값 VLookup  |   여러 시트에 걸친 VLookup   |   퍼지 조회 ....
고급 드롭다운 목록: 드롭다운 목록을 빠르게 생성   |  종속 드롭다운 목록   |  다중 선택 드롭 다운 목록 ....
열 관리자: 특정 개수의 열 추가  |  열 이동  |  Toggle 숨겨진 열의 가시성 상태  |  범위 및 열 비교 ...
특색 지어진 특징: 그리드 포커스   |  디자인보기   |   큰 수식 바    통합 문서 및 시트 관리자   |  리소스 라이브러리 (자동 텍스트)   |  날짜 선택기   |  워크 시트 결합   |  셀 암호화/해독    목록으로 이메일 보내기   |  슈퍼 필터   |   특수 필터 (굵게/기울임꼴/취소선 필터링...) ...
상위 15개 도구 세트12 본문 도구 (텍스트 추가, 문자 제거,...)   |   50+ 거래차트 유형 (Gantt 차트,...)   |   40+ 실용 방식 (생일을 기준으로 나이 계산,...)   |   19 삽입 도구 (QR 코드 삽입, 경로에서 그림 삽입,...)   |   12 매출 상승 도구 (숫자를 단어로, 환율,...)   |   7 병합 및 분할 도구 (고급 결합 행, 셀 분할,...)   |   ... 그리고 더

Excel용 Kutools로 Excel 기술을 강화하고 이전과는 전혀 다른 효율성을 경험해 보세요. Excel용 Kutools는 생산성을 높이고 시간을 절약하기 위해 300개 이상의 고급 기능을 제공합니다.  가장 필요한 기능을 얻으려면 여기를 클릭하십시오...

상품 설명


Office Tab은 Office에 탭 인터페이스를 제공하여 작업을 훨씬 쉽게 만듭니다.

  • Word, Excel, PowerPoint에서 탭 편집 및 읽기 사용, Publisher, Access, Visio 및 Project.
  • 새 창이 아닌 동일한 창의 새 탭에서 여러 문서를 열고 만듭니다.
  • 생산성을 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