excel - 图像调整excel宏
问题描述
我有一个宏,我正在调整图像大小。应该发生什么:图像位于单元格内,当我单击它时它会放大,当我再次单击它时它会恢复正常。但是,当我单击图像以使其恢复正常时,它不会,它会从单元格中消失。代码如下和一张图片,看看我点击图片时会发生什么
Sub Picture1_Click()
Dim shp As Shape
Dim big As Single, small As Single
Dim shpDouH As Double, shpDouOriH As Double
big = 3
small = 1
On Error Resume Next
Set shp = ActiveSheet.Shapes(Application.Caller)
With shp
shpDouH = .Height
.ScaleHeight 1, msoTrue, msoScaleFromTopLeft
shpDouOriH = .Height
If Round(shpDouH / shpDouOriH, 2) = big Then
.ScaleHeight small, msoTrue, msoScaleFromTopLeft
.ScaleWidth small, msoTrue, msoScaleFromTopLeft
.ZOrder msoSendToBack
Else
.ScaleHeight big, msoTrue, msoScaleFromTopLeft
.ScaleWidth big, msoTrue, msoScaleFromTopLeft
.ZOrder msoBringToFront
End If
End With
End Sub
如何将图像重置为以前的图像?
解决方案
您的代码中的问题是使用 ScaleHeight。此方法根据当前大小调整图像大小,因此.ScaleHeight 1, msoTrue, msoScaleFromTopLeft
什么也不做;它基本上将图像调整为它已经拥有的大小。
在此处阅读有关 ScaleHeight 的更多信息:Shape.ScaleHeight 方法 (Excel)
在编写代码时,shpDouH
将始终等于shpDouOriH
,这意味着Round(shpDouH / shpDouOriH, 2)
它将始终精确计算为 1。这违背了您的If ... Then ... Else
逻辑。每点击一次,图片尺寸会增加3倍的比例因子;它永远不会缩小。
将图像缩小回其原始尺寸的最简单方法是,如果您能够为未放大的图像定义最大尺寸。然后,您可以使用类似的测试If shp.Height > MaxHeight Then
。在基本概念中,如果当前高度小于 MaxHeight,则缩放 3 倍;否则按 1/3 倍缩放。如果原始图像的大小大致相同,这应该可以工作。
否则,您将需要在每张图像放大之前选择一种方法来记录原始尺寸。
推荐阅读
- android - WorkManager的Worker的doWork内部的RxJava Observable流
- pandas - 根据行的聚合计数排除 pandas 数据框的列
- python - 在 Python 上评估导数时出错(使用 .subs、.evalf 和 .lambdify)
- python-3.x - 仅 Gunicorn 是否足以托管 Flask 应用程序/是否必须配置 Gunicorn
- r - 根据(部分)匹配的行名计算列均值
- asp.net-mvc - 通过配置禁用 asp.net mvc 中的授权属性
- javascript - Chart.js + Angular 5 - Destroy() 通过 *ngFor 循环创建的多个动态图表
- r - for循环在r中创建新变量
- python - 变量未定义错误,即使它是?
- kentico - DocumentHelper.GetDocuments().InCategories() API 方法未按预期工作