Code To Open Files Within Folder Make Changes And Close

Dazzawm

Well-known Member
Joined
Jan 24, 2011
Messages
3,748
Office Version
  1. 365
Platform
  1. Windows
I need a code that will open dozens of files within a folder in my desktop and if what is found in column A in the active sheet in column AG in each file then it needs to be changed to what is in column B as below. Thanks

Excel 2010
AB
1Column AGChanges To
2CentreCentre
3Centre-LHLH Centre
4Centre-RHRH Centre
5FRFR
6FR Inner-LHFR LH Inner
7FR Inner-RHFR RH Inner
8FR Lower-LHFR LH Lower
9FR Lower-RHFR RH Lower
10FR Outer-LHFR LH Outer
11FR Outer-RHFR RH Outer
12FR UpperFR Upper
13FR Upper-LHFR LH Upper
14FR Upper-RHFR RH Upper
15FR-DSFR DS
16FR-InnerFR Inner
17FR-LHFR LH
18FR-OuterFR Outer
19FR-PSFR PS
20FR-RHFR RH
21InnerInner
22Inner-LHLH Inner
23Inner-RHRH Inner
24LHLH
25OuterOuter
26Outer-LHLH Outer
27Outer-RHRH Outer
28RHRH
29RRRR
30RR Inner-LHRR LH Inner
31RR Inner-RHRR RH Inner
32RR LH+RHRR LH+RH
33RR-InnerRR Inner
34RR-LHRR LH
35RR-OuterRR Outer
36RR-RHRR RH

<colgroup><col style="width: 25pxpx"><col><col></colgroup><thead>
</thead><tbody>
</tbody>
Sheet1
So looking at row 3 if Centre-LH is found in column AG of any of the files then it needs to change it to LH Centre, save and close each file and so on...
 
Last edited:
Thanks Tony the first part seems to work great. The second part is not quite what I need. If any word(s) are found in column AG that are not in the list in column A then I would like to know the file it is in and the word(s) starting in column C.

So if the word 'thanks' is found in AG in the file named 'Sample' and it is not in the list in column A (which it is not in this case) then the file name of Sample will be in C1 and the word 'thanks' will be underneath and so on with any other words found in other files. We are almost there and if this could be done it would be great. Thanks.
 
Upvote 0

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Try this....

Code:
Sub CheckSheets()


    Dim fDialog         As Office.FileDialog
    Dim varFile         As Variant
    Dim wb              As Workbook
    Dim ws              As Worksheet
    Dim lookupRange     As Range
    Dim newRange        As Range
    Dim thisCell        As Range
    Dim r               As Long
    Dim nmr              As Long
    Dim lstCol          As Long
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual
    
    Set lookupRange = ThisWorkbook.Sheets("LookUp").Range("A1:A36")     '*****Set as you see fit
    Set newRange = ThisWorkbook.Sheets("LookUp").Range("B1:B36")         '*****Set as you see fit
    Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
    
    With fDialog
        .AllowMultiSelect = True
        .Title = "Import Files"
        .Filters.Clear
        .Filters.Add "Excel Documents", "*.xlsx"
        .Filters.Add "Excel Macro Documents", "*.xlsm"
        .Filters.Add "All Files", "*.*"
    End With
    
    If fDialog.Show Then
    'Check for cancel
        
        For Each varFile In fDialog.SelectedItems
        
            lstCol = Cells(1, Columns.Count).End(xlToLeft).Column + 1
             nmr = 2  'report no match row
    
            Set wb = Workbooks.Open(varFile)
            Set ws = wb.Sheets(1) '*****  uses first sheet or if all the same names then ws.Sheets("yoursheet")
            Cells(1, lstCol) = wb.Name
            For Each thisCell In ws.Range("AG2:AG" & ws.Range("C" & ws.Rows.Count).End(xlUp).Row)   '*****Set as you see fit  Column C
            r = 0
                On Error Resume Next
                r = Application.WorksheetFunction.Match(thisCell.Value, lookupRange, 0)
                On Error GoTo 0
                If r > 0 Then
                     thisCell.Value = Application.WorksheetFunction.Index(newRange, r)
                 Else
                     '**** Mark the word as having been found in AG but not in change list
                     If Not thisCell = "" Then Application.WorksheetFunction.Index(lookupRange.Offset(0, lstCol - 1), nmr, 1) = thisCell
                     nmr = nmr + 1
                End If
        
            Next thisCell
            
            Application.Calculate
            wb.Close True
        
        Next varFile
    
    End If


    Application.Calculation = xlCalculationSemiautomatic
    Application.ScreenUpdating = True


