gpt4 book ai didi

excel - 标签内的图像捕获

转载 作者:行者123 更新时间:2023-12-03 03:06:06 25 4
gpt4 key购买 nike

我有一个用户表单,其中包含一些带有图像的按钮。然后,当我单击按钮/图像时,我会得到三个空白标签,图像会显示在第一个标签中。然后,如果我单击另一个按钮/图像,第二个标签就是图像,第三个标签也是如此。

标记为 btn1 至 btn36 的 36 个按钮三个空白标签,名为 capture1 capture2 capture3

我不确定是否需要发布任何其他信息。

我需要帮助编写此代码(VBA)

最佳答案

从按钮获取图像到标签的基础知识很简单:

Capture1.Picture = btn1.picture

真正令人头痛的是,您无法将相同的代码块分配给 VBA 中的多个 Button.Click 事件。与 VB 中的语法不同:

Sub MyRoutine(sender, <args>) Handles Button1.Click, Button2.Click...

在 VBA 中,您基本上需要 36 个不同的 _Click 例程,每个按钮 1 个。设置模块级计数器来跟踪您要引用的标签。

Dim counter As Integer

Private Sub UserForm_Activate()
counter = 1
End Sub

Private Sub btn1_Click()
If counter > 3 Then
counter = 1
End If
Controls("capture" & counter).Picture = btn1.Picture
counter = counter + 1
End Sub

您将需要 36 个与此相同的例程,除了每个例程中的按钮名称之外。幸运的是,您主要可以通过剪切和粘贴来完成此操作。

如果这样做,您可以节省几行代码:

Private Sub btn1_Click()
Call CaptureImage(btn1.Name)
End Sub

Private Sub btn2_Click()
Call CaptureImage(btn2.Name)
End Sub
.
.
.
Private Sub CaptureImage(ByVal btnName As String)
If counter > 3 Then
counter = 1
End If
Controls("capture" & counter).Picture = Controls(btnName).Picture
counter = counter + 1
End Sub

但您最终仍会得到 36 个调用 CaptureImage 子例程的 _Click 例程。

关于excel - 标签内的图像捕获,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5597345/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com