' -----------------------------------------------------------------------------------------
' Define variables
' -----------------------------------------------------------------------------------------
Dim strFilename As String
Dim strWorkbookName As String
Dim objInputWB As Workbook
Dim objExtractWB As Workbook
Dim oWB As Workbook
Dim lngLastRow As Long
' -----------------------------------------------------------------------------------------
' Get the name of the file to extract data to
' -----------------------------------------------------------------------------------------
strFilename = Application.GetOpenFilename(FileFilter:="Microsoft Excel, *.xls*", _
FilterIndex:=1, _
Title:="Select file to extract to:", _
MultiSelect:=False)
' -----------------------------------------------------------------------------------------
' Ensure we haven't selected an invalid filename
' -----------------------------------------------------------------------------------------
If strFilename = "False" Then Exit Sub ' We hit cancel
' -----------------------------------------------------------------------------------------
' Check to see if it is already open
' -----------------------------------------------------------------------------------------
strWorkbookName = VBA.Strings.Right(strFilename, VBA.Strings.Len(strFilename) - _
VBA.Strings.InStrRev(strFilename, Application.PathSeparator))
If strFilename = ThisWorkbook.Name Then ' We selected this workbook
MsgBox Prompt:="Invalid selection detected, cancelling data Extraction!", _
Buttons:=vbCritical, _
Title:="Data Extraction Error"
Exit Sub
End If
For Each oWB In Workbooks
If oWB.Name = strWorkbookName Then
Set objInputWB = oWB
Exit For
End If
Next oWB
Set oWB = Nothing
' -----------------------------------------------------------------------------------------
' If not already open, open it.
' -----------------------------------------------------------------------------------------
If objInputWB Is Nothing Then Set objInputWB = Workbooks.Open(Filename:=strFilename)
' -----------------------------------------------------------------------------------------
' Determine if our extract workbook (exportParts.xls) is open
' -----------------------------------------------------------------------------------------
For Each oWB In Workbooks
If oWB.Name = "exportParts.xls" Then
Set objExtractWB = oWB
Exit For
End If
Next oWB
Set oWB = Nothing
' -----------------------------------------------------------------------------------------
' If we don't find our extract workbook open, open it.
' -----------------------------------------------------------------------------------------
If objExtractWB Is Nothing Then
' -------------------------------------------------------------------------------------
' Determine where the file is located and open.
' -------------------------------------------------------------------------------------
strFilename = Application.GetOpenFilename(FileFilter:="Microsoft Excel, exportParts.xls", _
FilterIndex:=1, _
Title:="Select extract file:", _
MultiSelect:=False)
If strFilename = "False" Then Exit Sub
strWorkbookName = VBA.Strings.Right(strFilename, VBA.Strings.Len(strFilename) - _
VBA.Strings.InStrRev(strFilename, Application.PathSeparator))
If strFilename = ThisWorkbook.Name Then ' We selected this workbook
MsgBox Prompt:="Invalid selection detected, cancelling data Extraction!", _
Buttons:=vbCritical, _
Title:="Data Extraction Error"
objInputWB.Close SaveChanges:=False
Exit Sub
End If
Set objExtractWB = Workbooks.Open(Filename:=strFilename)
End If
' -----------------------------------------------------------------------------------------
' SPECIAL NOTE: For our purposes we are assuming that the data is going from and to
' Sheets(1) of each WB
' -----------------------------------------------------------------------------------------
With objExtractWB.Sheets(1)
' --------------------------------------------------------------------------------------
' Determine last line of column B
' --------------------------------------------------------------------------------------
lngLastRow = 3
Do Until .Range("B" & lngLastRow).Value = vbNullString
lngLastRow = lngLastRow + 1
Loop
lngLastRow = lngLastRow - 1
' --------------------------------------------------------------------------------------
' Transfer column B to AB of objInputWB
' --------------------------------------------------------------------------------------
.Range("B3:B" & lngLastRow).Copy
objInputWB.Sheets(1).Range("AB5").PasteSpecial
' --------------------------------------------------------------------------------------
' Determine last line of column B
' --------------------------------------------------------------------------------------
lngLastRow = 3
Do Until .Range("E" & lngLastRow).Value = vbNullString
lngLastRow = lngLastRow + 1
Loop
lngLastRow = lngLastRow - 1
' --------------------------------------------------------------------------------------
' Transfer column E to AC of objInputWB
' --------------------------------------------------------------------------------------
.Range("E3:E" & lngLastRow).Copy
objInputWB.Sheets(1).Range("AC5").PasteSpecial
' --------------------------------------------------------------------------------------
' Determine last line of column B
' --------------------------------------------------------------------------------------
lngLastRow = 3
Do Until .Range("N" & lngLastRow).Value = vbNullString
lngLastRow = lngLastRow + 1
Loop
lngLastRow = lngLastRow - 1
' --------------------------------------------------------------------------------------
' Transfer column B to AB of objInputWB
' --------------------------------------------------------------------------------------
.Range("N3:N" & lngLastRow).Copy
objInputWB.Sheets(1).Range("AD5").PasteSpecial
End With
MsgBox "Transfer Complete!"