Macro to open file if not already in list

shelbinb1532

New Member
Joined
Aug 14, 2017
Messages
8
I have a macro that is designed to open files from a folder (Support Requests) and copy all the relevant data from each to a database file (zzMasterSupportDatabase).
It copies all the information needed onto the next empty row in the database.
Right now it is set to run for all files in Support Requests, and to list the file name in column A. I need to add in an If statement that stops the code from copying a file whose file name is already listed in column A.
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Code:
For i = 1 To 100
     If Range("A" & i).Value = Filename Then
     Else
     *copy code here*
      End If
Next i
 
Last edited:
Upvote 0
Hia & welcome to the board
Rather than using a loop try
Code:
If WorksheetFunction.CountIf(Columns(1), [COLOR=#ff0000]Wbk[/COLOR]) = 0 Then
    'Your code
End If
Where Wbk is the name of the workbook
 
Upvote 0
The code above does not change the functionality of the macro. It still pulls in data from every file in the folder, even those that are already in the database file.
I think I need a code that reads the file name from the folder, looks for that file name in column A, and if found, then skips to the next file name in the folder.
 
Upvote 0
I think that you should store the file names in an array. Then go through each value of that array, pasting whenever there is not a duplicate present. Regardless, the macro will not run if there the file name is already present which is what you wanted, right?
 
Upvote 0
This is what the worksheet looks like
[TABLE="width: 500"]
<tbody>[TR]
[TD]File Name[/TD]
[TD]Customer[/TD]
[TD]Distributor[/TD]
[TD]Dist #[/TD]
[TD]Product[/TD]
[TD]Amount[/TD]
[TD]Qty[/TD]
[TD]Location[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]john[/TD]
[TD]abc[/TD]
[TD]123[/TD]
[TD]A[/TD]
[TD]12[/TD]
[TD]2[/TD]
[TD]Here[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]jack[/TD]
[TD]def[/TD]
[TD]234[/TD]
[TD]B[/TD]
[TD]24[/TD]
[TD]3[/TD]
[TD]There[/TD]
[/TR]
</tbody>[/TABLE]

Every time we get a new file, that file is saved to a folder. Instead of opening every file to see the requests, we are importing all the data from these files into 1 master file. As is, when I run the code, even with the suggested modifications, it will copy file names 1 and 2 again as well as the new file that was added: resulting in this

[TABLE="width: 500"]
<tbody>[TR]
[TD]File Name[/TD]
[TD]Customer[/TD]
[TD]Distributor[/TD]
[TD]Dist #[/TD]
[TD]Product[/TD]
[TD]Amount[/TD]
[TD]Qty[/TD]
[TD]Location[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]john[/TD]
[TD]abc[/TD]
[TD]123[/TD]
[TD]A[/TD]
[TD]12[/TD]
[TD]2[/TD]
[TD]Here[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]jack[/TD]
[TD]def[/TD]
[TD]234[/TD]
[TD]B[/TD]
[TD]24[/TD]
[TD]3[/TD]
[TD]There[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]john[/TD]
[TD]abc[/TD]
[TD]123[/TD]
[TD]A[/TD]
[TD]12[/TD]
[TD]2[/TD]
[TD]Here[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]jack[/TD]
[TD]def[/TD]
[TD]234[/TD]
[TD]B[/TD]
[TD]24[/TD]
[TD]3[/TD]
[TD]There[/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]jill[/TD]
[TD]efg[/TD]
[TD]345[/TD]
[TD]C[/TD]
[TD]23[/TD]
[TD]2[/TD]
[TD]Anywhere[/TD]
[/TR]
</tbody>[/TABLE]

As you can see this will get problematic the more files we add to the folder. I don't know how adding an array somewhere in the workbook would help with this issue.
 
Upvote 0
Could you please post the code that you already have, it will make it a lot easier for us to help
 
Upvote 0
Private Sub Auto_Open()
Dim SummarySheet As Worksheet
Dim FolderPath As String
Dim NRow As Long
Dim FileName As String
Dim WorkBk As Workbook
Dim SourceRange As Range
Dim DestRange As Range
Dim i As Integer


' Create a new workbook and set a variable to the first sheet.
Set SummarySheet = ThisWorkbook.Sheets("Database")

' Modify this folder path to point to the files you want to use.
FolderPath = "H:\Reduce Pricing Response Time\Price Supports Master\Support Requests"

' NRow keeps track of where to insert new rows in the destination workbook.
'Insert data in the next empty row.
i = 3
Do While Cells(i, 1).Value <> ""
NRow = i + 1
i = i + 1
Loop


' Call Dir the first time, pointing it to all Excel files in the folder path.
FileName = Dir(FolderPath & "*.xl*")

' Loop until Dir returns an empty string.
Do While FileName <> ""
'Skip file names already in database.

' Open a workbook in the folder
Set WorkBk = Workbooks.Open(FolderPath & FileName)

' Set the cell in column A to be the file name.
SummarySheet.Range("A" & NRow).Value = FileName

' Set the source range to be B1.
' Modify this range for your workbooks.
' It can span multiple rows.
Set SourceRange = WorkBk.Worksheets(1).Range("b1")
' Set the destination range to start at column B and
' be the same size as the source range.
Set DestRange = SummarySheet.Range("B" & NRow)
Set DestRange = DestRange.Resize(SourceRange.Rows.Count, _
SourceRange.Columns.Count)
' Copy over the values from the source to the destination.
DestRange.Value = SourceRange.Value

' Set the source range to be B3:B9.
' Modify this range for your workbooks.
' It can span multiple rows.
Set SourceRange = WorkBk.Worksheets(1).Range("b3")
' Set the destination range to start at column C and
' be the same size as the source range.
Set DestRange = SummarySheet.Range("F" & NRow)
Set DestRange = DestRange.Resize(SourceRange.Rows.Count, _
SourceRange.Columns.Count)
' Copy over the values from the source to the destination.
DestRange.Value = SourceRange.Value
Set SourceRange = WorkBk.Worksheets(1).Range("b4")
Set DestRange = SummarySheet.Range("H" & NRow)
Set DestRange = DestRange.Resize(SourceRange.Rows.Count, _
SourceRange.Columns.Count)
DestRange.Value = SourceRange.Value
Set SourceRange = WorkBk.Worksheets(1).Range("b5")
Set DestRange = SummarySheet.Range("I" & NRow)
Set DestRange = DestRange.Resize(SourceRange.Rows.Count, _
SourceRange.Columns.Count)
DestRange.Value = SourceRange.Value
Set SourceRange = WorkBk.Worksheets(1).Range("b6")
Set DestRange = SummarySheet.Range("J" & NRow)
Set DestRange = DestRange.Resize(SourceRange.Rows.Count, _
SourceRange.Columns.Count)
DestRange.Value = SourceRange.Value
Set SourceRange = WorkBk.Worksheets(1).Range("b7")
Set DestRange = SummarySheet.Range("K" & NRow)
Set DestRange = DestRange.Resize(SourceRange.Rows.Count, _
SourceRange.Columns.Count)
DestRange.Value = SourceRange.Value
Set SourceRange = WorkBk.Worksheets(1).Range("b8")
Set DestRange = SummarySheet.Range("L" & NRow)
Set DestRange = DestRange.Resize(SourceRange.Rows.Count, _
SourceRange.Columns.Count)
DestRange.Value = SourceRange.Value
Set SourceRange = WorkBk.Worksheets(1).Range("b9")
Set DestRange = SummarySheet.Range("M" & NRow)
Set DestRange = DestRange.Resize(SourceRange.Rows.Count, _
SourceRange.Columns.Count)
DestRange.Value = SourceRange.Value

' Set the source range to be A13:Ni.
' Modify this range for your workbooks.
' It can span multiple rows.
i = 13
Do While Cells(i, 1).Value <> ""
Set SourceRange = WorkBk.Worksheets(1).Range("a" & i)
' Set the destination range to start at column J and
' be the same size as the source range.
Set DestRange = SummarySheet.Range("N" & NRow)
Set DestRange = DestRange.Resize(SourceRange.Rows.Count, _
SourceRange.Columns.Count)
' Copy over the values from the source to the destination.
DestRange.Value = SourceRange.Value
Set SourceRange = WorkBk.Worksheets(1).Range("a13:N" & i)
' Set the destination range to start at column J and
' be the same size as the source range.
Set DestRange = SummarySheet.Range("n" & NRow)
Set DestRange = DestRange.Resize(SourceRange.Rows.Count, _
SourceRange.Columns.Count)
' Copy over the values from the source to the destination.
DestRange.Value = SourceRange.Value

i = i + 1
Loop

' Increase NRow so that we know where to copy data next.
NRow = NRow + DestRange.Rows.Count

' Close the source workbook without saving changes.
WorkBk.Close savechanges:=False

' Use Dir to get the next file name.
FileName = Dir()
Loop


' Call AutoFit on the destination sheet so that all
' data is readable.
SummarySheet.Columns.AutoFit
End Sub
 
Upvote 0
Untested, but try the mod highlighted in red
Code:
Private Sub Auto_Open()
Dim SummarySheet As Worksheet
Dim FolderPath As String
Dim NRow As Long
Dim FileName As String
Dim WorkBk As Workbook
Dim SourceRange As Range
Dim DestRange As Range
Dim i As Integer


' Create a new workbook and set a variable to the first sheet.
Set SummarySheet = ThisWorkbook.Sheets("Database")

' Modify this folder path to point to the files you want to use.
FolderPath = "H:\Reduce Pricing Response Time\Price Supports Master\Support Requests"

' NRow keeps track of where to insert new rows in the destination workbook.
'Insert data in the next empty row.
i = 3
Do While Cells(i, 1).Value <> ""
NRow = i + 1
i = i + 1
Loop


' Call Dir the first time, pointing it to all Excel files in the folder path.
FileName = Dir(FolderPath & "*.xl*")

' Loop until Dir returns an empty string.
Do While FileName <> ""
'Skip file names already in database.
[COLOR=#ff0000]If WorksheetFunction.CountIf(SummarySheet.Columns(1), InStr(FileName, Find(FileName, ".") - 1)) = 0 Then[/COLOR]
' Open a workbook in the folder
Set WorkBk = Workbooks.Open(FolderPath & FileName)
 
Upvote 0
I am getting a type mismatch error when I add in the red code. I think it is because FileName is defined as a String but I'm not sure.
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,154
Members
453,021
Latest member
Justyna P

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