[英]Resize and change the format of multiple pictures using Excel VBA
I have an excel worksheet with a lot of pictures with various sizes and formats. 我有一个excel工作表,有很多不同大小和格式的图片。 I want to use excel VBA to loop through all the pictures in the worksheet, and set each picture to the same width (214) and change the picture type to a JPEG after resizing (to keep the file size down). 我想使用excel VBA循环遍历工作表中的所有图片,并将每张图片设置为相同的宽度(214)并在调整大小后将图片类型更改为JPEG(以保持文件大小不变)。 My pictures are located in various cells, and I don't want the picture locations to change (ie stay in the same cell). 我的图片位于不同的单元格中,我不希望图片位置发生变化(即保持在同一个单元格中)。 I'm new to VBA and tried the following - but it doesn't work. 我是VBA的新手并尝试了以下内容 - 但它不起作用。 The debugger stops at the line where I'm trying to cut the picture. 调试器停在我试图剪切图片的行。
Sub Macro6()
Dim p As Object
Dim iCnt As Integer
For Each p In ActiveSheet.Shapes
p.Width = 217.44
p.Cut
p.PasteSpecial Format:="Picture (JPEG)", Link:=False
iCnt = iCnt + 1
Next p
End Sub
It's not the cutting part that Excel doesn't like--it's the pasting part. 这不是Excel不喜欢的切割部分 - 它是粘贴部分。 Paste
and PasteSpecial
are methods you call with a worksheet object (where you're pasting to) instead of the image (the thing you're pasting). Paste
和PasteSpecial
是您使用工作表对象(粘贴到的地方)而不是图像(您要粘贴的东西)调用的方法。 I don't know if you want to just shrink the width and hold the height constant or if you want to scale both dimensions evenly. 我不知道你是想缩小宽度并保持高度不变,或者你想要均匀地缩放两个尺寸。 If you want to scale both evenly, try this: 如果要均匀缩放,请尝试以下操作:
Sub Macro6()
Dim p As Object
Dim iCnt As Integer
Dim s As Double
Dim r As Range
For Each p In ActiveSheet.Shapes
s = 214 / p.Width
Set r = p.TopLeftCell
p.Width = 214
p.Height = p.Height * s
p.Cut
r.Select
ActiveSheet.PasteSpecial Format:="Picture (JPEG)", Link:=False
Application.CutCopyMode = False
iCnt = iCnt + 1
Next p
End Sub
If you're just trying to shrink the width and leave the height the same, try this: 如果您只是想缩小宽度并保持高度相同,请尝试以下方法:
Sub Macro6()
Dim p As Object
Dim iCnt As Integer
Dim r As Range
For Each p In ActiveSheet.Shapes
Set r = p.TopLeftCell
p.Width = 214
p.Cut
r.Select
ActiveSheet.PasteSpecial Format:="Picture (JPEG)", Link:=False
Application.CutCopyMode = False
iCnt = iCnt + 1
Next p
End Sub
The locations of your pictures should stay exactly the same if they were originally right at the corner of a cell. 如果图片最初位于单元格的角落,则图片的位置应保持完全相同。 Otherwise, this will align the top left corner of the image to the nearest cell corner. 否则,这会将图像的左上角对齐到最近的单元格角。 The Application.CutCopyMode = False
is good practice after pasting. 粘贴后, Application.CutCopyMode = False
是一种很好的做法。 It tells Excel to wipe the clipboard and go back to normal operation instead of waiting for you to paste again. 它告诉Excel擦除剪贴板并返回正常操作,而不是等待您再次粘贴。 Hope this helps. 希望这可以帮助。
Thanks for answering my question! 谢谢回答我的问题! Here's the code I ended up using based on your suggestions. 这是我根据您的建议最终使用的代码。 The program took several minutes to run (had over 5000 pictures in the file - yikes!). 该程序需要几分钟才能运行(文件中有超过5000张图片 - 哎呀!)。 However, it was worth the wait, because it shrunk the file size in half. 然而,值得等待,因为它将文件大小缩小了一半。
Sub all_pics_to_jpeg()
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Dim mypic As Shape
Dim picleft As Double
Dim pictop As Double
For Each mypic In ActiveSheet.Shapes
mypic.LockAspectRatio = msoTrue
If mypic.Width > mypic.Height Then
mypic.Width = 217.44
Else: mypic.Height = 157.68
End If
picleft = mypic.Left
pictop = mypic.Top
With mypic
.Cut
ActiveSheet.PasteSpecial Format:="Picture (JPEG)", Link:=False, _
DisplayAsIcon:=False
Application.CutCopyMode = False
Selection.Left = picleft
Selection.Top = pictop
End With
Next mypic
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.