Macro for folder Name reference

Chandresh

Board Regular
Joined
Jul 21, 2009
Messages
146
HI,all ,

I am using following macro for extracting the data from sub folders my macro is working fine.

just need a help if macro can update the reference of folder in column "Z" from where the data is copied.

Ex : if data is copied from Folder no - 1 then my reference should be - 1 in column Z

Thanks in advance.



Sub TDS()
Dim fNAME As String: fNAME = "03-TDS.xls"
Dim fPATH As String: fPATH = "C:\Users\chandresh.choudhary\Desktop\test Merge"
Dim FSO As Object: Set FSO = CreateObject("Scripting.FileSystemObject")
Dim FLD As Object: Set FLD = FSO.GetFolder(fPATH)
Dim SubFLDRS As Object: Set SubFLDRS = FLD.SubFolders
Dim SubFLD As Object
Dim wbMain As Workbook: Set wbMain = ThisWorkbook
Dim wbData As Workbook
Dim ws As Worksheet
Dim LR As Long
For Each SubFLD In SubFLDRS
Set wbData = Workbooks.Open(fPATH & SubFLD.Name & "" & fNAME)

For Each ws In ActiveWorkbook.Worksheets
LR = ws.Range("A" & ws.Rows.Count).End(xlUp).Row
ws.Range("A1:A" & LR).EntireRow.Copy
wbMain.Sheets(ws.Name).Range("A" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteValues
Next ws

Application.CutCopyMode = False
wbData.Close False
Next SubFLD
Set wbMain = Nothing
End Sub
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Re: Macro for forder Name reference

Try this

Code:
Sub TDS()
    Dim fNAME As String: fNAME = "01.xlsx" '"03-TDS.xls"
    Dim fPATH As String: fPATH = "C:\trabajo\" '"C:\Users\chandresh.choudhary\Desktop\test Merge"
    Dim FSO As Object: Set FSO = CreateObject("Scripting.FileSystemObject")
    Dim FLD As Object: Set FLD = FSO.GetFolder(fPATH)
    Dim SubFLDRS As Object: Set SubFLDRS = FLD.SubFolders
    Dim SubFLD As Object
    Dim wbMain As Workbook: Set wbMain = ThisWorkbook
    Dim wbData As Workbook
    Dim ws As Worksheet
    Dim LR As Long, lr2 As Long
    For Each SubFLD In SubFLDRS
        Set wbData = Workbooks.Open(fPATH & SubFLD.Name & "\" & fNAME)
        
        For Each ws In ActiveWorkbook.Worksheets
            LR = ws.Range("A" & ws.Rows.Count).End(xlUp).Row
            ws.Range("A1:A" & LR).EntireRow.Copy
            lr2 = wbMain.Sheets(ws.Name).Range("A" & Rows.Count).End(xlUp).Offset(1).Row
            wbMain.Sheets(ws.Name).Range("A" & lr2).PasteSpecial xlPasteValues
            wbMain.Sheets(ws.Name).Range("Z" & lr2).Resize(LR).Value = SubFLD.Name
        Next ws
        
        Application.CutCopyMode = False
        wbData.Close False
    Next SubFLD
    Set wbMain = Nothing
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,431
Messages
6,119,462
Members
448,899
Latest member
maplemeadows

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top