|
-
Apr 27th, 2011, 11:37 AM
#1
Thread Starter
Addicted Member
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
-
Apr 27th, 2011, 04:09 PM
#2
Lively Member
Re: Excel - Make macro work for multiple files?
If you only want to open a single file, you can use:
vb Code:
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:
Application.Workbooks.Open (FileLocation)
-
Apr 27th, 2011, 05:00 PM
#3
Thread Starter
Addicted Member
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.
-
Apr 27th, 2011, 10:26 PM
#4
Re: Excel - Make macro work for multiple files?
filelocation is always an array, unless cancel is clicked, then false
vb Code:
for each myfile in filelocation ' do stuff 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
-
Apr 28th, 2011, 07:39 AM
#5
Thread Starter
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|