Copying column titles from different workbooks into new workbook

slimano

New Member
Joined
Apr 18, 2011
Messages
13
Hello,

I'm trying to copy data from 5 different worksbooks into a single new workbook. The code is as follows:

Code:
Sub tiso()

Dim fn, e, wb As Workbook
fn = Application.GetOpenFilename(FileFilter:="Microsoft excel files (*.xls), *.xls", _
                     Title:="Press CTRL Key to Select Multiple Files", MultiSelect:=True)
If Not IsArray(fn) Then Exit Sub
Set wb = Workbooks.Add
wb.SaveAs Filename:="C:\tiso\compiled.xls"


For Each e In fn
    With Workbooks.Open(e)
        With .Sheets(1)
            LastRow = Range("A65536").End(xlUp).Row
            Range("A2", Cells(LastRow, "O")).Copy
            wb.Sheets(1).Cells(wb.Sheets(1).Cells.SpecialCells(11).Row + 1, 1).PasteSpecial xlPasteValues
        End With
        .Close False
    End With
Next

Application.CutCopyMode = False
Set wb = Nothing

End Sub


My problem is with how to get the macro to copy the Row 1 which is present in all the 5 worksheets to appear on ONLY Row 1 in the new worksheet.


Excel Workbook
ABCDEFGHIJKLMNO
1Transaction Ref.Debit Acct NoDebit Acct NameDebit CurrencyDebit AmountCredit Their RefCredit Acct NoCredit CurrencyCOMMISSION.AMTCHARGE.AMTAmount DebitedAmount CreditedLocal Amount DebitedLocal Amt CreditedProcessing Date
Sheet1
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Try (untested):

Rich (BB code):
Sub tiso()
Dim fn, e, wb As Workbook
Dim LastRow As Long
Dim NextCell As Range
Dim First As Boolean
fn = Application.GetOpenFilename(FileFilter:="Microsoft excel files (*.xls), *.xls", _
                     Title:="Press CTRL Key to Select Multiple Files", MultiSelect:=True)
If Not IsArray(fn) Then Exit Sub
Set wb = Workbooks.Add
wb.SaveAs Filename:="C:\tiso\compiled.xls"
First = True
For Each e In fn
    With Workbooks.Open(e)
        With .Sheets(1)
            LastRow = Range("A65536").End(xlUp).Row
            Range("A2", Cells(LastRow, "O")).Copy
            Set NextCell = wb.Sheets(1).Cells(wb.Sheets(1).Cells.SpecialCells(11).Row + 1, 1)
            With NextCell
                .PasteSpecial xlPasteValues
                If First = True Then
                    First = False
                Else
                    .EntireRow.Delete
                End If
            End With
        End With
        .Close False
    End With
Next
Application.CutCopyMode = False
Set wb = Nothing
End Sub
 
Upvote 0
Thanks Andrew,

What i meant was that the column headings should show in Row1 of the new workbook only as well as add on the data from the 5 worksheets starting at Row2.

My code was compiling all the data into the new workbook but left Row1 blank that's what i seek to correct.

The new code does not make the Row1 appear, rather Row1 of all the other 4 workbooks except the very first one are blanked out.

Thanks.
 
Upvote 0
Andrew thanks,

When i run that, it worked but started populating the new workbook from Row2.

Data from workbook1 and workbook2 followed each other smoothly but subsequent data sets left a blank before adding on new data from another workbook.

So i edited to:

Code:
 Set NextCell = wb.Sheets(1).Cells(wb.Sheets(1).Cells.SpecialCells(11).[COLOR="Red"]Row[/COLOR], 1)

However, that started from Row1 and eliminated the blanks that appeared earlier, but the first data set was short by one row.
 
Last edited:
Upvote 0
Hello,

I have 2 sets of data (100 and 101) for a set of entities (A, B, C, D) that i want to merge.
I have written a macro that combines all the data of 101 for A,B,C,D into one file (data 101) and also combines all the data of 100 for A,B,C,D into another file (data 100).

Now i want to merge the 2 files into a single master file called "combined" in a specific format and save it. Can you please help?

my code so far is as follows:
Code:
Sub tiso()
Dim fn, e, wb As Workbook
Dim LastRow As Long
Dim NextCell As Range
Dim First As Boolean
Dim sAppPath As String, sFileName As String, sDate As String

fn = Application.GetOpenFilename(FileFilter:="Microsoft excel files (*.xls), *.xls", _
                     Title:="Go to Data 100 and Press CTRL A to Select All the Files", MultiSelect:=True)
If Not IsArray(fn) Then Exit Sub
Set wb = Workbooks.Add
'wb.SaveAs Filename:="C:\tiso\compiled & Format(Date, "").xls"


