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

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
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,223,947
Messages
6,175,563
Members
452,652
Latest member
eduedu

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