End Sub
 
Upvote 0
Thanks Tony almost perfect! When it finds a word in a file not listed in column A it lists it every time it is not in the file so in this example if the word 'thanks' is listed 100 times in the Sample file it lists it 100 times in column C. Could it be done so it is listed just once and the same with the next word that may be found in that file. If its too tricky then no problem.
 
Upvote 0
Dazza,

This is my final fling before bed....

Code:
Sub CheckSheets()


    Dim fDialog         As Office.FileDialog
    Dim varFile         As Variant
    Dim wb              As Workbook
    Dim ws              As Worksheet
    Dim lookupRange     As Range
    Dim newRange        As Range
    Dim thisCell        As Range
    Dim r               As Long
    Dim x               As Long
    Dim nmr             As Long
    Dim lstCol          As Long
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual
    
    Set lookupRange = ThisWorkbook.Sheets("LookUp").Range("A1:A36")     '*****Set as you see fit
    Set newRange = ThisWorkbook.Sheets("LookUp").Range("B1:B36")         '*****Set as you see fit
    Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
    
    With fDialog
        .AllowMultiSelect = True
        .Title = "Import Files"
        .Filters.Clear
        .Filters.Add "Excel Documents", "*.xlsx"
        .Filters.Add "Excel Macro Documents", "*.xlsm"
        .Filters.Add "All Files", "*.*"
    End With
    
    If fDialog.Show Then
    'Check for cancel
        
        For Each varFile In fDialog.SelectedItems
        
            lstCol = Cells(1, Columns.Count).End(xlToLeft).Column + 1
             nmr = 2  'report no match row
    
            Set wb = Workbooks.Open(varFile)
            Set ws = wb.Sheets(1) '*****  uses first sheet or if all the same names then ws.Sheets("yoursheet")
            Cells(1, lstCol) = wb.Name
            For Each thisCell In ws.Range("AG2:AG" & ws.Range("C" & ws.Rows.Count).End(xlUp).Row)   '*****Set as you see fit  Column C
            r = 0
                On Error Resume Next
                r = Application.WorksheetFunction.Match(thisCell.Value, lookupRange, 0)
                On Error GoTo 0
                If r > 0 Then
                     thisCell.Value = Application.WorksheetFunction.Index(newRange, r)
                 Else
                     '**** Mark the word as having been found in AG but not in change list
                     If Not thisCell = "" Then
                            x = 0
                            On Error Resume Next
                            x = Application.WorksheetFunction.Match(thisCell.Value, lookupRange.Offset(0, lstCol - 1), 0)
                            On Error GoTo 0
                            If x = 0 Then
                                Application.WorksheetFunction.Index(lookupRange.Offset(0, lstCol - 1), nmr, 1) = thisCell
                                nmr = nmr + 1
                            End If
                     End If
                End If
        
            Next thisCell
            
            Application.Calculate
            wb.Close True
        
        Next varFile
    
    End If


    Application.Calculation = xlCalculationSemiautomatic
    Application.ScreenUpdating = True


End Sub

ZZZZZZ..... ZZZZZZZZZ....ZZZZZZZZ
 
Upvote 0
I appreciate all your hard work especially this late. I will try it in the morning.
 
Upvote 0
Hi Tony, tried this morning and the second part doesnt work at all. It lists a couple of the words that is in one of the files that is not in column A but it lists them over the data in column B and doesnt give the name of the file in row 1. Also it hasnt given some words from other files. Dont worry the code in post 12 seems to be ok i will just do a manual 'remove duplicates' on each column at the end. I thank you for all your time and hard work.

Thank you also Adam and next time your in Soton drop me a PM and we will meet for a bevvy.
 
Upvote 0
Dazza,

Sorry for late response but have been out all day.

Previous code tested ok here on limited data but I can now see how it might be lacking if you have many un-matched words.
Re the printing in column B. The code requires that there be a header of some sort in B1 so that the printing starts at C1. Could be anything, XXXXXXX, just as long as it is not blank.


Excel 2007
BC
1!!!Must Not be BLANK"!!Dazza.xlsx
2Centrefrtyu
3LH Centrexxxxx
4RH CentreRR Outer
5FRRR RH
6FR LH Innerzzz
7FR RH Inneraa1
8FR LH Loweraa2
9FR RH Loweraa3
Lookup


