excel 中的按钮引用了不正确的行
Button referencing incorrect row in excel
我有这段代码可以帮助按钮识别它在哪一行。但是,当我隐藏上面的行时,按钮会引用隐藏的行。
例如:如果按钮在第 20 行,而我隐藏第 19 行,则单击按钮 returns 第 19 行。如果我同时隐藏第 19 和 18 行,则按钮 returns 第 18 行.
真的很奇怪
这是我用来创建按钮的块:
Sub AddButtons()
Dim button As button
Application.ScreenUpdating = False
Dim st As Range
Dim sauce As Integer
For sauce = 10 To Range("F" & Rows.Count).End(xlUp).Row Step 1
Set st = ActiveSheet.Range(Cells(sauce, 11), Cells(sauce, 11))
Set button = ActiveSheet.Buttons.Add(st.Left, st.Top, st.Width, st.Height)
With button
.OnAction = "GoToIssue.GoToIssue"
.Caption = "Go To Source"
.Name = "Button" & sauce
End With
Next sauce
Application.ScreenUpdating = True
End Sub
这里是 returns 单击按钮后的行 ID 的块:
Sub GoToIssue()
Dim b As Object
Dim myrow As Integer
Dim hunt As String
Set b = ActiveSheet.Buttons(Application.Caller)
With b.TopLeftCell
myrow = .Row
End With
hunt = Worksheets("Dummy").Range("F" & myrow).Value
'MsgBox hunt
End Sub
感谢您的宝贵时间和帮助。
您可以使用这个功能:
Public Function FindButtonRow(btn As Object) As Long
Dim cell As Excel.Range
'-------------------------------------------------
Set cell = btn.TopLeftCell
Do While cell.EntireRow.Hidden
Set cell = cell.Offset(1, 0)
Loop
FindButtonRow = cell.row
End Function
检查TopLeftCell
方法返回的单元格是否不在隐藏行中。如果是,该函数会尝试下面的单元格,依此类推,只要它从未隐藏的行中找到单元格即可。
您可以在您的子程序中使用它 GoToIssue
就像这样:
Sub GoToIssue()
Dim b As Object
Dim myrow As Integer
Dim hunt As String
Set b = ActiveSheet.Buttons(Application.Caller)
myrow = FindButtonRow(b)
hunt = Worksheets("Dummy").Range("F" & myrow).Value
'MsgBox hunt
End Sub
我有这段代码可以帮助按钮识别它在哪一行。但是,当我隐藏上面的行时,按钮会引用隐藏的行。
例如:如果按钮在第 20 行,而我隐藏第 19 行,则单击按钮 returns 第 19 行。如果我同时隐藏第 19 和 18 行,则按钮 returns 第 18 行.
真的很奇怪
这是我用来创建按钮的块:
Sub AddButtons()
Dim button As button
Application.ScreenUpdating = False
Dim st As Range
Dim sauce As Integer
For sauce = 10 To Range("F" & Rows.Count).End(xlUp).Row Step 1
Set st = ActiveSheet.Range(Cells(sauce, 11), Cells(sauce, 11))
Set button = ActiveSheet.Buttons.Add(st.Left, st.Top, st.Width, st.Height)
With button
.OnAction = "GoToIssue.GoToIssue"
.Caption = "Go To Source"
.Name = "Button" & sauce
End With
Next sauce
Application.ScreenUpdating = True
End Sub
这里是 returns 单击按钮后的行 ID 的块:
Sub GoToIssue()
Dim b As Object
Dim myrow As Integer
Dim hunt As String
Set b = ActiveSheet.Buttons(Application.Caller)
With b.TopLeftCell
myrow = .Row
End With
hunt = Worksheets("Dummy").Range("F" & myrow).Value
'MsgBox hunt
End Sub
感谢您的宝贵时间和帮助。
您可以使用这个功能:
Public Function FindButtonRow(btn As Object) As Long
Dim cell As Excel.Range
'-------------------------------------------------
Set cell = btn.TopLeftCell
Do While cell.EntireRow.Hidden
Set cell = cell.Offset(1, 0)
Loop
FindButtonRow = cell.row
End Function
检查TopLeftCell
方法返回的单元格是否不在隐藏行中。如果是,该函数会尝试下面的单元格,依此类推,只要它从未隐藏的行中找到单元格即可。
您可以在您的子程序中使用它 GoToIssue
就像这样:
Sub GoToIssue()
Dim b As Object
Dim myrow As Integer
Dim hunt As String
Set b = ActiveSheet.Buttons(Application.Caller)
myrow = FindButtonRow(b)
hunt = Worksheets("Dummy").Range("F" & myrow).Value
'MsgBox hunt
End Sub