Results 1 to 5 of 5

Thread: Excel - Make macro work for multiple files?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 2008
    Location
    Colorado
    Posts
    193

    Excel - Make macro work for multiple files?

    I have the following code that I would like to change. I initially wrote this to work with a specific couple of files, and I'm finding that I need to have it be able to work for multiple files for both the dnc/ccl lists. I've never been good at code that allows you to select files from an open dialog. Can someone help me with this, either with sample code for selecting files, or by tweaking the below?

    Code:
    Sub Cleanup()
    Dim sTmpEmail As String 'Email address we're checking
    Dim iDNClist As Integer 'Loop counter for Do Not Call List
    Dim iCustList As Integer 'Loop coutner for ColoradoCustomers spreadsheet
    Dim dnc As Worksheet 'Do Not Call worksheet
    Dim ccl As Worksheet 'Customer list worksheet
    Set dnc = Workbooks("DoNotMailBook.xlsm").Worksheets("DoNotMailSheet")
    Set ccl = Workbooks("Customer ListBook.xlsx").Worksheets("CustomerListSheet")
    Dim dncS As Range 'Do Not Call Cell A1
    Dim cclS As Range 'Customer List cell A1
    Set dncS = Workbooks("DoNotMailBook.xlsm").Worksheets("DoNotMailSheet").Range("A1")
    Set cclS = Workbooks("Customer ListBook.xlsx").Worksheets("CustomerListSheet").Range("A1")
    Dim dncC As Integer 'Do Not Call last row
    Dim cclC As Integer 'Customer List last row
    
    dncC = dnc.Range("A65536").End(xlUp).Row
    cclC = ccl.Range("A65536").End(xlUp).Row
    Debug.Print dncC
    Debug.Print cclC
    
    'dnc.Range("A1").Font.Bold = False
    'ccl.Range("A1").Font.Bold = False
    
    Application.ScreenUpdating = False
    For iDNClist = 1 To dncC
        sTmpEmail = dncS.Offset(iDNClist, 0).Value
        For iCustList = 1 To cclC
            If sTmpEmail = cclS.Offset(iCustList, 0).Value Then
                cclS.Offset(iCustList, 0).EntireRow.Delete
                cclC = cclC - 1
            Else
            End If
        Next iCustList
    Next iDNClist
    Application.ScreenUpdating = True
    
    End Sub

  2. #2
    Lively Member Vanquish2888's Avatar
    Join Date
    Sep 2007
    Posts
    104

    Re: Excel - Make macro work for multiple files?

    If you only want to open a single file, you can use:
    vb Code:
    1. FileLocation = Application.GetOpenFilename("Excel Files,*.xls", 1, "Select a file", , False)
    This simply returns a String of the path to the file, to open the file you must use:
    vb Code:
    1. Application.Workbooks.Open (FileLocation)

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jul 2008
    Location
    Colorado
    Posts
    193

    Re: Excel - Make macro work for multiple files?

    Thank you for that. I will see what that can do for me, but I'm looking for more than a single file... Is there a way to make "FileLocation" an array and contain multiple paths?

    Ultimately I want to be able to reference multiple files (most likely in a loop). What my code does is remove information contained on 1 list from another list. Lately, I've been finding that I need to remove data on multiple lists from multiple other lists.

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Excel - Make macro work for multiple files?

    filelocation is always an array, unless cancel is clicked, then false

    vb Code:
    1. for each myfile in filelocation
    2. ' do stuff
    3. next
    to select multiple files the last parameter of getopenfilename must be passed as true
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jul 2008
    Location
    Colorado
    Posts
    193

    Re: Excel - Make macro work for multiple files?

    Thank you. I will look into this in a little bit and then mark resolved if it's resolved.

Posting Permissions

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



Click Here to Expand Forum to Full Width