ignoring files importing data

Forum home » Delegate support and help forum » Microsoft Excel VBA Training and help » Ignoring files when importing data from multiple spreadsheets

Ignoring files when importing data from multiple spreadsheets

resolvedResolved · Low Priority · Version 2016

Zeesha has attended:
Excel VBA Introduction course

Ignoring files when importing data from multiple spreadsheets

Hi,

I'm using the code below to import data from multiple excel files into one master file but would like to ignore any excel file that does not contain a worksheet called 'Data' within this process. Any advice will be much appreciated. Thanks and regards. Zeeshan


Sub ImportDataRaw() 'Import data from recording forms

Dim i As Integer
Dim lLastRow As Long
Dim strImportDir As String
Dim strImportFile As String
Dim wsRaw As Worksheet
Dim wsData As Worksheet
Dim src As Workbook

strImportDir = Application.ActiveWorkbook.Path & "\Import\"
strImportFile = Dir(strImportDir & "\*" & "xlsx")

Set wsRaw = Application.ThisWorkbook.Worksheets("Raw Data")

wsRaw.Activate

lLastRow = Cells(Rows.Count, 6).End(xlUp).Row

i = lLastRow + 1

Do While Len(strImportFile) > 0

Set src = Workbooks.Open(strImportDir & strImportFile)
Set wsData = src.Worksheets("Data")

ThisWorkbook.Activate 'Activate template

wsRaw.Range(wsRaw.Cells(i, 1), wsRaw.Cells(i, 12)).Value2 = wsData.Range(wsData.Cells(2, 1), wsData.Cells(2, 12)).Value2

wsRaw.Range(wsRaw.Cells(i, 13), wsRaw.Cells(i, 13)).Value2 = src.Name

Workbooks(strImportFile).Close savechanges:=False

i = i + 1 'Next data file

strImportFile = Dir

Loop

End Sub

RE: Ignoring files when importing data from multiple spreadsheet

Hi Zeesha,

Thank you for the forum question.

I have added 4 lines to the code and after the added lines I have typed "'added line!!" so you can find my new lines.

Dim i As Integer
Dim lLastRow As Long
Dim strImportDir As String
Dim strImportFile As String
Dim wsRaw As Worksheet
Dim wsData As Worksheet
Dim src As Workbook

Dim sh as Object 'added line!!

strImportDir = Application.ActiveWorkbook.Path & "\Import\"
strImportFile = Dir(strImportDir & "\*" & "xlsx")

Set wsRaw = Application.ThisWorkbook.Worksheets("Raw Data")

wsRaw.Activate

lLastRow = Cells(Rows.Count, 6).End(xlUp).Row

i = lLastRow + 1

Do While Len(strImportFile) > 0

Set src = Workbooks.Open(strImportDir & strImportFile)

For Each Sh in src.Worksheets 'added line!!
If Sh.Name="Data" GoTo EndHere 'added line!!
Next Sh 'added line!!

Set wsData = src.Worksheets("Data")

ThisWorkbook.Activate 'Activate template

wsRaw.Range(wsRaw.Cells(i, 1), wsRaw.Cells(i, 12)).Value2 = wsData.Range(wsData.Cells(2, 1), wsData.Cells(2, 12)).Value2

wsRaw.Range(wsRaw.Cells(i, 13), wsRaw.Cells(i, 13)).Value2 = src.Name

EndHere: 'added line!!

Workbooks(strImportFile).Close savechanges:=False

i = i + 1 'Next data file

strImportFile = Dir

Loop

End Sub





Kind regards

Jens Bonde
Microsoft Office Specialist Trainer

Tel: 0207 987 3777
STL - https://www.stl-training.co.uk
98%+ recommend us

London's leader with UK wide delivery in Microsoft Office training and management training to global brands, FTSE 100, SME's and the public sector

RE: Ignoring files when importing data from multiple spreadsheet

Hi Jens,

Thanks for your email. I get the following error message with this code - 'Run-time error message 9: Script out of range' when the procedure opens a file without a Data worksheet.

Debug points to the following code:
Set wsData = src.Worksheets("Data")

The code also does not seem to be importing the valid files with a Data worksheet any more.

I will be grateful if you can please have another look when you get a chance.

Thanks again

Regards
Zeeshan

RE: Ignoring files when importing data from multiple spreadsheet

Sorry Zeeshan,

I read your question wrong.

I have added a little bit more.

Try this and let me know if this work.

Dim i As Integer
Dim lLastRow As Long
Dim strImportDir As String
Dim strImportFile As String
Dim wsRaw As Worksheet
Dim wsData As Worksheet
Dim src As Workbook

Dim sh as Object 'added line!!

strImportDir = Application.ActiveWorkbook.Path & "\Import\"
strImportFile = Dir(strImportDir & "\*" & "xlsx")

Set wsRaw = Application.ThisWorkbook.Worksheets("Raw Data")

wsRaw.Activate

lLastRow = Cells(Rows.Count, 6).End(xlUp).Row

i = lLastRow + 1

Do While Len(strImportFile) > 0

Set src = Workbooks.Open(strImportDir & strImportFile)

