+ Reply to Thread
Results 1 to 4 of 4

Passing variable value from Private Sub to Module

Hybrid View

  1. #1
    Valued Forum Contributor
    Join Date
    11-20-2003
    MS-Off Ver
    2010, 2016
    Posts
    1,176

    Passing variable value from Private Sub to Module

    I am trying to pass a variable called "Filter" from a Private Sub to a Module but keeping coming up with a zero value in the Module. I tried to make the variable Global but that didn't seem to work. Any suggestions for a solution is appreciated. Thanks.

    Private Sub Code as follows:
    Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    
    Dim i               As Long
    Dim LastRow         As Long
    Dim Rep             As String
    Dim Color           As Long
    Dim Filter          As Long
        
        Filter = 1
    Global Variables as follows:
    Global SortByColumn As Long
    Global SortOrder As Long
    Global Filter As Long
    Module Code as follows:
    Sub MoodysSort()
    
    Dim i As Long
    Dim lngSortIndex As Long
    Dim EndRow As Long
    
        If Filter = 0 Then
            'do something
        Else
            'do something else
        End If
    Last edited by maacmaac; 08-12-2009 at 08:38 AM.

  2. #2
    Forum Contributor
    Join Date
    06-14-2008
    Posts
    153

    Re: Passing variable value from Private Sub to Module

    Hi,

    You are defining Filter in
    Private Sub Worksheet_BeforeDoubleClick"
    So when you say
    Filter = 1
    the value is assigned to the local variable and not the global one, and you will not get it in the module.

    Just remove
    Dim Filter          As Long
    from
    Private Sub Worksheet_BeforeDoubleClick
    and hopefully that will resolve the problem.

    Regards.
    Welcome to: http://www.exceldigest.com/myblog/
    "Excel help for the rest of us"

  3. #3
    Forum Contributor
    Join Date
    02-23-2006
    Location
    Near London, England
    MS-Off Ver
    Office 2003
    Posts
    770

    Re: Passing variable value from Private Sub to Module

    Personally I'd get rid of the Global variables unless you really need them (ie. can justify a reason for having them other than needing the values in more that one function), and just pass them from one function to the next when you need to.

    Option Explicit
    
    Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    
    Dim i               As Long
    Dim LastRow         As Long
    Dim Rep             As String
    Dim Color           As Long
    
    Dim SortByColumn As Long
    Dim SortOrder As Long
    Dim Filter As Long    
    
    Filter = 1
    SortByColumn = 12
    SortOrder = 2
    
    call MoodysSort(Filter, SortByColumn, SortOrder)
    Option Explicit
    
    Sub MoodysSort(filter As Long, sortByCol As Long, sortOrder As Long)
    Dim i As Long
    Dim lngSortIndex As Long
    Dim EndRow As Long
    
        If filter = 0 Then
            'do something
        Else
            'do something else
        End
    If you find the response helpful please click the scales in the blue bar above and rate it
    If you don't like the response, don't bother with the scales, they are not for you

  4. #4
    Valued Forum Contributor
    Join Date
    11-20-2003
    MS-Off Ver
    2010, 2016
    Posts
    1,176

    Re: Passing variable value from Private Sub to Module

    Both suggestions are working fine. Thanks.

+ 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