Quantcast
Channel: Active questions tagged excel - Stack Overflow
Viewing all articles
Browse latest Browse all 88150

Variable isn't set unless debug.print executed first

$
0
0

On my sheet, I click a button, the button fires the below Sub. Which is supposed to read the row and col of the clicked button location, then pop up a message box the with the content of a hidden cell on the same row.

When this code executes, the ipf variable always remains as the string "initial" unless I add a debug.print statement. I can debug.print any of the variables (ipf, row_no, col_no) and the ipf variable is set fine.

After I've added debug.print, clicked the relevant button to get the right result, then removed debug.print, it continues to work until I reload the sheet.

I've tried casting as a string in case that is the problem, but I simply do not understand why it's doing this.

Essentially, the sheet is filled with up to three buttons on each row, when one of those buttons is clicked, this code grabs the row and col of the clicked button to pull the value of the cell, which will be one of three strings; "Initial", "Prelim" or "Final". Using that string (in the ipf variable) it will fall into one of the cases to pull a string from another cell on that row to show in a message box.

Unless the debug.print is there, the ipf var is always"Initial".

Public Sub showDefinitionMsgBox()
    Dim obj As Object, _
        oGlobals As clsGlobals, _ ' Class that contains project-wide things. Access to sheets and often used variables.
        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 oGlobals = New clsGlobals
    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 = oGlobals.wsMain
    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

I expect the value from the correct cell to appear in the msgbox, but I always get the value from the first case, unless I add the debug.print line.


Viewing all articles
Browse latest Browse all 88150

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>