【问题标题】:Assigning a unique code to multiple commandbuttons created with VBA为使用 VBA 创建的多个命令按钮分配唯一代码
【发布时间】:2013-11-07 03:15:28
【问题描述】:

美好的一天, 第一次在这里为我发帖!即使上周 stackoverflow 社区多次帮助我编写此项目的代码,我也找不到任何问题的答案。

这是我想做的: 我在一家生产私人飞机的大公司实习,我被要求找到一种简单的方法来在特定表格中查找信息。这张表重新组合了公司销售的所有飞机,并附有每架飞机的特定信息。

正如我所说,感谢这个网站,我做了很多事情,但现在我有一件非常特别的事情要做。

当用户知道要查找哪架飞机时,它会打开一个选项卡(“结果”),其中包含有关这架飞机的所有信息。这是第一个搜索选项。它就像一个魅力!

这是我的excel界面:http://i.stack.imgur.com/TXcpY.jpg

但是当用户不知道要寻找哪架飞机时,他可以选择通过用户界面(“搜索”选项卡)搜索模型或内部所有者(或两者)。 代码执行,工作表“RESULTS2”在搜索栏中列出了所有带有命令按钮的飞机。我用 OLEObjects.add 创建了这些按钮,一切正常。

现在,我需要为这些对象中的每一个(Commandbutton1、Commandbutton2、...、CommandbuttonN)分配一个代码。 用户每次点击命令按钮时,都会执行第一次搜索的代码(当用户知道飞机编号时)与同一行的飞机编号。

我有要为每个按钮执行的代码,这些代码将被放入“RESULTS2”选项卡中:

Private Sub CommandButton1_Click()

'Variables
Dim avion As String
Dim lRow2 As Long

'Settings
Set sourceBook = ActiveWorkbook
Set sourceSheet = sourceBook.Sheets("SEARCH")
Set sourceSheet1 = sourceBook.Sheets("TABLE")
Set sourceSheet2 = sourceBook.Sheets("RESULTS")
Set sourceSheet3 = sourceBook.Sheets("RESULTS2")

'Set "avion" as the aircraft number on the button row ("RESULTS2" tab)
avion = sourceSheet3.Cells(6, "B").Value

'Set the aircraft number on the "SEARCH" tab (User interface) as "avion"
sourceSheet.Cells(5, "E").Value = avion

'Calls the macro to execute search
Call sourceSheet.Searchaircraft

End Sub

在这种特殊情况下的结果将仅适用于第一行的按钮 (Commandbutton1)。

有没有办法为所有命令按钮创建一个私有子? 或者也许创建一个 for 循环:

For i=1 To LastRow

    Private Sub "Commandbutton" & i & "_click()"
    [...]
    avion = sourceSheet3.Cells(i+5, "B").Value
    [...]
    End Sub

Next i

供您参考,这里是“搜索”选项卡中创建命令按钮的代码部分(在 for 循环中创建每一行):

'Bouton
            Set rng = sourceSheet3.Range("G" & n)
            Set mybutton = sourceSheet3.OLEObjects.Add(ClassType:="Forms.CommandButton.1")
            rng.Borders.LineStyle = xlContinuous
            With mybutton
                .Object.Caption = "<->"
                .Object.Font.Size = 3
                .Left = rng.Left + 1
                .Top = rng.Top + 1
                .Height = 11.75
                .Width = 60
            End With

您的任何帮助将不胜感激! 几个小时以来,这件事让我发疯......!

谢谢你,祝你有美好的一天, 亚历克斯


编辑:

Nutsch 的答案就是这个!

这是我在模块中编写的代码:

`Public Sub WhichButton()

On Error GoTo Err_cmdNewTerm_Click

'Variables
Dim avion As String
Dim ButtonText As String
Dim b As Object
Dim Ligne As Integer


'Settings
Set sourceBook = ActiveWorkbook
Set sourceSheet = sourceBook.Sheets("SEARCH")
Set sourceSheet1 = sourceBook.Sheets("TABLE")
Set sourceSheet2 = sourceBook.Sheets("RESULTS")
Set sourceSheet3 = sourceBook.Sheets("RESULTS2")


'Vérification de quel bouton a été appuyé
Set b = sourceSheet3.Buttons(Application.Caller)
    With b.TopLeftCell
        Ligne = .Row
    End With


' Récupération du numéro de l'avion
avion = sourceSheet3.Cells(Ligne, "B").Text

' Inscription du numéro dans la page SEARCH
sourceSheet.Cells(5, "E").Value = avion

'Calls the macro to execute search
Call sourceSheet.Searchaircraft


' Fenêtre en cas d'erreur de programmation
GoTo Exit_cmdNewTerm_Click

Err_cmdNewTerm_Click:
MsgBox Err.Description
Resume Exit_cmdNewTerm_Click

Exit_cmdNewTerm_Click:

End Sub

这是我工作表中添加按钮的代码:

'Bouton
Set rng = sourceSheet3.Range("G" & n)
sourceSheet3.Buttons.Add(rng.Left + 1, rng.Top + 1, 60, 11.75).OnAction = "WhichButton"
sourceSheet3.Buttons.Caption = "Search"

希望有一天这会对某人有所帮助。 谢谢! 亚历克斯

【问题讨论】:

    标签: excel vba commandbutton


    【解决方案1】:

    对所有按钮使用相同的宏,并使用 application.caller 找出按下了哪个按钮。

    【讨论】:

    • 好的,我想我现在不明白你的建议,我现在就试试!谢谢!
    猜你喜欢
    • 2021-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-22
    • 2022-08-12
    • 1970-01-01
    相关资源
    最近更新 更多