Hi nagabhn
This Code is in the attached File "Industry-Mapping". The Code runs from this file with CTRL + x. The Code will ask you which File to open. Place both Files in the SAME Folder.
Option Explicit
Sub Extract_Industry()
Dim wb1 As Workbook, wb2 As Workbook
Dim ws1 As Worksheet, ws2 As Worksheet
Dim LR1 As Long, LR2 As Long, LC2 As Long
Dim Rng1 As Range
Dim cel1 As Range, c As Range
Dim myPath As String, fname As String
Set wb1 = ThisWorkbook
Set ws1 = wb1.Sheets("Sheet1")
myPath = wb1.Path & "\"
Application.ScreenUpdating = False
With ws1
.Range("D:D").Clear
LR1 = .Range("B" & .Rows.Count).End(xlUp).Row
.Range("B1:B" & LR1).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=.Range("D1"), Unique:=True
ActiveWorkbook.Names.Add Name:="Industry", RefersTo:= _
"=OFFSET(Sheet1!$D$2,0,0,(COUNTA(Sheet1!$D:$D)-1),1)"
Set Rng1 = .Range("Industry")
End With
With Application.FileDialog(msoFileDialogOpen)
.InitialFileName = myPath 'this is the default folder shown
.AllowMultiSelect = False
.Filters.Add "All Files", "*.*" 'everything
.Filters.Add "Excel Files", "*.xl*", 1 'default
.Show
If .SelectedItems.Count > 0 Then
fname = .SelectedItems(1)
Else
Exit Sub
End If
End With
Set wb2 = Workbooks.Open(fname)
Set ws2 = wb2.Sheets("Sheet1")
With ws2
LC2 = .Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious).Column
.Range(.Cells(1, 1), .Cells(1, LC2)).Copy
End With
With wb2
For Each cel1 In Rng1
If Not Evaluate("ISREF(" & cel1.Value & "!A1)") Then
Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = cel1.Value
Sheets(cel1.Value).Range("A1").PasteSpecial Paste:=8
Sheets(cel1.Value).Range("A1").PasteSpecial
Else
Sheets(cel1.Value).UsedRange.Offset(1, 0).ClearContents
End If
Next cel1
End With
For Each cel1 In ws1.Range("A2:A" & LR1)
Set c = ws2.Range("A:A").Find(cel1.Value, LookIn:=xlValues)
If Not c Is Nothing Then
ws2.Range(ws2.Cells(c.Row, "A"), ws2.Cells(c.Row, LC2)).Copy
With wb2.Sheets(cel1.Offset(0, 1).Value)
LR2 = .Range("A" & .Rows.Count).End(xlUp).Row + 1
.Range("A" & LR2).PasteSpecial
End With
End If
Next cel1
With ws1
.Range("D:D").Clear
End With
'wb2.Close True
Application.ScreenUpdating = True
End Sub
Bookmarks