在工作表上,我单击一个按钮,该按钮触发下面的Sub。可以读取单击的按钮位置的行和列,然后弹出一个消息框,其中包含同一行中隐藏单元格的内容。
执行此代码时,除非我添加ipf
语句,否则debug.print
变量始终保持为字符串“ initial”。我可以进行调试。打印任何变量(ipf
,row_no
,col_no
),然后将ipf
变量设置为正确。
我添加了debug.print之后,单击相关按钮以获取正确的结果,然后删除debug.print,它会一直起作用,直到我重新加载工作表为止。
在出现问题的情况下,我尝试过将其转换为字符串,但我根本不理解为什么这样做。
基本上,该工作表在每行上最多填充三个按钮,当单击这些按钮之一时,此代码将捕获被单击按钮的行和列以拉出该单元格的值,这将是三弦“初始”,“初步”或“最终”。使用该字符串(在ipf
变量中),将属于一种情况,即从该行上的另一个单元格中提取一个字符串以显示在消息框中。
除非存在debug.print
,否则ipf
变量总是 “初始”。
Public Sub showDefinitionMsgBox()
Dim obj As Object, _
row_no As Long, _
col_no As Long, _
wsMain As Worksheet 'This is the worksheet were using
' Get the row and column of the clicked button
Set obj = ActiveSheet.Buttons(Application.Caller)
row_no = obj.TopLeftCell.row
col_no = obj.TopLeftCell.column
' Grab the name (e.g. INitial, Prelim, Final)
' When a button is clicked, we pull the value from the cell under the button.
' It will be either "Initial", "Prelim" or "Final"
Set wsMain = ActiveWorkbook.Sheets("MainSheet")
Dim ipf As String
ipf = CStr(wsMain.Cells(row_no, col_no).Value)
' Get the element name
' This simply get's a string, which is the name of the row
Dim element As String
element = wsMain.Cells(row_no, 12).Value
' Withouth this, ipf is _always_ "initial"
Debug.Print ipf
' Pull the text from the dictionary
' 13 = init, 14 = prelim, 15 = final
' Using the above ipf variable, set the message text as the content
' of another cell in the wsMain sheet.
Dim message As String
Select Case ipf
Case "Initial":
message = wsMain.Cells(row_no, 13).Value
Case "Prelim":
message = wsMain.Cells(row_no, 14).Value
Case "Final":
message = wsMain.Cells(row_no, 15).Value
Case Else
message = "Cannot find that element type:"
End Select
' Show the message box
' @TODO: Update this to UserForm for more space
MsgBox message, vbOKOnly, element
End Sub
我希望正确的单元格中的值会出现在msgbox中,但是除非总是添加debug.print行,否则我总是从第一种情况中获取值。