For Each Sh in src.Worksheets 'added line!!
If Sh.Name="Data" Then 'added line!!
GoTo JustContinue 'added line!!
else 'added line!!
goTo EndHere 'added line!!
end if 'added line!!
Next Sh 'added line!!

JustContinue: 'added line!!

Set wsData = src.Worksheets("Data")

ThisWorkbook.Activate 'Activate template

wsRaw.Range(wsRaw.Cells(i, 1), wsRaw.Cells(i, 12)).Value2 = wsData.Range(wsData.Cells(2, 1), wsData.Cells(2, 12)).Value2

wsRaw.Range(wsRaw.Cells(i, 13), wsRaw.Cells(i, 13)).Value2 = src.Name

EndHere: 'added line!!

Workbooks(strImportFile).Close savechanges:=False

i = i + 1 'Next data file

strImportFile = Dir

Loop

End Sub






Kind regards

Jens Bonde
Microsoft Office Specialist Trainer

Tel: 0207 987 3777
STL - https://www.stl-training.co.uk
98%+ recommend us

London's leader with UK wide delivery in Microsoft Office training and management training to global brands, FTSE 100, SME's and the public sector

RE: Ignoring files when importing data from multiple spreadsheet

Sorry Zeeshan,

Ignore my last answer. I will be back with a right answer shortly.

Kind regards

Jens Bonde
Microsoft Office Specialist Trainer

Tel: 0207 987 3777
STL - https://www.stl-training.co.uk
98%+ recommend us

London's leader with UK wide delivery in Microsoft Office training and management training to global brands, FTSE 100, SME's and the public sector

RE: Ignoring files when importing data from multiple spreadsheet

Hi Zeeshan,

I hope I got the logic right this time.

Please let me know if it is working thanks.

Dim i As Integer
Dim lLastRow As Long
Dim strImportDir As String
Dim strImportFile As String
Dim wsRaw As Worksheet
Dim wsData As Worksheet
Dim src As Workbook

Dim sh as Object 'added line!!

strImportDir = Application.ActiveWorkbook.Path & "\Import\"
strImportFile = Dir(strImportDir & "\*" & "xlsx")

Set wsRaw = Application.ThisWorkbook.Worksheets("Raw Data")

wsRaw.Activate

lLastRow = Cells(Rows.Count, 6).End(xlUp).Row

i = lLastRow + 1

Do While Len(strImportFile) > 0

Set src = Workbooks.Open(strImportDir & strImportFile)

For Each Sh in src.Worksheets 'added line!!
If Sh.Name="Data" GoTo JustContinue 'added line!!
Next Sh 'added line!!

goTo EndHere 'added line!!

JustContinue: 'added line!!

Set wsData = src.Worksheets("Data")

ThisWorkbook.Activate 'Activate template

wsRaw.Range(wsRaw.Cells(i, 1), wsRaw.Cells(i, 12)).Value2 = wsData.Range(wsData.Cells(2, 1), wsData.Cells(2, 12)).Value2

wsRaw.Range(wsRaw.Cells(i, 13), wsRaw.Cells(i, 13)).Value2 = src.Name

EndHere: 'added line!!

Workbooks(strImportFile).Close savechanges:=False

i = i + 1 'Next data file

strImportFile = Dir

Loop

End Sub





Kind regards

Jens Bonde
Microsoft Office Specialist Trainer

Tel: 0207 987 3777
STL - https://www.stl-training.co.uk
98%+ recommend us

London's leader with UK wide delivery in Microsoft Office training and management training to global brands, FTSE 100, SME's and the public sector

RE: Ignoring files when importing data from multiple spreadsheet

Hi Jens,

Thanks very much for your reply.

Although that seems to be working much better, the code is adding a blank line in the master file for the file that has been ignored (i.e. for the file without a Data worksheet). I'm guessing this is perhaps because the i = i + 1 is after the EndHere statement.

Is there a quick and easy way to avoid a blank line being added to the master file for the file that does not have a Data worksheet?

Any advice will be much appreciated.

Thanks again

Regards
Zeeshan

RE: Ignoring files when importing data from multiple spreadsheet

Hi Zeesha,


yes move the line:
i = i + 1

above:

EndHere: 'added line!!

Kind regards

Jens Bonde
Microsoft Office Specialist Trainer

Tel: 0207 987 3777
STL - https://www.stl-training.co.uk
98%+ recommend us

London's leader with UK wide delivery in Microsoft Office training and management training to global brands, FTSE 100, SME's and the public sector

RE: Ignoring files when importing data from multiple spreadsheet

Hi Jens,

That seems to be working fine now, thanks very much for your help. It's really appreciated.

Regards
Zeeshan

 

Training courses

 

Training information:

Welcome. Please choose your application (eg. Excel) and then post your question.

Our Microsoft Qualified trainers will then respond within 24 hours (working days).

Frequently Asked Questions
What does 'Resolved' mean?

Any suggestions, questions or comments? Please post in the Improve the forum thread.


 

Excel tip:

Outlining - ungrouping rows or columns

Highlight want you want to ungroup and press ALT + SHIFT + right cursor arrow

View all Excel hints and tips


Server loaded in 0.06 secs.