Shapes family:
First of all, in VBA they are called shapes
Example: Calculate how many shapes
Sub test()
MsgBox Sheet1.Shapes.Count
End Sub
shape attribute
Sub test() Dim shp As Shape For Each shp In Sheet1.Shapes i = i + 1 Range("a" & i) = shp.Name Range("b" & i) = shp.TopLeftCell.Address Range("c" & i) = shp.Type Next End Sub
There are pictures in excel above
Sheet table also has its own type
Sub test1 () MsgBox Sheets ( 2 ) .Type ' Worksheet also has its own type End Sub
Delete pictures, delete according to different types
Sub test() Dim shp As Shape For Each shp In Sheet1.Shapes If shp.Type = msoPicture Then 'shp.type = 13也行 shp.Delete End If Next End Sub
Additional note: The parameters with "[]" in the parameters can be omitted, the rest are the parameters that must be written
Insert and adjust pictures according to position (can help, record macro to achieve, learn to learn by yourself)
As shown below, you need to import pictures
The implementation code is as follows:
Sub test() Dim i As Integer Dim shp As Shape On Error Resume Next For Each shp In Sheet1.Shapes '删除所有图片,以免越点越多 shp.Delete Next For i = 2 To 12 Sheet1.Shapes.AddPicture "d:\data\" & Range("a" & i) & ".jpg", msoFalse, msoTrue, Range("d" & i).Left, Range("d" & i).Top, Range("d" & i).Width, Range("d" & i).Height Next End Sub
Further automation: make the size of the picture change with the size of the cell, realize it by recording a macro, learn
Sub test() Dim i As Integer Dim shp As Shape Dim shp1 As Shape On Error Resume Next For Each shp In Sheet1.Shapes '删除所有图片,以免越点越多 shp.Delete Next For i = 2 To 12 Set shp1 = Sheet1.Shapes.AddPicture("d:\data\" & Range("a" & i) & ".jpg", msoFalse, msoTrue, Range("d" & i).Left, Range("d" & i).Top, Range("d" & i).Width, Range("d" & i).Height) shp1.Placement = xlMoveAndSize Next End Sub
Change the file name
The method of renaming files in VBA is as follows: name ..... as ......
Sub test1() Name "d:\data\汪梅.jpg" As "d:\data\汪梅123.jpg" End Sub
Change the name of the picture according to the data in the excel sheet as follows
code show as below:
Sub test() Dim i As Integer On Error Resume Next For i = 2 To 12 Name "d:\data\" & Range("a" & i) & ".jpg" As "d:\data\" & Range("a" & i) & Range("d" & i) & ".jpg" Next End Sub
Chart object: achieved by recording a macro
achieve
code show as below:
Sub test () Dim shp As Shape Set shp = Sheet1.Shapes.AddChart2 shp.Chart.SetSourceData Range ( " b2: c14 " ) ' Data source shp.Chart.ChartType = xlLine ' Set the bar chart or line chart and other graphics shp. Chart.Axes (xlValue) .MinimumScale = 1000000 ' Set the vertical coordinate interval End Sub
Use form controls
Form control saves memory than ActiveX control, simple and flexible
Use grouping boxes to make the multiple choices mutually exclusive
There is a group box that affects the appearance, so how to hide it? In the group box attribute, he does not have this hiding function, so it cannot be recorded to achieve, rely on guessing, and bypass the category
Sub test () Dim shp As Shape ' Find the difference between form controls For Each shp In Sheet1.Shapes i = i + 1 Range ( " g " & i) = shp.Name ' range ("g" & i) = shp. type Next End Sub
----------------------------- Sub test1 () Dim shp As Shape For Each shp In Sheet1.Shapes ' If shp.Name = "Group Box *" Then writing this has no effect, = must be a precise name If shp.Name Like " Group Box * " Then shp.Visible = msoFalse End If Next End Sub
Can also be like this
Sub test1() Dim shp As Shape For Each shp In Sheet1.Shapes If shp.FormControlType = xlGroupBox Then shp.Visible = msoFalse End If Next End Sub
like operator
The characters inside need to remember
Sub test() Dim i As Integer Range("a2:a15").Interior.Pattern = xlNone For i = 2 To 15 'If Range("a" & i) Like "J*" Then '"J??????" "J???w???" 'If Range("a" & i) Like "[A-M a-m]*" Then 代表以字母开头的 'If Range("a" & i) Like "[0-9]*" Then '或者可以 "#*";"##*"#代表一个数字 'If Range("a"I &) Like "[0-9] [! 0-9] *" the Then 'Exclamation point representative of "non" means !'If Range("a" & i) Like "J???[A-Z a-z]??" Then Range("a" & i).Interior.Color = 65535 'Range("a" & i).Font.Color = 65535 k = k + 1 End If Next Range("e1") = k End Sub