!: tham khảo:
Nguồn: www.get-digital-help.com/2013/10/25/show-and-hide-a-picture-vba/
Show and hide a picture (vba)
Have you ever seen dashboards where you can
click a shape and a picture shows up. If you click it again the picture
disappears. This post explains how to do that. The following picture
shows when you click the shape, the picture is hidden. Click it again
and the picture shows up.
Move and click the shape and the picture is automatically repositioned to the lower right shape corner. You can also position the picture wherever you like, I'll show later in this post.
Insert a shape
VBA Code
Step 2 - Check if text in shape "Rounded Rectangle 4" is "Hide"
Step 3 - If so, change text to "Show"
Step 4 - and hide Picture 1
Step 5 - If text in shape is not "Hide", change text to "Hide"
Step 6 - Also, move shape to the lower right shape corner and make it visible
Step 7 - End If-then-Else statement
Step 8 - End With statement
Upper left corner
Lower left corner
Move and click the shape and the picture is automatically repositioned to the lower right shape corner. You can also position the picture wherever you like, I'll show later in this post.
Instructions
First, let´s create a button (shape).Insert a shape
- Go to tab "Insert" on the ribbon
- Click Shapes button
- Select a shape
- Drag on sheet to create the shape
VBA Code
1 | Sub Macro1() |
2 | With ActiveSheet.Shapes( "Rounded Rectangle 4" ).TextFrame2.TextRange.Characters |
3 | If .Text = "Hide" Then |
4 | .Text = "Show" |
5 | ActiveSheet.Shapes( "Picture 1" ).Visible = False |
6 | Else |
7 | .Text = "Hide" |
8 | With ActiveSheet.Shapes( "Rounded Rectangle 4" ) |
9 | ActiveSheet.Shapes( "Picture 1" ).Left = .Left + .Width |
10 | ActiveSheet.Shapes( "Picture 1" ).Top = .Top + .Height |
11 | ActiveSheet.Shapes( "Picture 1" ).Visible = True |
12 | End With |
13 | End If |
14 | End With |
15 | End Sub |
- Go to VB Editor (Alt + F11)
- Click "Insert" on the menu
- Click "Module"
- Paste vba code
- Exit VB Editor and return to excel
- Right click on the shape
- Click "Assign Macro"
- Select Macro1
- Click OK
Explaining the vba code
Step 1 - The With statement execute a series of statements that repeatedly refers to a single object or structure1 | With ActiveSheet.Shapes( "Rounded Rectangle 4" ).TextFrame2.TextRange.Characters |
1 | If .Text = "Hide" Then |
1 | .Text = "Show" |
1 | ActiveSheet.Shapes( "Picture 1" ).Visible = False |
1 | Else |
2 | .Text = "Hide" |
1 | With ActiveSheet.Shapes( "Rounded Rectangle 4" ) |
2 | ActiveSheet.Shapes( "Picture 1" ).Left = .Left + .Width |
3 | ActiveSheet.Shapes( "Picture 1" ).Top = .Top + .Height |
4 | ActiveSheet.Shapes( "Picture 1" ).Visible = True |
5 | End With |
1 | End If |
1 | End With |
Positioning the picture
Upper right corner1 | With ActiveSheet.Shapes( "Rounded Rectangle 4" ) |
2 | ActiveSheet.Shapes( "Picture 1" ).Left = .Left + .Width |
3 | ActiveSheet.Shapes( "Picture 1" ).Top = .Top - ActiveSheet.Shapes( "Picture 1" ).Height |
4 | ActiveSheet.Shapes( "Picture 1" ).Visible = True |
5 | End With |
Upper left corner
1 | With ActiveSheet.Shapes( "Rounded Rectangle 4" ) |
2 | ActiveSheet.Shapes( "Picture 1" ).Left = .Left - ActiveSheet.Shapes( "Picture 1" ).Width |
3 | ActiveSheet.Shapes( "Picture 1" ).Top = .Top - ActiveSheet.Shapes( "Picture 1" ).Height |
4 | ActiveSheet.Shapes( "Picture 1" ).Visible = True |
5 | End With |
Lower left corner
1 | With ActiveSheet.Shapes( "Rounded Rectangle 4" ) |
2 | ActiveSheet.Shapes( "Picture 1" ).Left = .Left - ActiveSheet.Shapes( "Picture 1" ).Width |
3 | ActiveSheet.Shapes( "Picture 1" ).Top = .Top + .Height |
4 | ActiveSheet.Shapes( "Picture 1" ).Visible = True |
5 | End With |
Không có nhận xét nào:
Đăng nhận xét