Making an Excel Macro function across two open workbooks with changing file names.

nnhood

New member
Joined
Oct 21, 2017
Messages
3
Reaction score
0
Points
0
Hello,

I do IT for a health screening company. Basically a data review person will download a list of people who participated in a health screening event. This file lists all the people that attended and their personal information. This file name changes.

The second file is almost identical but is generated by our health screeners from our own system. This file name also changes.

We do a comparison between these two files and I'm trying to create a macro that will work no matter what the file names are. I'm not a programmer at all, macros are easy, but above that I just don't have that skill set.

So the two files would be open Book1.xls and Book2.xls
In both file we're working with Last Name, First Name, Unique ID columns, but there are many more columns present.

Book1.xls
Add column to right of Unique ID (say Column "J" for this example)
Concatenate Last Name, First Name, Unique ID Columns

Book2.xls
Add two columns to the right of Unique ID (say Columns "I" and "J" for this example)
In first column, Concatenate Last Name, First Name, Unique ID Columns (same as in Book1)
In second column, Vlookup on first cell to the left which is the concatenated value, then highlight Column "J" in Book1.xls, then add comma 1, then add false.

=VLOOKUP(E2,[Book2.xlsx]Sheet1!$J:$J,1,FALSE)

It's just comparing the columns for accuracy.
I can make macro work, but then of course the file names change and that's the end of the story.

Just trying to make someones life easier as they do this all day long with different files.

Thanks for any help,
Matt
 

Attachments

  • Book1.xlsx
    9.5 KB · Views: 16
  • Book2.xlsx
    8.6 KB · Views: 13
Matt,

Something like this?

Code:
Option Explicit

Public wkbScreening   As Workbook
Public wkbLocalSystem As Workbook
Public iErrorCnt      As Integer

Sub Main()

   Dim lRowCntr As Long
   
   lRowCntr = 2  '*** If no headers set to 1 ***

  GetFiles  'Get the two required files
  If iErrorCnt > 0 Then Exit Sub
  
  '*** your processing code here for example ***
  Do
    Cells(lRowCntr, 10).Formula = "=VLOOKUP(E2,[" & wkbLocalSystem.Name & "]Sheet1!$J:$J,1,FALSE)"
    lRowCntr = lRowCntr + 1
  Loop Until Cells(lRowCntr, 1).Value = ""
  
  '*** If you wish to close the files when done ***
  Application.DisplayAlerts = False  '*** Don't prompt for confirmation ***
  wkbScreening.Close (True)
  wkbLocalSystem.Close (True)
  
End Sub

Sub GetFiles()

   Dim zFileItems()   As String
   Dim zAllowedExts   As String
   Dim lNoFiles       As Long
  
   ReDim zFileItems(1)
   zAllowedExts = "*.txt"

   lNoFiles = GetFileToOpen(zSelected:=zFileItems, _
                            zPrompt:="Select Screening File", _
                            bMulti:=False, _
                            zExts:="*.xls*")
   
   If lNoFiles > 0 Then
     Set wkbScreening = Workbooks.Open(zFileItems(1))
     ReDim zFileItems(1)
 
     lNoFiles = GetFileToOpen(zSelected:=zFileItems, _
                              zPrompt:="Select Local System File", _
                              bMulti:=False, _
                              zExts:="*.xls*")
     
     
     If lNoFiles > 0 Then
       Set wkbLocalSystem = Workbooks.Open(zFileItems(1))
     Else
       iErrorCnt = iErrorCnt + 1
     End If
     
   Else
     MsgBox "User pressed Cancel or X (Close Box).", vbOKOnly, _
            "No Files Selected:"
     iErrorCnt = iErrorCnt + 1
   End If

End Sub


'                        +--------------------+                 +----------+
'------------------------|   GetFileToOpen()  |-----------------| 01/15/14 |
'                        +--------------------+                 +----------+
'Called by  :
'Arguments  : zSelected - a String array declared empty & ReDimed to 1
'             zExts     - a list of allowed extensions for the filter
'                         Ex: "*.xlsx, *.xls, *.xlsm, *.xlsb"
'                         Note: Only Excel filetypes as function is written!
'             zMulti    - True allows multi select, False allows single select.
'             zFileFilter - Optional - used to limit the files shown by name
'                         pattern, EX: "CA*.xls*" if ommited "*.xls*" will
'                         be used. Note: using "*.*" will over ride the zExts
'                         filter! You can also specify a drive/path to set
'                         the initial folder displayed.
'Notes      : You can uncomment the .Title line and supply your own
'             dialog box title and add an argument if you want to pass it!
'             You can uncomment the .ButtonName to supply a custom OK button
'             caption which can also be passed by argument if desired.

Function GetFileToOpen(ByRef zSelected, zPrompt As String, zExts As String, _
                             bMulti As Boolean, _
                       Optional zFileFilter As Variant) As Long

    Dim fd             As FileDialog
    Dim lCnt           As Long
    
    If IsMissing(zFileFilter) Then zFileFilter = "*.xls*"
    Set fd = Application.FileDialog(msoFileDialogFilePicker)
    With fd
        .Filters.Clear   '*** Clear old filters just precautionary ***
        .Filters.Add "Spreadsheets", zExts, 1
        .InitialFileName = zFileFilter  '*** File Name Filter control. ***
        .Title = zPrompt
'        .ButtonName = "OK button caption"
        .AllowMultiSelect = bMulti 'Note: if not specified defaults to True!
        
        '.Show  Returns: -1 if Open button or 0 if Cancel button is pushed!
        If .Show = -1 Then
        
          ReDim zSelected(.SelectedItems.Count) 'Make array the proper size.
          
          For lCnt = 1 To .SelectedItems.Count 'Load the array with selections.
             zSelected(lCnt) = .SelectedItems.Item(lCnt)
          Next lCnt
        
        End If
        
        GetFileToOpen = .SelectedItems.Count
        
    End With   'fd
    
End Function 'GetFileToOpen

Note: This code would go in a separate "Master" workbook!

HTH :cool:
 
Wow thanks HTH! That's a lot of work, I really appreciate it sir.
Where do I insert this code?

I created a new macro enabled workbook and named it Master and pasted the code using the visual basic editor and saved it.

Thanks so much!
Matt
 
Last edited:
Matt,

The code should be inserted into a New or Existing Module. Do NOT place it in a Sheet or Workbook module!

HTH (Hope this Helps) :cool:
 
I made an entire new workbook and put it in there.
I see now I have two Macros Getfiles and Main.
Do I just run those and open up my two files that I need to compare?

Thanks,
Matt
 
Back
Top