HELP!!! VBA Question - How can I make a macro jump to the next file I want to import

Miche25

New Member
Joined
Jan 5, 2016
Messages
24
On a weekly basis I import to an excel file, 5 csv files for the last 5 days from a folder that holders numerous files (to tell each file apart the file name is the date). Within my excel spreadsheet I have set up an admin tab showing the file path of the files I need to import, shown in rows (3 to 7). The macro looks at the file names to be imported and imports to sheets names 'File 1' through to 'File 5'. I update the dates of the files I want to import under the heading 'Date of file' the macro runs fine until there is less than 5 file names showing or the file is missing from the folder I am trying to import from. I have 2 questions

1) if there is less than 5 files to be imported, how do I reflect this in my code as current the macro reads For rep = 3 To 7, I need to say if the file name is missing move onto importing the next file
2) if the the file for a date is missing from the folder how to I get the code to show a message to reflect 'date xxxx is missing from the import'?

FilesOutput Sheet NameOutput RowDate of File
C:\Documents\Example\20160101.csvFile1120160101
C:\Documents\Example\20160102.csvFile2120160102
C:\Documents\Example\20160103.csvFile3120160103
C:\Documents\Example\20160104.csvFile4120160104
C:\Documents\Example\20160105.csvFile5120160105

<colgroup><col span="5"></colgroup><tbody>
</tbody>



<colgroup><col><col><col><col><col></colgroup><tbody></tbody>

<colgroup><col><col><col><col><col></colgroup><tbody>
</tbody>
Sub Test_ImportAllFiles()
For rep = 3 To 7


Dim file_name As String
Dim row_number As String
Dim output_sheet As String


file_name = Sheets("Admin").Range("A" & rep).Value
output_sheet = Sheets("Admin").Range("B" & rep).Value
row_number = Sheets("Admin").Range("C" & rep).Value

With Sheets(output_sheet).QueryTables.Add(Connection:="TEXT;" + file_name, Destination:=Sheets(output_sheet).Range("$A" + row_number))
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 850
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = False
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = True
.TextFileSpaceDelimiter = False
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With


Next rep


End Sub
 

Some videos you may like

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying

Watch MrExcel Video

Forum statistics

Threads
1,122,857
Messages
5,598,484
Members
414,242
Latest member
Lehel

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
Top