+ Reply to Thread
Results 1 to 3 of 3

Is there a way to make my code shorter?

Hybrid View

  1. #1
    Registered User
    Join Date
    06-17-2013
    Location
    Denver, Colorado
    MS-Off Ver
    Excel 2010
    Posts
    37

    Is there a way to make my code shorter?

    Hi,

    I am still fairly new to VBA and have another question. I have a very repetitive code and am wondering if there is a way to make it cleaner by creating a variable or array or something. Here is my dilemmna:

    I have about 10 names. Each person has a macro assigned to them that filters a database by their name. I need to calculate functions for each particular person to see how they are performing. I have several functions I need to perform for each person. (The functions are all SUM, COUNTIF, and SUBTOTAL functions that pull info from another spreadsheet) To perform my code, I run each person's macro, then call the particular function. Its very repetitive so I wonder if there is a way to shrink it down.

    I have posted my code below. You can very clearly see the pattern. Imagine there are about 30 functions. Is there a way I don't have to type in each person's name like a hundred times? Especially if I want to add/delete 1 person in the future, editing the code becomes cumbersome. Thank you so much for your help!

        Application.Run "PERSONAL.XLSB!Bob"
        Call Function1
        
        Application.Run "PERSONAL.XLSB!Sally"
        Call Function1
        
        Application.Run "PERSONAL.XLSB!Sue"
        Call Function1
        
        Application.Run "PERSONAL.XLSB!David"
        Call Function1
        
        Application.Run "PERSONAL.XLSB!Mary"
        Call Function1
        
       Application.Run "PERSONAL.XLSB!Bob"
        Call Function2
        
        Application.Run "PERSONAL.XLSB!Sally"
        Call Function2
        
        Application.Run "PERSONAL.XLSB!Sue"
        Call Function2
        
        Application.Run "PERSONAL.XLSB!David"
        Call Function2
        
        Application.Run "PERSONAL.XLSB!Mary"
        Call Function2
    
       Application.Run "PERSONAL.XLSB!Bob"
        Call Function3
        
        Application.Run "PERSONAL.XLSB!Sally"
        Call Function3
        
        Application.Run "PERSONAL.XLSB!Sue"
        Call Function3
        
        Application.Run "PERSONAL.XLSB!David"
        Call Function3
        
        Application.Run "PERSONAL.XLSB!Mary"
        Call Function3

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Is there a way to make my code shorter?

    I would modify the individual macros to be a single macro that accepts a name as an argument. Then you could do

    Sub x()
        Dim asName()    As String
        Dim iFunc       As Long
        Dim iName       As Long
    
        asName = Split("Bob,Sally,David,Mary", ",")
    
        For iFunc = 1 To 3
            For iName = 0 To UBound(asName)
                FilterByName asName(iName)
                Run "Function" & iFunc
            Next iName
        Next iFunc
    End Sub
    
    Sub FilterByName(sName As String)
        ' ...
    End Sub
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Registered User
    Join Date
    06-17-2013
    Location
    Denver, Colorado
    MS-Off Ver
    Excel 2010
    Posts
    37

    Re: Is there a way to make my code shorter?

    Hi shg,

    I see what you are doing and I am definitely going to give this a try. The only problem is that all of my names are "wildcard" arguments that accept a certain string. Thanks for the starting point though!

+ 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