Dim Filesfolder As String
Sub datacollection()
x = 2
'Filesfolder = InputBox("Please enter the folder address where files are store")
'LoopThroughFiles
For a = 1 To Workbooks("Book5.xlsm").Sheets("Mapping").Range("H5000").End(xlUp).Row
Filename = Workbooks("Book5.xlsm").Sheets("Mapping").Range("H" & a).Value
Workbooks.Open ("C:\Users\amardeep-singh\Desktop\Lean Automation and Templates\CCR\Raw\" & Filename)
For b = 1 To Workbooks("Book5.xlsm").Sheets("Mapping").Range("c5000").End(xlUp).Row
Carfamily = Workbooks("Book5.xlsm").Sheets("Mapping").Range("c" & b).Value
For i = 1 To 17
Va = Workbooks("Book5.xlsm").Sheets("Mapping").Range("E" & i).Value
'Va = Workbooks("Book5.xlsx").Worksheets("Sheet1").Range(Cells(1, i)).Value
Range("A1:D5000").Find(What:=Carfamily).Select
Range(Selection, Selection.End(xlDown).End(xlDown).End(xlToRight).End(xlToRight).End(xlToRight)).Select
Range(Selection, Selection).UnMerge
On Error GoTo errorhandler
Range(Selection, Selection).Find(What:=Va).Select
Rowy = Range(Selection, Selection).Row
Coly = Range(Selection, Selection).Column
y = 4
For j = 1 To 13
Workbooks("Book5.xlsm").Sheets("Sheet1").Cells(x, y).Value = Cells(Rowy, Coly).Offset(0, 1).Value
y = y + 1
Coly = Coly + 1
Next j
Workbooks("Book5.xlsm").Sheets("Sheet1").Range("A100000").End(xlUp).Offset(1, 0).Value = Filename
Workbooks("Book5.xlsm").Sheets("Sheet1").Range("B100000").End(xlUp).Offset(1, 0).Value = Carfamily
Workbooks("Book5.xlsm").Sheets("Sheet1").Range("C100000").End(xlUp).Offset(1, 0).Value = Va
errorhandler: x = x + 1
Next i
Next b
Next a
End Sub
Asked
Active
Viewed 34 times
0
-
Getting error at this statement "Range(Selection, Selection).Find(What:=Va).Select" – adi rocks Aug 06 '21 at 12:17
-
Please add a description of what you tried and the error that you get directly in the description of the questions. – Jaco-Ben Vosloo Aug 06 '21 at 19:13
1 Answers
0
The statement Range(Selection, Selection).Find(What:=Va)
returns Nothing
if the value was not found. So nothing to .Select, hence the error.
Check the result of Find before select it:
Set f = Selection.Find(What:=Va)
If Not f Is Nothing Then f.Select
Side note: Range(Selection, Selection)
= Selection
in this case.
I also recommend that you pay attention to How to avoid using Select in Excel VBA

Алексей Р
- 7,507
- 2
- 7
- 18