2012-02-08 146 views
5

我正在Excel電子表格中選擇下拉框的值時,將彈出一個圖像,如果選擇了另一個值,它將隱藏當前圖像並彈出與選擇相關的圖像。我發現了一些僅僅使用表格和使用座標定位圖像太耗時的方法;這不完全是我想要去的路線。在使用StackOverflow之前,我已經做了相當多的研究,到目前爲止似乎沒有任何工作。以下是我想要實現的。我試圖保留電子表格中的所有圖像,這增加了另一個級別的挑戰,但我相信有一種方法可以做到這一點,因爲excel在插入EX時會爲圖像指定一個數字。圖片9(Excel VBA)如果單元格值等於「」然後顯示/隱藏圖像

Sub Main() 
    If Range(G11).Value = "anything" Then 

    Picture1 show 

    Picture2 hide 

    End If 
End Sub 

任何幫助,非常感謝。謝謝

回答

5

而不是隱藏/移動/減少不需要的圖片的大小,爲什麼不簡單地刪除它?

邏輯: 將所有圖像保存在臨時工作表中。如果有相關圖片應該顯示,請從臨時表中取出並刪除前一張。

這裏是一個例子。

Sub Sample() 
    Select Case Range("G11").Value 
     Case "Picture 1": ShowPicture ("Picture 1") 
     Case "Picture 2": ShowPicture ("Picture 2") 
     Case "Picture 3": ShowPicture ("Picture 3") 
     Case "Picture 4": ShowPicture ("Picture 4") 
    End Select 
End Sub 

Sub ShowPicture(picname As String) 
    '~~> The reason why I am using OERN is because it is much simpler 
    '~~> than looping all shapes and then deleting them. There could be 
    '~~> charts, command buttons and other shapes. I will have to write 
    '~~> extra validation code so that those shapes are not deleted. 
    On Error Resume Next 
    Sheets("Sheet1").Shapes("Picture 1").Delete 
    Sheets("Sheet1").Shapes("Picture 2").Delete 
    Sheets("Sheet1").Shapes("Picture 3").Delete 
    Sheets("Sheet1").Shapes("Picture 4").Delete 
    On Error GoTo 0 

    Sheets("Temp").Shapes(picname).Copy 

    '<~~ Alternative to the below line. You may re-position the image 
    '<~~ after you paste as per your requirement 
    Sheets("Sheet1").Range("G15").Select 

    Sheets("Sheet1").Paste 
End Sub 

快照溫度片

enter image description here

0
Sub hidePicture(myImage) 
    ActiveSheet.Shapes.Range(Array(myImage)).Select 
    Selection.ShapeRange.Height = 0 
    Selection.ShapeRange.Width = 0 
End Sub 

Sub showPicture(myImage) 
    ActiveSheet.Shapes.Range(Array(myImage)).Select 
    Selection.ShapeRange.Height = 200 
    Selection.ShapeRange.Width = 300 
End Sub 

方便的提示:記錄宏並查看它生成的代碼!

+0

我沒有這樣做,但從來沒有想過,只是它縮小到什麼感謝的解決方案現在要嘗試 – 2012-02-08 17:35:33

+0

對一個沒有運氣只允許一個圖像不能做多張圖片。我希望使用單元格值可以控制顯示的圖像 – 2012-02-08 17:52:47

0

只是爲了將圖片移動到「屏幕外」可能更好,尤其是如果它們的尺寸不同。

Sub Tester() 
    ShowPicture "Picture 3" 
End Sub 

Sub ShowPicture(PicName As String) 

    Dim s As Shape 
    For Each s In ActiveSheet.Shapes 
     With s 
     .Top = IIf(.Name = PicName, 100, 100) 
     .Left = IIf(.Name = PicName, 100, 1000) 
     End With 
    Next s 

End Sub 
2

這裏是使用對象的可見性屬性的溶液。 我用它來顯示基於字段中的值的圖片。 該領域有一個公式,導致「好」或「壞」。 如果它的價值是「好」,我想展示一張照片;對於「壞」,另一張照片應該顯示;並且他們不應該同時顯示。 無論何時用戶刷新數據透視表,該字段都需要更新其值,因此我將代碼放在工作表的數據透視表和圖片出現的位置。

Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable) 
'hide both pictures by loopng through all the shapes on the sheet 
Dim s As Shape 
For Each s In ActiveSheet.Shapes 
'hide the shape if it is a picture, leave other shapes on the page visible. 
If s.Type = msoPicture Then s.Visible = msoFalse 
Next 

Dim judgement As String 
'The field whose value tells what picture to use is a one-cell named range called "judgement" 
judgement = Range("judgement") 

'you need to know which picture is which. 
If judgement = "Good" Then ActiveSheet.Shapes("Picture 8").Visible = True 
If judgement = "Bad" Then ActiveSheet.Shapes("Picture 1").Visible = True 

End Sub 
+0

非常有用,謝謝。 – JCO9 2017-03-14 15:03:44

相關問題