I have a bit of a problem. The following code does everything, except actually bring the data from the source to the destination.
-What its supposed to do:
It is intended to open a directory listing and allow selection of specific workbook/s to be processed.
Then to collect data from specific cells in each workbook selected on a specified sheet.
Then store the collected data to the destination workbook.
Then close the source workbook and move onto the next selected workbook until the end of the selected workbooks, doing the same thing.
-What happens:
1. The directory opens and allows selection of the source workbooks.
2. It goes through until all workbooks selected have been processed and says Data import completed.
3. It does not import any data.
I have watched the rng variable during the process and it remains at 'nothing'. It appears that the data source is not actually being addressed.
Does anyone have any idea what I am doing wrong here?
'If at first you don't succeed, then your hammer is below specifications'
-What its supposed to do:
It is intended to open a directory listing and allow selection of specific workbook/s to be processed.
Then to collect data from specific cells in each workbook selected on a specified sheet.
Then store the collected data to the destination workbook.
Then close the source workbook and move onto the next selected workbook until the end of the selected workbooks, doing the same thing.
-What happens:
1. The directory opens and allows selection of the source workbooks.
2. It goes through until all workbooks selected have been processed and says Data import completed.
3. It does not import any data.
I have watched the rng variable during the process and it remains at 'nothing'. It appears that the data source is not actually being addressed.
Does anyone have any idea what I am doing wrong here?
Code:
Sub importFPRptData()
Dim X As Long, Z As Variant, Y As Variant
Dim Bk As Workbook, Sh As Worksheet, Sh1 As Worksheet
Dim rng As Range
Dim rng1 As Range
Set Sh = Workbooks("FP Report.xlsm").Worksheets("Data") 'Destination
Application.ScreenUpdating = False
'Get the fields to be used for extracting data.
Z = Application.GetOpenFilename(filefilter:="Excel Files (*.xls*), *.xls*", MultiSelect:=True)
If Not IsArray(Z) Then
MsgBox "Nothing was selected"
Exit Sub
End If
For X = 1 To UBound(Z)
Set Bk = Workbooks.Open(Z(X))
On Error Resume Next
Set Sh1 = Bk.Worksheets(Sheet2) ' The data source sheet in the source report
On Error GoTo 0
If Not Sh1 Is Nothing Then
Set rng = Sh1.Range("I5") 'Name
Set rng1 = Sh.Cells(Rows.Count, 1).End(xlUp)(2)
If rng = "" Then
rng = "N/K"
rng1.Copy
rng1.PasteSpecial xlValues
Else
rng.Copy
rng1.PasteSpecial xlValues
End If
Set rng = Sh1.Range("I6") 'DOB
Set rng1 = Sh.Cells(Rows.Count, 2).End(xlUp)(2)
If rng = "" Then
rng = "N/K"
rng1.Copy
rng1.PasteSpecial xlValues
Else
rng.Copy
rng1.PasteSpecial xlValues
End If
Set rng = Sh1.Range("I8") 'Nationality
Set rng1 = Sh.Cells(Rows.Count, 3).End(xlUp)(2)
If rng = "" Then
rng = "N/K"
rng1.Copy
rng1.PasteSpecial xlValues
Else
rng.Copy
rng1.PasteSpecial xlValues
End If
End If
Bk.Close
Next X
MsgBox "The Data import is complete"
End Sub
'If at first you don't succeed, then your hammer is below specifications'