CBHodgetts
New Member
- Joined
- Nov 29, 2010
- Messages
- 7
Hi All I have manage to "cobble" some script to import a numner of files into a single consolidate list. In Coulmn A of the file produced, it inserts the full path and filename. Is there a way of only inserting the filename?
Code:
Many thanks in advance for any assistance, as this is driving me mad...
CBHodgetts
Code:
Code:
Private Declare Function SetCurrentDirectoryA Lib _
"kernel32" (ByVal lpPathName As String) As Long
Sub ChDirNet(szPath As String)
SetCurrentDirectoryA szPath
End Sub
Sub MergeSpecificWorkbooks2()
Dim MyPath As String
Dim SourceRcount As Long, Fnum As Long
Dim mybook As Workbook, BaseWks As Worksheet
Dim sourceRange As Range, destrange As Range
Dim rnum As Long, CalcMode As Long
Dim SaveDriveDir As String
Dim FName As Variant
' Set application properties.
With Application
CalcMode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
.EnableEvents = False
End With
SaveDriveDir = CurDir
' Change this to the path\folder location of the files.
ChDirNet "X:\Management Support\ADDO REPORTING PROJECT\ATB\(B) Phase Two Report Tracker & Issues Log"
[I][COLOR="Red"]FName = Application.GetOpenFilename(filefilter:="Excel Files (*.xl*), *.xl*", Title:="Choose Tracker Files", _
MultiSelect:=True)[/COLOR][/I]
If IsArray(FName) Then
' Add a new workbook with one sheet.
Set BaseWks = Workbooks.Add(xlWBATWorksheet).Worksheets(1)
rnum = 1
' Loop through all files in the myFiles array.
For Fnum = LBound(FName) To UBound(FName)
Set mybook = Nothing
On Error Resume Next
Set mybook = Workbooks.Open(FName(Fnum))
On Error GoTo 0
If Not mybook Is Nothing Then
On Error Resume Next
With mybook.Worksheets(2)
Set sourceRange = .Range("A5:N200")
End With
If Err.Number > 0 Then
Err.Clear
Set sourceRange = Nothing
Else
' If the source range uses all columns then
' skip this file.
If sourceRange.Columns.Count >= BaseWks.Columns.Count Then
Set sourceRange = Nothing
End If
End If
On Error GoTo 0
If Not sourceRange Is Nothing Then
SourceRcount = sourceRange.Rows.Count
If rnum + SourceRcount >= BaseWks.Rows.Count Then
MsgBox "There are not enough rows in the target worksheet."
BaseWks.Columns.AutoFit
mybook.Close savechanges:=False
GoTo ExitTheSub
Else
' Copy the file name in column A.
With sourceRange
[I][COLOR="Red"]BaseWks.Cells(rnum, "A"). _
Resize(.Rows.Count).Value = FName(Fnum)[/COLOR][/I]
End With
' Set the destination range.
Set destrange = BaseWks.Range("B" & rnum)
' Copy the values from the source range
' to the destination range.
With sourceRange
Set destrange = destrange. _
Resize(.Rows.Count, .Columns.Count)
End With
destrange.Value = sourceRange.Value
rnum = rnum + SourceRcount
End If
End If
mybook.Close savechanges:=False
End If
Next Fnum
' tidy up data
Cells.EntireColumn.AutoFit
Rows("1:1").Select
Selection.AutoFilter
Selection.AutoFilter Field:=2, Criteria1:="="
Rows("12:12").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Delete Shift:=xlUp
Selection.AutoFilter Field:=2
Selection.AutoFilter Field:=3, Criteria1:="Drop"
Rows("12:12").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Delete Shift:=xlUp
Range("B346").Select
Selection.AutoFilter Field:=3
ActiveWindow.SmallScroll Down:=-15
BaseWks.Columns.AutoFit
BaseWks.Rows.AutoFit
End If
ExitTheSub:
' Restore the application properties.
With Application
.ScreenUpdating = True
.EnableEvents = True
.Calculation = CalcMode
End With
ChDirNet SaveDriveDir
End Sub
Many thanks in advance for any assistance, as this is driving me mad...
CBHodgetts