sAppPath = "c:\tiso\"
sDate = Replace(FormatDateTime(Now(), vbShortDate), "/", "-")
sFileName = sAppPath & "Data 100  " & sDate & ".xls"

ActiveWorkbook.SaveAs Filename:= _
sFileName, FileFormat:= _
xlNormal, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False _
, CreateBackup:=False

For Each e In fn
    With Workbooks.Open(e)
        With .Sheets(1)
            'LastRow = Range("A65536").End(xlUp).Row
            'Range("B15", Cells(LastRow + 1, "J")).Copy
            Range("a4:b4").Cells.Copy
            wb.Sheets(1).Cells(wb.Sheets(1).Cells.SpecialCells(11).Row + 2, 2).PasteSpecial xlPasteValues
            Range("b15 : h15,b17 : h17").Cells.Copy
            wb.Sheets(1).Cells(wb.Sheets(1).Cells.SpecialCells(11).Row + 2, 2).PasteSpecial xlPasteValues
           ' Range("b17 : h17").Cells.Copy
            'wb.Sheets(1).Cells(wb.Sheets(1).Cells.SpecialCells(11).Row + 1, 2).PasteSpecial xlPasteValues
        End With
        .Close False
    End With
Next

Application.CutCopyMode = False
Set wb = Nothing




fn = Application.GetOpenFilename(FileFilter:="Microsoft excel files (*.xls), *.xls", _
                     Title:="Go to Data 101 and Press CTRL A to Select All the Files", MultiSelect:=True)
If Not IsArray(fn) Then Exit Sub
Set wb = Workbooks.Add
'wb.SaveAs Filename:="C:\tiso\compiled & Format(Date, "").xls"


sAppPath = "c:\tiso\"
sDate = Replace(FormatDateTime(Now(), vbShortDate), "/", "-")
sFileName = sAppPath & "Data 101  " & sDate & ".xls"

ActiveWorkbook.SaveAs Filename:= _
sFileName, FileFormat:= _
xlNormal, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False _
, CreateBackup:=False

For Each e In fn
    With Workbooks.Open(e)
        With .Sheets(1)
            'LastRow = Range("A65536").End(xlUp).Row
            'Range("B15", Cells(LastRow + 1, "J")).Copy
            Range("a4:b4").Cells.Copy
            wb.Sheets(1).Cells(wb.Sheets(1).Cells.SpecialCells(11).Row + 2, 2).PasteSpecial xlPasteValues
            Range("b15 : d16").Cells.Copy
            wb.Sheets(1).Cells(wb.Sheets(1).Cells.SpecialCells(11).Row + 2, 2).PasteSpecial xlPasteValues
           ' Range("b17 : h17").Cells.Copy
            'wb.Sheets(1).Cells(wb.Sheets(1).Cells.SpecialCells(11).Row + 1, 2).PasteSpecial xlPasteValues
        End With
        .Close False
    End With
Next

Application.CutCopyMode = False
Set wb = Nothing




End Sub


Data 100

Excel Workbook
ABCDEFGHI
1***Data 100*****
2*********
3*Data A*******
4*********
5*EQUIVALENT( MILLONS)3292668LONG-11638SHORT737897.1LONG*
6*NET OWN FUNDS(NOFs)7.32LONG-0.03SHORT1.64LONG*
7*********
8*Data B*******
9*********
10*EQUIVALENT( MILLONS)3292668LONG-11638SHORT737897.1LONG*
11*NET OWN FUNDS(NOFs)7.32LONG-0.03SHORT1.64LONG*
12*********
13*Data C*******
14*********
15*EQUIVALENT( MILLONS)3292668LONG-11638SHORT737897.1LONG*
16*NET OWN FUNDS(NOFs)7.32LONG-0.03SHORT1.64LONG*
17*********
18*Data D*******
19*********
20*EQUIVALENT( MILLONS)3292668LONG-11638SHORT737897.1LONG*
21*NET OWN FUNDS(NOFs)7.32LONG-0.03SHORT1.64LONG*
Sheet1




Data 101


Excel Workbook
ABCDE
1***Data 101*
2*****
3*Data A***
4*****
5*NOP IN PERCENTAGE%27232600LONG*
6*AFOP IN PERCENTAGE1.53LONG*
7*****
8*Data B***
9*****
10*NOP IN PERCENTAGE%27232600LONG*
11*AFOP IN PERCENTAGE1.53LONG*
12*****
13*Data C***
14*****
15*NOP IN PERCENTAGE%27232600LONG*
16*AFOP IN PERCENTAGE1.53LONG*
17*****
18*Data D***
19*****
20*NOP IN PERCENTAGE%27232600LONG*
21*AFOP IN PERCENTAGE1.53LONG*
22*****
Sheet1






Combined should look like this:


