Excel VBA TopLeftCell.Row属性不一致的结果

问题描述 投票:0回答:1

这是场景。当在工作表中插入一行时,有一个worksheet_change事件,它调用一个子插件,在插入的行上插入五对按钮。当单击其中一个按钮时,它会运行一个子窗口,该子窗口发出显示按钮的TopLeftCell.Row和Column的MsgBox。可以插入多行,每行包含五组按钮(总共十个)。任何时候都可以选择任何行中的任何按钮。

我看到的是,在打开工作簿并按下其中一个按钮后,MsgBox始终显示正确的列,但无论我点击哪一行按钮,它似乎都“卡在”某一行上实际上是。如果我删除了它被卡住的行,那么它会“卡在”不同的行上(只要它还包含按钮)。但并非所有的按钮都会粘在同一行上。

如果我将两个相邻的行复制到同一行“卡在”另一个位置的按钮上,那么这些按钮仍然会粘在一起,除了在另一行上。

看起来按钮集合存在问题。如果我保存工作簿,问题就会消失。如果我插入一个新行并再次运行Add_Buttons例程,则问题会重新出现但涉及不同的行。所以我的按钮例程可能会留下一些临时的东西,当我进行保存时会被清除。

这是构建按钮的代码。

Public activeWS As Worksheet
Public activeRG As Range

Public Sub Add_Buttons(ByVal myRow As Long)
'Add the five sets of Submit IM and Submit Webins buttons to a new row. The code
'uses named ranges to locate the cells where the buttons should be added so that
'new columns can be added to the spreadsheet without requiring changes to the code.
'The headings must be labeled 'IM#' and 'Webins#'.

Dim i As Long
Dim t As Range
Dim btn As Button


'In each range, place the button below the column label.
Application.ScreenUpdating = False
For i = 1 To 5

Set activeWS = Sheet1
Set activeRG = activeWS.Range("Scan" & i & "_Hdngs")

'The start of the range plus the position of the specified column in
'the range gives the absolute column location to add the button

'Create the Submit IM button
nCol = activeRG.Cells(1, 1).Column + findCol("IM#", activeRG) - 1
Set t = activeWS.Range(Cells(myRow, nCol), Cells(myRow, nCol))
Set btn = activeWS.Buttons.Add(t.Left, t.Top, t.Width, t.Height)
With btn
    .OnAction = "Create_Primary_IM"
    .Caption = "Submit IM"
    .Name = "BtnIM" & myRow & i
    .Font.Size = 10
End With

'Create the Submit Webins button
nCol = activeRG.Cells(1, 1).Column + findCol("Webins#", activeRG) - 1
Set t = activeWS.Range(Cells(myRow, nCol), Cells(myRow, nCol))
Set btn = activeWS.Buttons.Add(t.Left, t.Top, t.Width, t.Height)
With btn
    .OnAction = "Create_Primary_WAS"
    .Caption = "Submit Webins"
    .Name = "BtnWAS" & myRow & i
    .Font.Size = 10
End With
Next i
Application.ScreenUpdating = True

End Sub

这是按钮执行的代码:

Public Sub Create_Primary_IM()
    MsgBox ("Row, Col of pressed button: " & ActiveSheet.Shapes(Application.Caller).TopLeftCell.Row _
        & ", " & ActiveSheet.Shapes(Application.Caller).TopLeftCell.Column)
End Sub

Public Sub Create_Primary_WAS()
    MsgBox ("Row, Col of pressed button: " & ActiveSheet.Shapes(Application.Caller).TopLeftCell.Row _
        & ", " & ActiveSheet.Shapes(Application.Caller).TopLeftCell.Column)
End Sub
excel-vba button vba excel
1个回答
0
投票

以下是我认为是问题的原因,它对电子表格的影响以及解决方案。

原因是重复的按钮名称。我在创建时根据行和列命名按钮。问题是可以在工作表中的任何位置插入或删除行及其按钮。不难看出(除了我暂时没有看到它)可以插入一行并在同一绝对行中多次创建它们的按钮。这导致按钮具有相同的.name属性。

结果是,当我单击其中一个重复按钮时,它找到了具有该名称的原始按钮的TopLeftCell.Row,而不是该按钮实际所在的行。当我保存文件(未关闭它)并单击相同的按钮时,它已被重命名为“Button nnn”,并返回了正确的行。如果我保存文件并重新打开它,则仍然存在重复的按钮名称条件。因此,当我保存文件时Excel正在替换该集合中的重复名称,但在关闭文件时不提交这些更改。

最快的解决方案也是最简单的。我让Excel做命名。我真的不需要按钮来命名。我只需要行号。另一个解决方案是生成一个唯一的名称,但是,如果那就是Excel将要为我做什么呢?

© www.soinside.com 2019 - 2024. All rights reserved.