This has to do with the scope of the variable. I'd suggest Googling "Variable Scope" to find a good article that will help you understand all about variable scope. In a nutshell, the buggNR variable is exclusive to your CB_Hitta_Click() macro. Once that macro ends, the variable is nothing. The best way to get around this in your case should be to pass the variable as an argument in the HittaBug method.
Something like this:
UserForm
Private Sub CB_Hitta_Click()
Dim BuggNR As Integer
BuggNR = Int(Me.LB_BuggNr.Value)
MsgBox (BuggNR)
Call HittaBugg(BuggNR)
End Sub
Macro
Sub HittaBugg(BuggNR As Integer)
MsgBox (BuggNR)
For i = 1 To Cells(1, 1).CurrentRegion.Rows.Count
If Cells(i, 1).Value = BuggNR Then
rad = i
MsgBox (rad)
Exit Sub
End If
Next i
End Sub
This also brings up the importance of declaring all variables with a dim statement.
Bookmarks