Venz Kenworth De Chavez
New Member
Hi Kindly help with my problem of my macro. i am trying to create a merging file that also copying the file name. The file is working but the problem is when it copies the 2nd file, the previous filename is being replaced. Below is my code:
MOD EDIT: ADDED CODE TAGS. PLEASE USE [ CODE ] TAGS TO WRAP YOUR CODE.
Code:
Sub MergeFilesWithoutSpaces_working6()
Dim path As String, ThisWB As String
Dim wbDest As Workbook, shtdest As Worksheet
Dim filename As String, wkb As Workbook
Dim copyRng As Range, Dest As Range
Dim M_rows As Integer, M_columns As Integer
Dim lastR As Range, ws As Worksheet
Dim iRow As Integer, iCol As Integer, splitFile As Variant, sFile As String
Dim mergeObj As Object
ThisWB = ActiveWorkbook.Name
path = Worksheets("Control").txtPath.Value
Application.EnableEvents = False
Application.ScreenUpdating = False
Set shtdest = ActiveWorkbook.Sheets("Summary")
Set ws = ActiveWorkbook.Sheets("Summary")
filename = Dir(path & "\*.xls", vbNormal)
If Len(filename) = 0 Then Exit Sub
Do Until filename = vbNullString
If Not filename = ThisWB Then
Set wkb = Workbooks.Open(filename:=path & "\" & filename, UpdateLinks:=False)
'previous code
'month of June
With wkb.Sheets("June")
M_rows = 32767
M_columns = 7
Set copyRng = wkb.Worksheets("June").Range("A2").Resize(M_rows, M_columns)
Set Dest = shtdest.Range("A" & shtdest.Cells(rows.Count, 1).End(xlUp).Row + 1)
copyRng.Copy
Dest.PasteSpecial xlPasteFormats
Dest.PasteSpecial xlPasteValuesAndNumberFormats
Application.CutCopyMode = False 'Clear Clipboard
wkb.Close False
'splitFile = Split(sFile, "-")
'For iCol = 0 To UBound(splitFile)
'Sheets("Summary").Range("G2").Cells(iRow, iCol + 1) = splitFile(iCol)
Set lastR = ws.Range("G:G" & ws.Cells(rows.Count, 1)).End(xlUp)(2)
With Range("G:G").CurrentRegion
With .Resize(.rows.Count - 1).Offset(1)
lastR(1, 1).Resize(.rows.Count).Value = CreateObject("Scripting.FileSystemObject").GetBasename(filename)
End With
End With
'Next iCol
End With
Application.ScreenUpdating = True
End If
filename = Dir()
Loop
End Sub
MOD EDIT: ADDED CODE TAGS. PLEASE USE [ CODE ] TAGS TO WRAP YOUR CODE.
Last edited by a moderator: