Question : Combine worksheets from different workbooks into one workbook using VB.Net

Hi Experts.  Im struggling with a number of problems with Excel 2007 and VB2008/3.5.  The main one is that I need to get sheets from three different workbooks, renamed and then combined into a seperate workbook.  This is the entire sheet, not ranges from a sheet.  Does anyone have an idea for this?  Many Thanks in advance

Answer : Combine worksheets from different workbooks into one workbook using VB.Net

I have attached some code that will complete the task.  Let me know how this works.  The code runs by going through a list of file paths that you provide in a range of cells.  This path must start with the drive location and go all the way down to the filename with its extention.

Example:

C:\Users\Matthew Sheskey\Desktop\ExcelImport.xlsx

You can provide as many as you would like.  The only restriction here is Excel can only handle 250ish sheets.

You mentioned the sheets you have only have one worksheet.  I structured the code to take all of the worksheets in the workbook just in case you want to add more/take more sheets later.  This is easily changed to take only 1 sheet if thats the desired functionality.

The code pastes values now.  Let me know if you want me to change the code to paste everything including formatting.

The workbooks must be closed when you run this macro.  I am working on some changes right now to check whether they are already open and from there only close the ones that were not already open.  I am also working on some code to take the filenames and sheet names and combine them into naming the worksheet in the new workbook.  This may not work because sheet names are capped at 32 characters.  I thought this functionality was needed after testing.  I found the consolidated workbook had a dozen or so sheets and was hard to keep track of them.  Let me know if you do not want this.

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
Sub getSheetsFromBooks()

Dim file, fileNames As Range 'range of file names
Dim ws, newWS, currentWorksheet As Worksheet
Dim assemblyWorkbook, dataWorkbook As Workbook
Dim dataWorkbookName As String 'will use this to name the sheets in new WB
Dim screenUpdate, displayAlerts As Boolean

Set fileNames = Application.Selection
Set assemblyWorkbook = Application.ActiveWorkbook
Set currentWorksheet = Application.ActiveSheet

screenUpdate = Application.ScreenUpdating
Application.ScreenUpdating = False

displayAlerts = Application.displayAlerts
Application.displayAlerts = False

'loop to iterate over file names
For Each file In fileNames
    
    Set dataWorkbook = Workbooks.Open(file.Value)
    dataWorkbook.Activate
    
    'loop to copy worksheets
    For Each ws In ActiveWorkbook.Worksheets
        
        ws.Activate
        Cells.Copy
        assemblyWorkbook.Activate
        Set newWS = Worksheets.Add(After:=Sheets(Sheets.Count))
        Range("A1").Activate
        ActiveCell.PasteSpecial Paste:=xlPasteValues
        
        'naming the worksheets
        'With newWS
        '    .Name = dataWorkbookName
        'End With
        
        Range("A1").Select
        dataWorkbook.Activate

    Next
    'close workbook
    dataWorkbook.Close
Next

'go back to the worksheet you started the program on
currentWorksheet.Activate

Application.CutCopyMode = False
Application.ScreenUpdating = screenUpdate
Application.ScreenUpdating = displayAlerts

Set file = Nothing
Set fileNames = Nothing
Set ws = Nothing
Set newWS = Nothing
Set assemblyWorkbook = Nothing
Set dataWorkbook = Nothing

End Sub
Random Solutions  
 
programming4us programming4us