VBA - Help adding filename to macro that pulls data from workbook

whazzzzzupp17

New Member
Joined
Jul 23, 2018
Messages
21
Hello, trying to modify my VBA script to include the filename within the range of data that I'm copying from multiple workbooks to my main workbook.

I'm currently pasting the data in column B, but would like column A to include the workbook name within each cell.

VBA Code:
Sub CopyFolderFiles()
    
    Dim xRg As Range
    Dim xSelItem As Variant
    Dim xFileDlg As FileDialog
    Dim xFileName, xSheetName, xRgStr As String
    Dim xBook, xWorkBook As Workbook
    Dim xSheet As Worksheet
    
    On Error Resume Next
    Application.DisplayAlerts = False
    Application.EnableEvents = False
    Application.ScreenUpdating = False
    
    xSheetName = "Project"
    xRgStr = "BJ9:CE100"
    
    
    Set xFileDlg = Application.FileDialog(msoFileDialogFolderPicker)
    
        With xFileDlg
            If .Show = -1 Then
                xSelItem = .SelectedItems.Item(1)
                Set xWorkBook = ThisWorkbook
                Set xSheet = xWorkBook.Sheets("New Sheet")
                
                If xSheet Is Nothing Then
                    xWorkBook.Sheets.Add(after:=xWorkBook.Worksheets(xWorkBook.Worksheets.Count)).Name = "New Sheet"
                    Set xSheet = xWorkBook.Sheets("New Sheet")
                End If

                xFileName = Dir(xSelItem & "\*.xlsm", vbNormal)
                
                If xFileName = "" Then Exit Sub
                
                Do Until xFileName = ""
                   Set xBook = Workbooks.Open(xSelItem & "\" & xFileName)
                    Set xRg = xBook.Worksheets(xSheetName).Range(xRgStr)
                    xRg.Copy xSheet.Range("B65536").End(xlUp).Offset(1, 0)
                    
                    xFileName = Dir()
                    xBook.Close
                    
                Loop
            End If
        End With
    
    Application.DisplayAlerts = True
    Application.EnableEvents = True
    Application.ScreenUpdating = True

End Sub
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Try this

VBA Code:
Sub CopyFolderFiles()
  Dim xRg As Range
  Dim xSelItem As Variant
  Dim xFileDlg As FileDialog
  Dim xFileName, xSheetName, xRgStr As String
  Dim xBook As Workbook, xWorkBook As Workbook
  Dim xSheet As Worksheet
  Dim lr1 As Long, lr2 As Long
  
  On Error Resume Next
  Application.DisplayAlerts = False
  Application.EnableEvents = False
  Application.ScreenUpdating = False
  
  xSheetName = "Project"
  xRgStr = "BJ9:CE100"
  
  Set xFileDlg = Application.FileDialog(msoFileDialogFolderPicker)
  
  With xFileDlg
      If .Show = -1 Then
          xSelItem = .SelectedItems.Item(1)
          Set xWorkBook = ThisWorkbook
          Set xSheet = xWorkBook.Sheets("New Sheet")
          
          If xSheet Is Nothing Then
              xWorkBook.Sheets.Add(after:=xWorkBook.Worksheets(xWorkBook.Worksheets.Count)).Name = "New Sheet"
              Set xSheet = xWorkBook.Sheets("New Sheet")
          End If

          xFileName = Dir(xSelItem & "\*.xlsm", vbNormal)
          Do While xFileName <> ""
            Set xBook = Workbooks.Open(xSelItem & "\" & xFileName)
            Set xRg = xBook.Worksheets(xSheetName).Range(xRgStr)
            lr1 = xSheet.Range("B65536").End(xlUp).Row + 1
            xRg.Copy xSheet.Range("B" & lr1)
            lr2 = xSheet.Range("B65536").End(xlUp).Row
            xSheet.Range("A" & lr1 & ":A" & lr2).Value = xBook.Name
            xBook.Close False
            xFileName = Dir()
          Loop
      End If
  End With
  
  Application.DisplayAlerts = True
  Application.EnableEvents = True
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
Try this

VBA Code:
Sub CopyFolderFiles()
  Dim xBook As Workbook, xWorkBook As Workbook, xSheet As Worksheet
  Dim xFileName As Variant, xSheetName As String, xRgStr As String
  Dim xSelItem As String, xRg As Range, lr1 As Long, lr2 As Long
  
  On Error Resume Next
  Application.DisplayAlerts = False
  Application.EnableEvents = False
  Application.ScreenUpdating = False
  
  xSheetName = "Project"
  xRgStr = "BJ9:CE100"
  xSelItem = "C:\trabajo\books\"
  Set xWorkBook = ThisWorkbook
  Set xSheet = xWorkBook.Sheets("New Sheet")
  If xSheet Is Nothing Then
    xWorkBook.Sheets.Add(after:=xWorkBook.Worksheets(xWorkBook.Worksheets.Count)).Name = "New Sheet"
    Set xSheet = xWorkBook.Sheets("New Sheet")
  End If

  xFileName = Dir(xSelItem & "\*.xlsm", vbNormal)
  Do While xFileName <> ""
    Set xBook = Workbooks.Open(xSelItem & "\" & xFileName)
    Set xRg = xBook.Worksheets(xSheetName).Range(xRgStr)
    lr1 = xSheet.Range("B65536").End(xlUp).Row + 1
    xRg.Copy xSheet.Range("B" & lr1)
    lr2 = xSheet.Range("B65536").End(xlUp).Row
    xSheet.Range("A" & lr1 & ":A" & lr2).Value = xBook.Name
    xBook.Close False
    xFileName = Dir()
  Loop
  
  Application.DisplayAlerts = True
  Application.EnableEvents = True
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
Can I instead of copying the data from the different workbooks by row, place by column (identifying the last empty cell)?
 

Attachments

  • 1.png
    1.png
    69.3 KB · Views: 7
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,201
Members
453,022
Latest member
RobertV1609

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