Skip to main content

 Conas macra a reáchtáil bunaithe ar luach cille in Excel?

Ag ceapadh, tá macra-chóid iolracha agam i mo leabhar oibre, agus anois, ba mhaith liom na cóid seo a reáchtáil bunaithe ar luach na cille. An t-alt seo, labhróidh mé faoi roinnt cásanna a d’fhéadfadh a bheith agat i do chuid oibre laethúla agus tú ag úsáid Excel.

Rith nó spreag macra má tá luach cille níos mó nó níos lú ná luach sonrach le cód VBA

Rith nó spreag macra más ionann luach cille agus téacs sonrach le cód VBA


mboilgeog cheart gorm saighead Rith nó spreag macra má tá luach cille níos mó nó níos lú ná luach sonrach le cód VBA

Mar shampla, má tá an luach i gcill A1 idir 10 agus 50, rith macra1, agus má tá an luach níos mó ná 50, rith macra2. Chun an post seo a réiteach in Excel, cuir an cód VBA seo a leanas i bhfeidhm.

1. Cliceáil ar dheis ar an táb bileog a theastaíonn uait an macra a fhorghníomhú bunaithe ar luach cille, agus ansin roghnaigh Féach an cód ón roghchlár comhthéacs, agus san oscailt Microsoft Visual Basic d’iarratais an cód seo a leanas a fhuinneogú, a chóipeáil agus a ghreamú isteach sa Mhodúl bán:

Cód VBA: Rith macra má tá luach cille níos mó nó níos lú ná:

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

macra reáchtáil doc bunaithe ar luach cille 1

nótaí: Sa chód thuas:

A1 an cill í ina bhfuil an luach sonrach ar mhaith leat an macra a rith bunaithe uirthi;

Cás 10 go 50: Macra1: ciallaíonn sé má tá an luach idir 10 agus 50, rith Macro1;

Cás Is> 50: Macro2: ciallaíonn sé má tá an luach níos mó ná 50, rith Macro2.

Athraigh macra-ainmneacha agus critéir tráchtais le do riachtanas, agus is féidir leat níos mó critéar a chur leis tar éis an Cás script.

2. Ansin sábháil agus dún an fhuinneog cód seo, anois, nuair a bheidh an luach a iontrálann tú idir 10 agus 50 i gcill A1, spreagfar Macro1, más mó an luach a iontráladh ná 50, déantar Macro2 a fhorghníomhú.


mboilgeog cheart gorm saighead Rith nó spreag macra más ionann luach cille agus téacs sonrach le cód VBA

Más mian leat an macra a spreagadh bunaithe ar théacs ar leith i gcill, mar shampla, chun an macra1 a rith má iontráiltear an téacs “Scrios”, agus rith macra2 má chlóscríobtar téacs “Ionsáigh”. Féadann an cód seo a leanas fabhar a thabhairt duit.

1. Cliceáil ar dheis ar an mbileog a theastaíonn uait an macra a fhorghníomhú bunaithe ar luach na cille, agus ansin roghnaigh Féach an cód ón roghchlár comhthéacs, agus san oscailt Microsoft Visual Basic d’iarratais an cód seo a leanas a fhuinneogú, a chóipeáil agus a ghreamú isteach sa Mhodúl bán:

Cód VBA: Rith an macra más téacs sonrach é luach cille

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 

macra reáchtáil doc bunaithe ar luach cille 2

nótaí: Sa chód thuas, “Scrios"Agus"Ionsáigh"Is iad na téacsanna cille ar mhaith leat macraí a reáchtáil bunaithe orthu, agus Macra1 agus Macra2 an bhfuil na macraí is mian leat a fhorghníomhú bunaithe ar théacs. Athraigh iad le do riachtanas le do thoil.

2. Ansin sábháil an cód seo agus dún an fhuinneog, anois, nuair a iontrálann tú an téacs “Scrios” i gcill A1, spreagtar macra1, má iontráiltear an téacs “Ionsáigh”, forghníomhófar macra2.