If it's not too late, I feel the below code should now be aok. I would be interested to know if it works for real at your end.

Code:
Sub CheckSheets()


    Dim fDialog         As Office.FileDialog
    Dim varFile         As Variant
    Dim wb              As Workbook
    Dim ws              As Worksheet
    Dim lookupRange     As Range
    Dim newRange        As Range
    Dim thisCell        As Range
    Dim r               As Long
    Dim x               As Long
    Dim nmr             As Long
    Dim lstCol          As Long
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual
    
    Set lookupRange = ThisWorkbook.Sheets("LookUp").Range("A1:A36")     '*****Set as you see fit
    Set newRange = ThisWorkbook.Sheets("LookUp").Range("B1:B36")         '*****Set as you see fit
    Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
    
    With fDialog
        .AllowMultiSelect = True
        .Title = "Import Files"
        .Filters.Clear
        .Filters.Add "Excel Documents", "*.xlsx"
        .Filters.Add "Excel Macro Documents", "*.xlsm"
        .Filters.Add "All Files", "*.*"
    End With
    
    If fDialog.Show Then
    'Check for cancel
        
        For Each varFile In fDialog.SelectedItems
        
            lstCol = Cells(1, Columns.Count).End(xlToLeft).Column + 1
             nmr = 2  'report no match row
    
            Set wb = Workbooks.Open(varFile)
            Set ws = wb.Sheets(1) '*****  uses first sheet or if all the same names then ws.Sheets("yoursheet")
            Cells(1, lstCol) = wb.Name
            For Each thisCell In ws.Range("AG2:AG" & ws.Range("C" & ws.Rows.Count).End(xlUp).Row)   '*****Set as you see fit  Column C
            r = 0
                On Error Resume Next
                r = Application.WorksheetFunction.Match(thisCell.Value, lookupRange, 0)
                On Error GoTo 0
                If r > 0 Then
                     thisCell.Value = Application.WorksheetFunction.Index(newRange, r)
                 Else
                     '**** Mark the word as having been found in AG but not in change list
                     If Not thisCell = "" Then
                            x = 0
                            On Error Resume Next
                            x = Application.WorksheetFunction.Match(thisCell.Value, ThisWorkbook.Sheets("Lookup").Range(Cells(2, lstCol), Cells(nmr + 1, lstCol)), 0)
                            On Error GoTo 0
                            If x = 0 Then
                               ThisWorkbook.Sheets("Lookup").Cells(nmr, lstCol) = thisCell
                                nmr = nmr + 1
                            End If
                     End If
                End If
        
            Next thisCell
            
            Application.Calculate
            wb.Close True
        
        Next varFile
    
    End If


    Application.Calculation = xlCalculationSemiautomatic
    Application.ScreenUpdating = True


End Sub
 
Upvote 0
I am sorry Tony but it is still not working. The first part of the code is fine, sorted. But the second is the stumbling block. Firstly it is not putting the name of the file in row 1 where data is found that is not in the list. Secondly all the words of the data found from each file is being put in column C and not one of each so it is stretching down 50000 rows instead of about 10!

I am trying it using 3 files. The first file has all the data that is coulmn A so it changes it to what is in B as it should. The next 2 files have none of the data that is in A so all the data that is in file 2 should be copied to column C (file 2 in C1) but only each word once, so in essence a 'remove all duplicates' needs performing on that column. And the same in column D with the third file with file 3 as a header in D1. I know it is being a headache for you but luckily enough I won't be using for 'real' until tomorrow, so that it is why I wanted to get it sorted the last couple of days. If you can get to the bottom of it then great, but if not like I say I could use your code in #12.

Thanks.
 
Upvote 0
Dazza,

It works absolutely fine for me at this end.
Column C post #17 shows various non matches reported once whereas there were 50 or so of each in the test AG column of file being converted.

Without testing it on your data I am at a loss as what more I can do.
 
Upvote 0
This is bizarre! I have created some small files to upload for you to look at and upon checking them the code runs as expected. But when I try it on my very large files (which are exactly laid out the same) the code fails? The files I am trying to use the code on has macros embedded in them but that shouldn't make a difference should it?
 
Upvote 0

Forum statistics

Threads
1,215,235
Messages
6,123,782
Members
449,123
Latest member
StorageQueen24

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top