Click here to Skip to main content
15,896,111 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is a question about macro in MS Excel, I am currently doing a summary sheet for multiple excel files in a directory using VBA macro, please keep in mind that each excel in that particular directory has the same format; I have made researches about this, and found this MSDN article https://msdn.microsoft.com/en-us/library/office/gg549168(v=office.14).aspx[^] I am using this code from that artice:

VB
Sub MergeSelectedWorkbooks()
    Dim SummarySheet As Worksheet
    Dim FolderPath As String
    Dim SelectedFiles() As Variant
    Dim NRow As Long
    Dim FileName As String
    Dim NFile As Long
    Dim WorkBk As Workbook
    Dim SourceRange As Range
    Dim DestRange As Range
    
    ' Create a new workbook and set a variable to the first sheet.
    Set SummarySheet = Workbooks.Add(xlWBATWorksheet).Worksheets(1)
    
    ' Modify this folder path to point to the files you want to use.
    FolderPath = "C:\Users\Peter\invoices\"
    
    ' Set the current directory to the the folder path.
    ChDrive FolderPath
    ChDir FolderPath
    
    ' Open the file dialog box and filter on Excel files, allowing multiple files
    ' to be selected.
    SelectedFiles = Application.GetOpenFilename( _
        filefilter:="Excel Files (*.xl*), *.xl*", MultiSelect:=True)
    
    ' NRow keeps track of where to insert new rows in the destination workbook.
    NRow = 1
    
    ' Loop through the list of returned file names
    For NFile = LBound(SelectedFiles) To UBound(SelectedFiles)
        ' Set FileName to be the current workbook file name to open.
        FileName = SelectedFiles(NFile)
        
        ' Open the current workbook.
        Set WorkBk = Workbooks.Open(FileName)
        
        ' Set the cell in column A to be the file name.
        SummarySheet.Range("A" & NRow).Value = FileName
        
        ' Set the source range to be A9 through C9.
        ' Modify this range for your workbooks. It can span multiple rows.
        Set SourceRange = WorkBk.Worksheets(1).Range("A9:C9")
        
        ' Set the destination range to start at column B and be the same size as the source range.
        Set DestRange = SummarySheet.Range("B" & NRow)
        Set DestRange = DestRange.Resize(SourceRange.Rows.Count, _
           SourceRange.Columns.Count)
           
        ' Copy over the values from the source to the destination.
        DestRange.Value = SourceRange.Value
        
        ' Increase NRow so that we know where to copy data next.
        NRow = NRow + DestRange.Rows.Count
        
        ' Close the source workbook without saving changes.
        WorkBk.Close savechanges:=False
    Next NFile
    
    ' Call AutoFit on the destination sheet so that all data is readable.
    SummarySheet.Columns.AutoFit
End Sub


My problem comes with the range part namely:
VB
Set SourceRange = WorkBk.Worksheets(1).Range("A9:C9")


This part covers all values from cell A9 to cell C9 but I need the data from cells L24, H19, and B29 (where these cells will never be blank) I tried using UNION to combine these ranges to a single range, but whenever I run the code it only gives the value of the first range I entered in UNION;

Please help me modify this code, I really dont know why the UNION does not give out those three values, where as far as I read UNION combines those ranges to a single range.


Thanks in advance
Posted

1 solution

Assuming that you want to copy data from L24, H19 and B29 cells in the source sheet into corresponding B, C and D cells in the destination sheet, try this:
VB
srcWorksheet.Range("L24, H19, B29").Copy dstWorksheet.Range("B" & NRow)


For further information, please see: Range.Copy method*[^]

Another way is to refer each cell this way:
VB
dstWorksheet.Range("B" & NRow) =srcWorksheet.Range("L24")
dstWorksheet.Range("C" & NRow) =srcWorksheet.Range("H19")
dstWorksheet.Range("D" & NRow) =srcWorksheet.Range("B29")


[EDIT]
I'd change it this way:

VB
Option Explicit

Sub MergeSelectedWorkbooks()
    Dim SrcBook As Workbook, DstBook As Workbook
    Dim SrcSheet As Worksheet, DstSheet As Worksheet
    Dim FolderPath As String
    Dim SelectedFiles() As Variant
    Dim NRow As Long, NFile As Long
    Dim FileName As String
    
    'Define destination Workbook and Worksheet
    ' Create a new workbook and set a variable to the first sheet.
    Set DstBook = Workbooks.Add(xlWBATWorksheet)
    Set DstSheet = SummaryBook.Worksheets(1)
    
    ' Modify this folder path to point to the files you want to use.
    FolderPath = "C:\Users\Peter\invoices\"
    
    ' Set the current directory to the the folder path.
    ChDrive FolderPath
    ChDir FolderPath
    
    ' Open the file dialog box and filter on Excel files, allowing multiple files
    ' to be selected.
    SelectedFiles = Application.GetOpenFilename( _
        filefilter:="Excel Files (*.xl*), *.xl*", MultiSelect:=True)
    
    ' NRow keeps track of where to insert new rows in the destination workbook.
    NRow = 1
    
    ' Loop through the list of returned file names
    For NFile = LBound(SelectedFiles) To UBound(SelectedFiles)
        ' Set FileName to be the current workbook file name to open.
        FileName = SelectedFiles(NFile)
        
        ' Open the current workbook.
        Set SrcBook = Workbooks.Open(FileName)
        
        ' Set the cell in column A to be the file name.
        DstSheet.Range("A" & NRow).Value = FileName
        DstShet.Range("B" & NRow) = SrcSheet.Range("L24")
        DstShet.Range("C" & NRow) = SrcSheet.Range("H19")
        DstShet.Range("D" & NRow) = SrcSheet.Range("B29")
        
        
        ' Increase NRow so that we know where to copy data next.
        NRow = NRow + 1
        
        ' Close the source workbook without saving changes.
        SrcWorkbook.Close savechanges:=False
    Next NFile
    
    ' Call AutoFit on the destination sheet so that all data is readable.
    DstSheet.Columns.AutoFit
    'save summary sheet
    DstBook.SaveAs FolderPath & "SummarySheet.xslx"
    
End Sub
 
Share this answer
 
v2
Comments
phyxian 5-Oct-15 3:26am    
i'm not that good at vba, exactly where in the code will i place that modification?
sorry for the trouble :)
Maciej Los 5-Oct-15 4:40am    
Please, see updated answer ;)
phyxian 5-Oct-15 22:29pm    
thank you very much... :)
Maciej Los 6-Oct-15 0:14am    
You're very welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900