+ Reply to Thread
Results 1 to 3 of 3

Pass Value/text from User Form to Macro

Hybrid View

tompa Pass Value/text from User... 05-24-2011, 12:36 PM
davegugg Re: Pass Value/text from User... 05-24-2011, 12:50 PM
tompa Re: Pass Value/text from User... 05-24-2011, 07:00 PM
  1. #1
    Forum Expert davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2010
    Posts
    1,884

    Re: Pass Value/text from User Form to Macro

    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.
    Is your code running too slowly?
    Does your workbook or database have a bunch of duplicate pieces of data?
    Have a look at this article to learn the best ways to set up your projects.
    It will save both time and effort in the long run!


    Dave

  2. #2
    Registered User
    Join Date
    05-24-2011
    Location
    Stockholm
    MS-Off Ver
    Excel 2007
    Posts
    2

    Re: Pass Value/text from User Form to Macro

    Excellent Dave! Thanks a lot. Will look up that article about variable scope

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1