Vai al contenuto principale

Come nascondere o mostrare una certa forma in base al valore di cella specificato in Excel?

In realtà, una certa forma può essere nascosta o non nascosta in base al valore di una cella specificata. Il seguente metodo può aiutarti.

Nascondi o mostra una certa forma in base al valore della cella specificato con il codice VBA


Nascondi o mostra una certa forma in base al valore della cella specificato con il codice VBA

Ad esempio, vuoi scoprire una certa forma quando inserisci il numero 1 nella cella A1 o nascondere questa forma se la cella A1 è altri valori. Si prega di eseguire il seguente codice VBA per ottenerlo.

1. Fare clic con il pulsante destro del mouse sulla scheda del foglio che contiene la forma da nascondere o visualizzare, quindi fare clic Visualizza codice dal menu di scelta rapida.

2. Quindi il file Microsoft Visual Basic, Applications Edition si apre la finestra. Copia e incolla il codice VBA sottostante nel file Code finestra.

Codice VBA: nasconde o mostra una certa forma in base al valore di cella specificato

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

Note: Nel codice:

1) Riga = 1 ed Colonna = 1 indicare la cella specifica che si trova nella riga uno e nella colonna uno, Celle (1, 1) è la cella corrispondente A1.
2) Valore = 1, il numero 1 è il valore specifico su cui desideri mostrare la forma.
3) "Oval 6"È il nome di una certa forma.

Puoi cambiarli in base alle tue necessità.

3. premi il altro + Q contemporaneamente i tasti per chiudere il file Microsoft Visual Basic, Applications Edition finestra.

D'ora in poi, quando si immette il numero 1 nella cella A1, la forma "Ovale 6" non è nascosta. Ma se inserisci un altro valore come il numero 2 nella cella A1, la forma "Ovale 6" viene nascosta immediatamente.


Articoli correlati:

I migliori strumenti per la produttività in ufficio

🤖 Assistente AI di Kutools: Rivoluziona l'analisi dei dati basandosi su: Esecuzione intelligente   |  Genera codice  |  Crea formule personalizzate  |  Analizzare i dati e generare grafici  |  Richiama le funzioni di Kutools...
Funzioni popolari: Trova, evidenzia o identifica i duplicati   |  Elimina righe vuote   |  Combina colonne o celle senza perdere dati   |   Round senza formula ...
Super ricerca: VLookup a criteri multipli    VLookup a valori multipli  |   VLookup su più fogli   |   Ricerca fuzzy ....
Elenco a discesa avanzato: Crea rapidamente un elenco a discesa   |  Elenco a discesa dipendente   |  Elenco a discesa a selezione multipla ....
Gestore di colonna: Aggiungi un numero specifico di colonne  |  Sposta colonne  |  Attiva/disattiva lo stato di visibilità delle colonne nascoste  |  Confronta intervalli e colonne ...
Funzionalità in primo piano: Messa a fuoco della griglia   |  Vista di progettazione   |   Grande barra delle formule    Gestore di cartelle di lavoro e fogli   |  Resource Library (Testo automatico)   |  Date picker   |  Combina fogli di lavoro   |  Crittografa/decrittografa le celle    Invia e-mail per elenco   |  Super filtro   |   Filtro speciale (filtro grassetto/corsivo/barrato...) ...
I 15 migliori set di strumenti12 Testo Strumenti (aggiungi testo, Rimuovi personaggi, ...)   |   50+ Grafico Tipi (Diagramma di Gantt, ...)   |   40+ Pratico Formule (Calcola l'età in base al compleanno, ...)   |   19 Inserimento Strumenti (Inserisci il codice QR, Inserisci immagine dal percorso, ...)   |   12 Conversione Strumenti (Numeri in parole, Conversione di valuta, ...)   |   7 Unisci e dividi Strumenti (Combina righe avanzate, Celle divise, ...)   |   ... e altro ancora

Potenzia le tue competenze di Excel con Kutools per Excel e sperimenta l'efficienza come mai prima d'ora. Kutools per Excel offre oltre 300 funzionalità avanzate per aumentare la produttività e risparmiare tempo.  Fai clic qui per ottenere la funzionalità di cui hai più bisogno...

Descrizione


Office Tab porta l'interfaccia a schede in Office e semplifica notevolmente il tuo lavoro

  • Abilita la modifica e la lettura a schede in Word, Excel, PowerPoint, Publisher, Access, Visio e Project.
  • Apri e crea più documenti in nuove schede della stessa finestra, piuttosto che in nuove finestre.
  • Aumenta la produttività del 50% e riduce ogni giorno centinaia di clic del mouse!
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