Hoppa till huvudinnehåll

Hur döljer eller visar jag en viss form baserat på specificerat cellvärde i Excel?

Egentligen kan en viss form döljas eller döljas baserat på värdet på en viss cell. Följande metod kan hjälpa dig.

Dölj eller visa en viss form baserat på specificerat cellvärde med VBA-kod


Dölj eller visa en viss form baserat på specificerat cellvärde med VBA-kod

Till exempel vill du ta bort en viss form när du anger nummer 1 i cell A1 eller dölja denna form om cell A1 är andra värden. Vänligen kör följande VBA-kod för att uppnå den.

1. Högerklicka på arkfliken som innehåller den form du döljer eller visar och klicka sedan på Visa kod från högerklickmenyn.

2. Sedan Microsoft Visual Basic för applikationer fönstret dyker upp. Kopiera och klistra in nedanstående VBA-kod i Koda fönster.

VBA-kod: Dölj eller ta bort en viss form baserat på det angivna cellvärdet

Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Row = 1 And Target.Column = 1 Then _
        Me.Shapes("Oval 6").Visible = (Cells(1, 1).Value = 1)
End Sub

Anmärkningar: I koden:

1) Rad = 1 och Kolumn = 1 ange den specifika cellen som finns i rad ett och kolumn ett, celler (1, 1) är motsvarande cell A1.
2) Värde = 1är siffran 1 det specifika värde som du vill visa formen baserat på.
3) "Oval 6”Är namnet på den specifika formen.

Du kan ändra dem baserat på vad du behöver.

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

Från och med nu, när du matar in nummer 1 i cell A1, är formen "Oval 6" oskyddad. Men om du anger ett annat värde som nummer 2 i cellen A1, döljs formen "Oval 6" omedelbart.


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 (14)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
Hello,

thank you for making this guide!

I have one question:
If i need a shape to show up based on two or more values, how is this done?
This comment was minimized by the moderator on the site
Hi Kasper Pedersen,

Suppose you want to show up a shape named "Oval 4" when both cells A1 and C8 contain the specified values (1 and 2 respectively), and if either of the cells is cleared (i.e., its value becomes empty), the shape will set to invisible. You can try the following VBA code to get it done.

Private Sub Worksheet_Change(ByVal Target As Range)
    If Range("A1").Value = 1 And Range("C8").Value = 2 Then
        Me.Shapes("Oval 4").Visible = True
    ElseIf Range("A1").Value = "" Or Range("C8").Value = "" Then
        Me.Shapes("Oval 4").Visible = False
    End If
End Sub
This comment was minimized by the moderator on the site
Thanks, but i want the shape not to be visible if the value is diffrent than "1" and "2" as shown in your example. eg. if the value is diffrent from 1 in cell A1 the shape becomes invisible. Is it possible?
This comment was minimized by the moderator on the site
This article doesn't give any hint as to how one gets the name of a shape.

I have checked the name manager and the object manager in VBA as well as the context menu - there is no "Properties" menu for shapes.

So, where is the shape name?
This comment was minimized by the moderator on the site
Hi Cornan,
The shape name will be displayed on the Name box of worksheet when selecting the shape. Sorry for the inconvenience.
This comment was minimized by the moderator on the site
2 questions:

1. I cant seem to get this code to run and i dont understand why... copied and pasted it exactly as it is there...help!!!


2. how do i change it for shapes like Shapes.Range(Array("Rounded Rectangle 1"))
This comment was minimized by the moderator on the site
Hi Gus,
You must miss something in the operation.
Supposing your shape name is "Rounded Rectangle 1", please copy the below code into the worksheet code window (this worksheet should contain the specified shape "Rounded Rectangle 1").
From now on, only typing number 1 into A1 cell can display the shape "Rounded Rectangle 1". If you type in other content, the shape will be hidden.

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Row = 1 And Target.Column = 1 Then _
Me.Shapes("Rounded Rectangle 1").Visible = (Cells(1, 1).Value = 1)
End Sub
This comment was minimized by the moderator on the site
You left out the ":"
This comment was minimized by the moderator on the site
I am a newbie in VBA Excel. I am working with this code and I would like to optimize it. This code makes a shape visible on an active cell if value is 1 other values hide it. Range includes J13:AC161. If I will use the code below, it will take me more lines of code. Any help will be much appreciated.

Private Sub Worksheet_Change(ByVal Target As Range)
If ActiveSheet.Range("E13").Value = 1 Then
ActiveSheet.Shapes("rt1").Visible = True
Else
ActiveSheet.Shapes("rt1").Visible = False
End If

If ActiveSheet.Range("F13").Value = 1 Then
ActiveSheet.Shapes("rt2").Visible = True
Else
ActiveSheet.Shapes("rt2").Visible = False
End If

If ActiveSheet.Range("G13").Value = 1 Then
ActiveSheet.Shapes("rt3").Visible = True
Else
ActiveSheet.Shapes("rt3").Visible = False
End If

If ActiveSheet.Range("H13").Value = 1 Then
ActiveSheet.Shapes("rt4").Visible = True
Else
ActiveSheet.Shapes("rt4").Visible = False
End If

If ActiveSheet.Range("I13").Value = 1 Then
ActiveSheet.Shapes("rt5").Visible = True
Else
ActiveSheet.Shapes("rt5").Visible = False
End If

If ActiveSheet.Range("J13").Value = 1 Then
ActiveSheet.Shapes("rt6").Visible = True
Else
ActiveSheet.Shapes("rt6").Visible = False
End If
...

End Sub
This comment was minimized by the moderator on the site
Good day,
Do you mean you want to display or hide lots of specified shapes based on cells in range J13:AC161 with brief code?
This comment was minimized by the moderator on the site
This works great for me as long as the value entered is a number. I need it to work on letters like A B C etc: when i use letters it works backwards enter A and it hides i need it to be visible when i enter a letter any ideas
This comment was minimized by the moderator on the site
You can use letters instead, you just need to add " to either side. E.g. Me.Shapes("Oval 6").Visible = (Cells(1, 1).Value = "A")
This comment was minimized by the moderator on the site
How about if i want to add two values as the input such as : E.g. Me.Shapes("Oval 6").Visible = (Cells(1, 1).Value = "A" Or "B")?
This comment was minimized by the moderator on the site
Me.Shapes("Rounded Rectangle 2").Visible = (Cells(1, 1).Value = "A" Or Cells(1, 1).Value = "B")
There are no comments posted here yet
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations