Hoppa till huvudinnehåll

Hur rensar du specificerat cellinnehåll om värdet för en annan cell ändras i Excel?

Om du antar att du vill rensa ett antal specificerade cellinnehåll om värdet på en annan cell ändras, hur kan du göra? Det här inlägget visar dig en metod för att lösa detta problem.

Rensa specificerat cellinnehåll om värdet för en annan cell ändras med VBA-kod


Rensa specificerat cellinnehåll om värdet för en annan cell ändras med VBA-kod

Som nedan visas skärmdump, när värdet i cell A2 ändras kommer innehållet i cell C1: C3 att rensas automatiskt. Gör så här.

1. I kalkylbladet kommer du att rensa cellinnehåll baserat på en annan celländring, högerklicka på arkfliken och välj Visa kod från snabbmenyn. Se skärmdump:

2. I öppningen Microsoft Visual Basic för applikationer fönster, kopiera och klistra in nedanför VBA-koden i kodfönstret.

VBA-kod: Rensa specificerat cellinnehåll om värdet för en annan cell ändras

Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("A2")) Is Nothing Then
        Range("C1:C3").ClearContents
    End If
End Sub

Anmärkningar: I koden är B2 den cell du rensar cellinnehåll baserat på, och C1: C3 är det intervall du rensar innehåll från. Ändra dem efter behov.

3. tryck på andra + Q för att stänga Microsoft Visual Basic för applikationer fönster.

Då kan du se innehåll i intervall C1: C3 rensas automatiskt när värdet i cell A2 ändras enligt nedanstående skärmdump.


Relaterade artiklar:

Bästa kontorsproduktivitetsverktyg

🤖 Kutools AI Aide: Revolutionera dataanalys baserat på: Intelligent utförande   |  Generera kod  |  Skapa anpassade formler  |  Analysera data och generera diagram  |  Anropa Kutools funktioner.
Populära funktioner: Hitta, markera eller identifiera dubbletter   |  Ta bort tomma rader   |  Kombinera kolumner eller celler utan att förlora data   |   Rund utan formel ...
Superuppslag: Flera kriterier VLookup    VLookup med flera värden  |   VSök över flera ark   |   Fuzzy Lookup ....
Avancerad rullgardinslista: Skapa snabbt en rullgardinslista   |  Beroende rullgardinslista   |  Flervals-rullgardinslista ....
Kolumnhanterare: Lägg till ett specifikt antal kolumner  |  Flytta kolumner  |  Växla synlighetsstatus för dolda kolumner  |  Jämför intervall och kolumner ...
Utvalda funktioner: Rutnätsfokus   |  Designvy   |   Stor formelbar    Arbetsbok & Bladhanterare   |  Resursbibliotek (Automatisk text)   |  Datumväljare   |  Kombinera arbetsblad   |  Kryptera/Dekryptera celler    Skicka e-postmeddelanden efter lista   |  Superfilter   |   Specialfilter (filtrera fet/kursiv/genomstruken...) ...
Topp 15 verktygssatser12 text verktyg (lägga till text, Ta bort tecken, ...)   |   50+ Diagram Typer (Gantt Chart, ...)   |   40+ Praktiskt Formler (Beräkna ålder baserat på födelsedag, ...)   |   19 Införande verktyg (Infoga QR-kod, Infoga bild från sökväg, ...)   |   12 Konvertering verktyg (Siffror till ord, Valutaväxling, ...)   |   7 Slå ihop och dela verktyg (Avancerade kombinera rader, Dela celler, ...)   |   ... och mer

Uppgradera dina Excel-färdigheter med Kutools för Excel och upplev effektivitet som aldrig förr. Kutools för Excel erbjuder över 300 avancerade funktioner för att öka produktiviteten och spara tid.  Klicka här för att få den funktion du behöver mest...

Beskrivning


Fliken Office ger ett flikgränssnitt till Office och gör ditt arbete mycket enklare

  • Aktivera flikredigering och läsning i Word, Excel, PowerPoint, Publisher, Access, Visio och Project.
  • Öppna och skapa flera dokument i nya flikar i samma fönster, snarare än i nya fönster.
  • Ökar din produktivitet med 50 % och minskar hundratals musklick för dig varje dag!
Comments (38)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
Hallo,

Dankeschön für die Hilfe.


LG Stefan
This comment was minimized by the moderator on the site
Hallo,
ich möchte das Makro bitte so erweitern, wenn ich in B2 klicke, das nur C2 gelöscht wird, wenn ich in B3 klicke dann soll nur C3 gelöscht werden usw. bis B100 dann soll nur C100 gelöscht werden.
Und das soll auch wechelseitig funktionieren.
Wenn ich in C2 klicke dann soll nur B2 gelöscht werden usw.

Vielen Dank im Vorraus

LG Stefan




Private Sub Worksheet_Change(ByVal Target As Range)
'Updated by Extendoffice 20220721
If Target.Cells.Count > 1 Then Exit Sub
If (Not Intersect(Target, Range("B2")) Is Nothing) And (Target.Value = "Yes") Then
Range("B3").ClearContents
Else
If (Not Intersect(Target, Range("B3")) Is Nothing) And (Target.Value = "Yes") Then
Range("B2").ClearContents
End If
End If
End Sub
This comment was minimized by the moderator on the site
Hi Stefan,