Earraí gaolmhara:

Conas macra a rith nuair a athraíonn luach cille in Excel?

Conas macra a reáchtáil go huathoibríoch sula ndéantar é a phriontáil in Excel?

Conas macra a reáchtáil bunaithe ar luach a roghnaíodh ón liosta anuas in Excel?

Conas macra a reáchtáil trí hipearnasc a chliceáil in Excel?

Conas macra a reáchtáil nuair a roghnaítear bileog ó leabhar oibre?

Uirlisí Táirgiúlachta Oifige is Fearr

🤖 Kutools AI Aide: anailís sonraí a réabhlóidiú bunaithe ar: Forghníomhú Chliste   |  Gin Cód  |  Cruthaigh Foirmlí Saincheaptha  |  Anailís a dhéanamh ar Sonraí agus Cairteacha a Ghin  |  Feidhmeanna Kutools a agairt...
Gnéithe Coitianta: Faigh, Aibhsigh nó Aithnigh Dúblaigh   |  Scrios Sraitheanna Bána   |  Comhcheangail Colúin nó Cealla gan Sonraí a Chailleadh   |   Babhta gan Foirmle ...
Cuardaigh Super: Ilchritéir VLookup    VLookup Illuachanna  |   VLookup Trasna Ilbhileoga   |   Amharc doiléir ....
Liosta anuas Casta: Go tapa Cruthaigh Liosta Anuas   |  Liosta anuas Cleithiúnach   |  Liosta Buail Isteach Ilroghnacha ....
Bainisteoir Colún: Cuir Líon Sonrach Colún leis  |  Colúin Bog  |  Scoránaigh Stádas Infheictheachta na gColún Ceilte  |  Déan comparáid idir Raonta & Colúin ...
Gnéithe Réadmhaoin: Fócas Eangaí   |  Amharc Dearaidh   |   Barra Mór na Foirmle    Leabhar Oibre & Bainisteoir Bileog   |  Leabharlann Acmhainní (Uaththéacs)   |  Piocálaí Dáta   |  Comhcheangail Bileoga Oibre   |  Criptigh/Díchriptigh Cealla    Seol Ríomhphost trí Liosta   |  Scagaire Super   |   Scagaire Speisialta (scagaire trom/iodálach/stailc tríd...) ...
Barr 15 Uirlisí12 Téacs uirlisí (Cuir Téacs, Bain Carachtair,...)   |   50 + Cairt cineálacha (Cairt Gantt,...)   |   40+ Praiticiúil Foirmlí (Ríomh aois bunaithe ar lá breithe,...)   |   19 Insertion uirlisí (Cuir isteach Cód QR, Ionsáigh Pictiúr ón gCosán,...)   |   12 Tiontú uirlisí (Uimhreacha le Focail, Comhshó Airgeadra,...)   |   7 Cumaisc & Scoilt uirlisí (Sraitheanna Comhcheangail Casta, Cealla Scoilt,...)   |   ... agus eile

Supercharge Do Scileanna Excel le Kutools le haghaidh Excel, agus Éifeachtúlacht Taithí Cosúil Ná Roimhe. Kutools le haghaidh Excel Tairiscintí Níos mó ná 300 Ardghnéithe chun Táirgiúlacht a Treisiú agus Sábháil Am.  Cliceáil anseo chun an ghné is mó a theastaíonn uait a fháil ...

Tuairisc


Tugann Tab Oifige comhéadan Tabbed chuig Office, agus Déan Do Obair i bhfad Níos Éasca

  • Cumasaigh eagarthóireacht agus léamh tabbed i Word, Excel, PowerPoint, Foilsitheoir, Rochtain, Visio agus Tionscadal.
  • Oscail agus cruthaigh cáipéisí iolracha i gcluaisíní nua den fhuinneog chéanna, seachas i bhfuinneoga nua.
  • Méadaíonn do tháirgiúlacht 50%, agus laghdaíonn sé na céadta cad a tharlaíonn nuair luch duit gach lá!
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