Sub Import_Data()
Dim WB As Workbook
Dim WS As Worksheet
Dim Start_Row As Byte, Last_Row As Byte, Next_Row As Byte, Counter As Byte, C As Byte
Dim File As Variant
Dim Header As Range
'Import the data of each file into the NewData tab
With shNewDat
.Cells.Clear
.Activate
End With
File = Application.GetOpenFilename("Excel files (.xlsx), *.xlsx", , "Select your files", , True)
If IsArray(File) Then
For Counter = 1 To UBound(File)
Set WB = Workbooks.Open(File(Counter))
Set WS = WB.ActiveSheet
If Counter = 1 Then
WS.Range("B4").CurrentRegion.Copy shNewDat.Range("B1")
Last_Row = shNewDat.Range("B1").End(xlDown).Row
shNewDat.Range("A1") = "Company_ID"
WS.Range("C2").Copy
shNewDat.Range("A2:A" & Last_Row).PasteSpecial xlPasteValues
Else
Next_Row = shNewDat.Range("B1").End(xlDown).Row + 1
WS.Range("B5").CurrentRegion.Offset(1).Copy shNewDat.Range("B" & Next_Row)
Last_Row = shNewDat.Range("B1").End(xlDown).Row
WS.Range("C2").Copy
shNewDat.Range("A" & Next_Row, "A" & Last_Row).PasteSpecial xlPasteValues
End If
WB.Close False
Next Counter
End If
'Import the data from the NewData tab into the Summary tab
C = 1
Do Until shAll.Cells(1, C).Value = ""
Set Header = shNewDat.Rows(1).Find(what:=shAll.Cells(1, C).Value, LookIn:=xlValues, MatchCase:=False, lookat:=xlWhole)
shNewDat.Range(Cells(2, Header.Column), Cells(Range("A1").End(xlDown).Row, Header.Column)).Copy shAll.Cells(2, C)
Loop
End Sub
Asked
Active
Viewed 29 times
0

BigBen
- 46,229
- 7
- 24
- 40

Varun Kapur
- 17
- 4
-
1That means that the `Find` did not succeed. – BigBen Jan 12 '22 at 13:54
-
1Side note, see [this](https://stackoverflow.com/questions/8047943/excel-vba-getting-range-from-an-inactive-sheet) for why `shNewDat.Range(Cells(2, Header.Column)....` is problematic too. – BigBen Jan 12 '22 at 13:56
-
Thank you, BigBen. I managed to identify the issue. There was nothing wrong with the script. A value in shNewDat was incorrectly formatted and that was causing the issue. – Varun Kapur Jan 13 '22 at 05:30