The following VBA can acheive: when the value of column A is changed, the corresponding cell in column C of the same row will be cleared.
Private Sub Worksheet_Change(ByVal Target As Range)
'Updated by Extendoffice 20221013
    Dim xRight As Range
    Dim KeyCells As Range
    Set KeyCells = Range("A:A")
    Set xRight = Target.Offset(0, 3)
    If Not Application.Intersect(KeyCells, Range(Target.Address)) Is Nothing Then
        xRight.ClearContents
    End If
End Sub
This comment was minimized by the moderator on the site
Hola, estoy trabajando una base de datos en OFFICE ONLINE a traves de ONEDRIVE, quiero que al PONER "CANCELADO" o "NOSHOW" elimine el contenido de la fila seleccionada.
This comment was minimized by the moderator on the site
Hi Angel,
The VBA code does not work in Office Online. Sorry for the inconvenience.
This comment was minimized by the moderator on the site
Hello,
The code below works as advertised but, the following problems occurs:

Firstly, when resizing the targeted table, all the table data is cleared AND, all but column 1 headers are re-labelled to "Column1, Column2, etc. AND the workbook autosave itslef and kills the undo.

Secondly, when deleting any table row, I get a "Run-time error 1004 (Method Offset of object Range failed.


Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("F3:F500")) Is Nothing Then
     Target.Offset(0, 1).ClearContents
    ElseIf Not Intersect(Target, Range("G3:G500")) Is Nothing Then
        Target.Offset(0, 1).ClearContents
        Target.Offset(0, 2).ClearContents
    ElseIf Not Intersect(Target, Range("H3:H500")) Is Nothing Then
        Target.Offset(0, 1).ClearContents
    End If
End Sub


Any idea of what could be wrong?

Thanks in advance!
This comment was minimized by the moderator on the site
Hallo,

Zu Punkt 3.
Die Taste "Andere" Finde ich nicht auf meiner Windows Tastatur. Ich Habe Strg, Alt, Tab... allerdings die Taste Andere gibt es auf meiner Tastatur leider nicht.

Lieben Gruß Mathias
This comment was minimized by the moderator on the site
Hi Mathias,
If you can't find the corresponding key on the keyboard. You can just click the Save button in the Microsoft Visual Basic Applications window to save the code and then manually close this window.
This comment was minimized by the moderator on the site
Помогите с решением, VBA не знаю. Мне нужно при изменении ячейки удалить данные из другой и чтобы это дейстовало на весь столбец.
Меняю А2 удаляется из G2, меняю А3 удаляется из G3, меняю A6 удаляется из G6 и т.д.

Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("A2")) Is Nothing Then
Range("G2").ClearContents
End If
End Sub


Данный код хорош для одной ячейки, а как его размножить на все ячейки столбца?
This comment was minimized by the moderator on the site
Hi Наталья,
The following VBA code can help you solve the problem. Please give it a try.

Private Sub Worksheet_Change(ByVal Target As Range)
'Updated by Extendoffice 20221013
    Dim xRight As Range
    Dim KeyCells As Range
    Set KeyCells = Range("A:A")
    Set xRight = Target.Offset(0, 6)
    If Not Application.Intersect(KeyCells, Range(Target.Address)) Is Nothing Then
        xRight.ClearContents
    End If
End Sub
This comment was minimized by the moderator on the site
Buongiorno, avrei bisogno di cancellare una serie di caselle (un rettangolo, quindi su più righe e colonne) in base al valore di un'altra cella. es: se la cella A2 è inferiore di 12, il quadrato con vertici opposto C2 : F4 venga cancellato.
Grazie mille
This comment was minimized by the moderator on the site
Hi Pietro,
Sorry I don't quite understand your question. Do you mind uploading a screenshot of your data?
This comment was minimized by the moderator on the site
Hello,

Just looking for an easy way to make it so if "B2" has selected "Yes" from the data validation list, cell B3 would clear it's data... and vice-versa: If "B3" has selected "Yes" from the data validation list, cell "B2" would clear it's data.

Basically B2 or B3 can say "Yes" (from the data validation list) but never at the same time, one should clear the other.
This comment was minimized by the moderator on the site
Hi Jeff,
The following VBA code can do you a favor. Please give it a try.
Private Sub Worksheet_Change(ByVal Target As Range)
'Updated by Extendoffice 20220721
If Target.Cells.Count > 1 Then Exit Sub
    If (Not Intersect(Target, Range("B2")) Is Nothing) And (Target.Value = "Yes") Then
        Range("B3").ClearContents
        Else
        If (Not Intersect(Target, Range("B3")) Is Nothing) And (Target.Value = "Yes") Then
        Range("B2").ClearContents
    End If
    End If
End Sub
This comment was minimized by the moderator on the site
Bonjour tout le monde,

Besoin d'aide, j'ai besoin d'effacer le contenu d'une cellule de la colonne "I" si la cellule (de la même ligne) de la colonne "O" =0, sur environ 2000 lignes avec des titres tout le 10 lignes environ.
This comment was minimized by the moderator on the site
Is it possible to clear specified cell contents if the trigger cell contains a specific number? Say, IF cell A1 = 1, then clear Cells A2:A4?
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