Excel Workbook
BCDEFGHI
1********
2Data A*******
3********
4EQUIVALENT( MILLONS)4,380,526.71LONG22,732.22LONG496,252.16LONG*
5NET OWN FUNDS(NOFs)******45,001,589.54
6NOP IN PERCENTAGE9.73LONG0.05LONG1.10LONG*
7AFOP IN PERCENTAGE******12.72
8********
9Data B*******
10********
11EQUIVALENT( MILLONS)-16,985,152.40SHORT-464,904.61SHORT96,455.07LONG*
12NET OWN FUNDS(NOFs)******264,186,952.00
13NOP IN PERCENTAGE%-6.43SHORT-0.18SHORT0.04LONG*
14AFOP IN PERCENTAGE******-6.61
15********
16Data C*******
17********
18EQUIVALENT( MILLONS)238,186.15LONG75,261.90LONG23,755.17LONG*
19NET OWN FUNDS(NOFs)******27,232,600.00
20NOP IN PERCENTAGE%0.87LONG0.28LONG0.09LONG*
21AFOP IN PERCENTAGE%******1.69
22********
23Data D*******
24********
25EQUIVALENT( MILLONS)1,479,823.88LONG179,947.64LONG1,681,151.03LONG*
26NET OWN FUNDS(NOFs)******22,331,551.00
27NOP IN PERCENTAGE%6.63LONG0.81LONG7.53LONG*
28AFOP IN PERCENTAGE%******14.96
Sheet1
 
Last edited by a moderator:
Upvote 0
data 100
Excel Workbook
ABCDEFGHI
1***Data 100*****
2*********
3*Data A*******
4*********
5*EQUIVALENT( MILLONS)3292668LONG-11638SHORT737897.1LONG*
6*NET OWN FUNDS(NOFs)7.32LONG-0.03SHORT1.64LONG*
7*********
8*Data B*******
9*********
10*EQUIVALENT( MILLONS)3292668LONG-11638SHORT737897.1LONG*
11*NET OWN FUNDS(NOFs)7.32LONG-0.03SHORT1.64LONG*
12*********
13*Data C*******
14*********
15*EQUIVALENT( MILLONS)3292668LONG-11638SHORT737897.1LONG*
16*NET OWN FUNDS(NOFs)7.32LONG-0.03SHORT1.64LONG*
17*********
18*Data D*******
19*********
20*EQUIVALENT( MILLONS)3292668LONG-11638SHORT737897.1LONG*
21*NET OWN FUNDS(NOFs)7.32LONG-0.03SHORT1.64LONG*
Sheet1

data 101
Excel Workbook
ABCDE
1***Data 101*
2*****
3*Data A***
4*****
5*NOP IN PERCENTAGE%27232600LONG*
6*AFOP IN PERCENTAGE1.53LONG*
7*****
8*Data B***
9*****
10*NOP IN PERCENTAGE%27232600LONG*
11*AFOP IN PERCENTAGE1.53LONG*
12*****
13*Data C***
14*****
15*NOP IN PERCENTAGE%27232600LONG*
16*AFOP IN PERCENTAGE1.53LONG*
17*****
18*Data D***
19*****
20*NOP IN PERCENTAGE%27232600LONG*
21*AFOP IN PERCENTAGE1.53LONG*
22*****
Sheet1


combined should look like this:
Excel Workbook
BCDEFGHI
1********
2Data A*******
3********
4EQUIVALENT( MILLONS)4,380,526.71LONG22,732.22LONG496,252.16LONG*
5NET OWN FUNDS(NOFs)******45,001,589.54
6NOP IN PERCENTAGE9.73LONG0.05LONG1.10LONG*
7AFOP IN PERCENTAGE******12.72
8********
9Data B*******
10********
11EQUIVALENT( MILLONS)-16,985,152.40SHORT-464,904.61SHORT96,455.07LONG*
12NET OWN FUNDS(NOFs)******264,186,952.00
13NOP IN PERCENTAGE%-6.43SHORT-0.18SHORT0.04LONG*
14AFOP IN PERCENTAGE******-6.61
15********
16Data C*******
17********
18EQUIVALENT( MILLONS)238,186.15LONG75,261.90LONG23,755.17LONG*
19NET OWN FUNDS(NOFs)******27,232,600.00
20NOP IN PERCENTAGE%0.87LONG0.28LONG0.09LONG*
21AFOP IN PERCENTAGE%******1.69
22********
23Data D*******
24********
25EQUIVALENT( MILLONS)1,479,823.88LONG179,947.64LONG1,681,151.03LONG*
26NET OWN FUNDS(NOFs)******22,331,551.00
27NOP IN PERCENTAGE%6.63LONG0.81LONG7.53LONG*
28AFOP IN PERCENTAGE%******14.96
Sheet1
 
Upvote 0

Forum statistics

Threads
1,224,532
Messages
6,179,388
Members
452,908
Latest member
MTDelphis

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