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'?

[TABLE="width: 1765"]
<colgroup><col span="5"></colgroup><tbody>[TR]
[TD]Files[/TD]
[TD]Output Sheet Name[/TD]
[TD]Output Row[/TD]
[TD][/TD]
[TD]Date of File[/TD]
[/TR]
[TR]
[TD]C:\Documents\Example\20160101.csv[/TD]
[TD]File1[/TD]
[TD="align: right"]1[/TD]
[TD][/TD]
[TD="align: right"]20160101[/TD]
[/TR]
[TR]
[TD]C:\Documents\Example\20160102.csv[/TD]
[TD]File2[/TD]
[TD="align: right"]1[/TD]
[TD][/TD]
[TD="align: right"]20160102[/TD]
[/TR]
[TR]
[TD]C:\Documents\Example\20160103.csv[/TD]
[TD]File3[/TD]
[TD="align: right"]1[/TD]
[TD][/TD]
[TD="align: right"]20160103[/TD]
[/TR]
[TR]
[TD]C:\Documents\Example\20160104.csv[/TD]
[TD]File4[/TD]
[TD="align: right"]1[/TD]
[TD][/TD]
[TD="align: right"]20160104[/TD]
[/TR]
[TR]
[TD]C:\Documents\Example\20160105.csv[/TD]
[TD]File5[/TD]
[TD="align: right"]1[/TD]
[TD][/TD]
[TD="align: right"]20160105[/TD]
[/TR]
</tbody>[/TABLE]
[TABLE="width: 600"]
<colgroup><col><col><col><col><col></colgroup><tbody>[TR]
[TD][/TD]
[TD][/TD]
[TD="align: right"][/TD]
[TD][/TD]
[TD="align: right"]

[TABLE="width: 600"]
<colgroup><col><col><col><col><col></colgroup><tbody></tbody>[/TABLE]
[/TD]
[/TR]
</tbody>[/TABLE]
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
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.

Forum statistics

Threads
1,223,239
Messages
6,170,947
Members
452,368
Latest member
jayp2104

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