Merge Excel Multiple Sheets from a single work book to one sheet.

rtr1811

New Member
Joined
Jun 3, 2020
Messages
24
Office Version
  1. 2007
Platform
  1. Windows
Hi all,
I've to merge all the excel sheets in a excel work book to one sheet and customize a few things. Can any one help me with VBA code?

I've to Copy each and every row in the sheets 98, 100 & 101 to Wardwise. After that I've to insert a column called "Part" and copy & paste the number in the sheet to the column.

File for Merge Test

After Copying all the rows is completed, I've to sort the resultant file first by Column called Ward No, then bby Part and then by Page.

Input data is in sheet ns 98, 100 and 101 and the expected output is presented in sheet called Wardwise. How to do this?
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Hi there,

I am not sure if you are still looking for solution for this.
As you requested for VBA code, I assume you had basic understanding on setting up a macro in template. (using developer tool, adding module, copy paste module, run macro)

The way I will do it will be using simply copy paste VBA command as below:
  1. Define all related sheets
  2. Clear data in "Wardwise" [I assume you will need to redo the report routinely.]
  3. Update any data in sheet "98", "100", "101" [I assume this is fixed name for those sheet, or else, you can edit the sheet name in the code]
  4. Unmerge all cells in data, to allow filtering in later part of macro / prevent copy-paste errors.
  5. Since all format from "98", "100", "101" will be the same every time, code will find last row for data for each sheet using "Net total"
  6. Then start copy sheet "98" into "Wardwise", using range of columns, and assign Part as 98 (sheet name)
  7. Then start copy sheet "100" into "Wardwise", using range of columns, and assign Part as 100 (sheet name)
  8. Then start copy sheet "101" into "Wardwise", using range of columns, and assign Part as 101 (sheet name)
  9. Sort all data by Ward No, then by Part, then by Page. (you can rearrange this in the code)
  10. Remove autofilter
  11. Msgbox "Done merged"
Try the code:

VBA Code:
Sub MergeSheets()

Dim wsD As Worksheet
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim ws3 As Worksheet

Set wsD = ThisWorkbook.Sheets("Wardwise")
Set ws1 = ThisWorkbook.Sheets("98") 'Change this if you have different named sheet
Set ws2 = ThisWorkbook.Sheets("100") 'Change this if you have different named sheet
Set ws3 = ThisWorkbook.Sheets("101") 'Change this if you have different named sheet

'delete previous data
wsD.Range("B6:R10000").Clear

'unmerge all cells
wsD.Range("B6:R10000").UnMerge
ws1.Range("B8:R10000").UnMerge
ws2.Range("B8:R10000").UnMerge
ws3.Range("B8:R10000").UnMerge

'count the lastrow of each sheets
Dim data_lastrow As Long
data_lastrow = wsD.Cells(Rows.Count, 4).End(xlUp).Row + 1

'Find net total as last row
Dim FoundCell1 As Range
Const WHAT_TO_FIND1 As String = "Net Total"
Set FoundCell1 = ws1.Range("B:B").Find(What:=WHAT_TO_FIND1)
Dim data_lastrow1 As Long
data_lastrow1 = FoundCell1.Row

'Find net total as last row
Dim FoundCell2 As Range
Const WHAT_TO_FIND2 As String = "Net Total"
Set FoundCell2 = ws2.Range("B:B").Find(What:=WHAT_TO_FIND2)
Dim data_lastrow2 As Long
data_lastrow2 = FoundCell2.Row

'Find net total as last row
Dim FoundCell3 As Range
Const WHAT_TO_FIND3 As String = "Net Total"
Set FoundCell3 = ws3.Range("B:B").Find(What:=WHAT_TO_FIND3)
Dim data_lastrow3 As Long
data_lastrow3 = FoundCell3.Row

'copy sheets into Destination sheet
ws1.Range("C8:Q" & data_lastrow1).Copy Destination:=wsD.Range("D" & data_lastrow)
ws1.Range("B8:B" & data_lastrow1).Copy Destination:=wsD.Range("B" & data_lastrow)

wsD.Range("C" & data_lastrow & ":C" & data_lastrow + data_lastrow1 - 8).Value = ws1.Name


'refresh last row of destination sheet
data_lastrow = wsD.Cells(Rows.Count, 4).End(xlUp).Row + 1
ws2.Range("C8:Q" & data_lastrow2).Copy Destination:=wsD.Range("D" & data_lastrow)
ws2.Range("B8:B" & data_lastrow2).Copy Destination:=wsD.Range("B" & data_lastrow)

wsD.Range("C" & data_lastrow & ":C" & data_lastrow + data_lastrow2 - 8).Value = ws2.Name

'refresh last row of destination sheet
data_lastrow = wsD.Cells(Rows.Count, 4).End(xlUp).Row + 1
ws3.Range("C8:Q" & data_lastrow3).Copy Destination:=wsD.Range("D" & data_lastrow)
ws3.Range("B8:B" & data_lastrow3).Copy Destination:=wsD.Range("B" & data_lastrow)

wsD.Range("C" & data_lastrow & ":C" & data_lastrow + data_lastrow3 - 8).Value = ws3.Name

'refresh last row of destination sheet
data_lastrow = wsD.Cells(Rows.Count, 4).End(xlUp).Row + 1
wsD.Range("B5:R" & data_lastrow).Sort Key1:=Range("D5"), Order1:=xlAscending, Header:=xlYes
wsD.Range("B5:R" & data_lastrow).Sort Key1:=Range("C5"), Order1:=xlAscending, Header:=xlYes
wsD.Range("B5:R" & data_lastrow).Sort Key1:=Range("B5"), Order1:=xlAscending, Header:=xlYes

wsD.AutoFilterMode = False

Set wsD = Nothing
Set ws1 = Nothing
Set ws2 = Nothing
Set ws3 = Nothing

MsgBox "Done merged"

End Sub
 
Upvote 0
Thanks for the reply. Only the prior data in the sheet called Wardwise is Cleared. I got Runtime Error while executing the code.

No. of Source Sheets may vary. It is not limited to 3. (not only 98, 100 & 101). It may be 2 or 8. So, Hard coding the sheets may not be helpful. Instead we have to loop through each sheet which has number as its name and copy the data.

After copying the data a column has to be inserted before ward no and the Name of the sheet (i.e 98 or 100 or 101) from which the data is copied has to be copied and pasted in all the cells. Then the sheet has to be sorted by Column called Ward No, then by Part and then by Page.

How to get around this error?
Error1.JPG
Error2.JPG

Thank you once again.
 
Upvote 0
Hi,

If there might be more sheets, let me try to put them in a LOOP. Will try to loop all other sheets except for "Wardwise". (code below)

For "Run time error 1004", this might be caused by the difference in our sheet "Wardwise". From your example sheet, there is merged cells:
1714038713224.png


I do not encourage merge cells as VBA cannot perform sort/filter/copypaste, and cannot recognize the correct row of header.

Here is my starting sheet for "Wardwise":

20240425 debug Pattadaikatti-Merge-Test.xlsm
ABCDEFGHIJKLMNOPQR
1
2Panchayat Union - Melaneelithanallur
3Panchayat - Pattadaikatti
4Total votersSTSCOthers
5Page noPartWard noMaleFemaleTGMaleFemaleTGMaleFemaleTGMaleFemaleTGSub Total MaleSub Total Female
Wardwise


By using same table header as my "Wardwise" sheet, try this code:
VBA Code:
Option Explicit

Sub MergeSheets2()

Dim wsD As Worksheet
Dim ws As Worksheet

Set wsD = ThisWorkbook.Sheets("Wardwise")

'delete previous data
wsD.Range("B6:R10000").Clear


Dim data_lastrow As Long
Dim FoundCell As Range
Dim data_lastrow1 As Long


For Each ws In ThisWorkbook.Worksheets
If ws.Name = "Wardwise" Then
Else

'unmerge all cells
ws.Range("B8:R10000").UnMerge

'count the lastrow of each sheets
data_lastrow = wsD.Cells(Rows.Count, 4).End(xlUp).Row + 1

'Find net total as last row
Const WHAT_TO_FIND As String = "Net Total"
Set FoundCell = ws.Range("B:B").Find(What:=WHAT_TO_FIND)
data_lastrow1 = FoundCell.Row

'copy sheets into Destination sheet
ws.Range("C8:Q" & data_lastrow1).Copy Destination:=wsD.Range("D" & data_lastrow)
ws.Range("B8:B" & data_lastrow1).Copy Destination:=wsD.Range("B" & data_lastrow)

wsD.Range("C" & data_lastrow & ":C" & data_lastrow + data_lastrow1 - 8).Value = ws.Name

End If
Next ws

'refresh last row of destination sheet
data_lastrow = wsD.Cells(Rows.Count, 4).End(xlUp).Row + 1
wsD.Range("B5:R" & data_lastrow).Sort Key1:=Range("D5"), Order1:=xlAscending, Header:=xlYes
wsD.Range("B5:R" & data_lastrow).Sort Key1:=Range("C5"), Order1:=xlAscending, Header:=xlYes
wsD.Range("B5:R" & data_lastrow).Sort Key1:=Range("B5"), Order1:=xlAscending, Header:=xlYes

wsD.AutoFilterMode = False

Set wsD = Nothing
Set ws = Nothing

MsgBox "Done merged"

End Sub
 
Upvote 0
Thank you for the reply. It works with the modified Wardwise sheet and the code above.

I need a few more favors from you.

1. The rows which contain the Word "Total" are not needed. Either they can be deleted or skipped from copying from the respective sheets. Can you modify the code to meet out this need?

2. Sorting has to be done in the following order.
(i) Column D
(ii) Column C
(iii) Column B

3. I want to know the changes you made to the "Wardwise" sheet in order to adopt the same technique wherever needed.

I thank you once again for your help.
 
Upvote 0
Hi,

Thank you for the reply. It works with the modified Wardwise sheet and the code above.

I need a few more favors from you.

1. The rows which contain the Word "Total" are not needed. Either they can be deleted or skipped from copying from the respective sheets. Can you modify the code to meet out this need?

2. Sorting has to be done in the following order.
(i) Column D
(ii) Column C
(iii) Column B

3. I want to know the changes you made to the "Wardwise" sheet in order to adopt the same technique wherever needed.

I thank you once again for your help.
For number 1:
I will add a loop to look for the word "*Total*", then delete entire row if found.
VBA Code:
'remove row contains TOTAL
Dim d As Long 'row number
d = 6
Do Until d = data_lastrow  'loop through each row
    If wsD.Cells(d, 2).Value Like "*" & "Total" & "*" Then
        wsD.Rows(d).EntireRow.Delete
    Else
        d = d + 1
    End If
Loop

For number 2:
Here is how I have coded the sequence of sorting:
wsD.Range("B5:R" & data_lastrow).Sort Key1:=Range("D5"), Order1:=xlAscending, Header:=xlYes
wsD.Range("B5:R" & data_lastrow).Sort Key1:=Range("C5"), Order1:=xlAscending, Header:=xlYes
wsD.Range("B5:R" & data_lastrow).Sort Key1:=Range("B5"), Order1:=xlAscending, Header:=xlYes
Which is Column D -> Column C -> Column B.
If this is not the end product you wanted, try swap the Column D with Column B. like this:
wsD.Range("B5:R" & data_lastrow).Sort Key1:=Range("B5"), Order1:=xlAscending, Header:=xlYes
wsD.Range("B5:R" & data_lastrow).Sort Key1:=Range("C5"), Order1:=xlAscending, Header:=xlYes
wsD.Range("B5:R" & data_lastrow).Sort Key1:=Range("D5"), Order1:=xlAscending, Header:=xlYes

For number 3:
From your original "Wardwise" sheet, simply select cell B4:D5, then click "Unmerge".
Then Cut word from cell B4:D4, and Paste into cell B5:D5, so that they become the table header just like the rest of the header (Row5).
1714359345224.png



Here is my updated code:
VBA Code:
Option Explicit

Sub MergeSheets3()

Dim wsD As Worksheet
Dim ws As Worksheet

Set wsD = ThisWorkbook.Sheets("Wardwise")

'delete previous data
wsD.Range("B6:R10000").Clear

Dim data_lastrow As Long
Dim FoundCell As Range
Dim data_lastrow1 As Long
    
    
For Each ws In ThisWorkbook.Worksheets
If ws.Name = "Wardwise" Then
Else
'unmerge all cells
ws.Range("B8:R10000").UnMerge


'count the lastrow of each sheets
data_lastrow = wsD.Cells(Rows.Count, 4).End(xlUp).Row + 1

'Find net total as last row
Const WHAT_TO_FIND As String = "Net Total"
Set FoundCell = ws.Range("B:B").Find(What:=WHAT_TO_FIND)
data_lastrow1 = FoundCell.Row

'copy sheets into Destination sheet
ws.Range("C8:Q" & data_lastrow1).Copy Destination:=wsD.Range("D" & data_lastrow)
ws.Range("B8:B" & data_lastrow1).Copy Destination:=wsD.Range("B" & data_lastrow)

wsD.Range("C" & data_lastrow & ":C" & data_lastrow + data_lastrow1 - 8).Value = ws.Name

End If
Next ws

'refresh last row of destination sheet
data_lastrow = wsD.Cells(Rows.Count, 3).End(xlUp).Row + 1

'remove row contains TOTAL
Dim d As Long 'row number
d = 6
Do Until d = data_lastrow  'loop through each row
    If wsD.Cells(d, 2).Value Like "*" & "Total" & "*" Then
        wsD.Rows(d).EntireRow.Delete
    Else
        d = d + 1
    End If
Loop

'sort order D then C then B, change this for your preferences
wsD.Range("B5:R" & data_lastrow).Sort Key1:=Range("D5"), Order1:=xlAscending, Header:=xlYes
wsD.Range("B5:R" & data_lastrow).Sort Key1:=Range("C5"), Order1:=xlAscending, Header:=xlYes
wsD.Range("B5:R" & data_lastrow).Sort Key1:=Range("B5"), Order1:=xlAscending, Header:=xlYes

wsD.AutoFilterMode = False

Set wsD = Nothing
Set ws = Nothing

MsgBox "Done merged"

End Sub
 
Upvote 0
Ok. Thank you.

I will sort manually as in the attached image. The ouptut of the sort operation should be as in the attached excel file.

Panchayat Union - Melaneelithanallur
Panchayat - Pattadaikatti
Total votersSTSCOthers
Page noPartWard noMaleFemaleTGMaleFemaleTGMaleFemaleTGMaleFemaleTG
3
101​
145450
4
101​
139390
7
100​
17117110
8
100​
110100
9
100​
154540
9
101​
101010
10
101​
166660
11
101​
103030
12
101​
101010
13
101​
101010
15
100​
121210
15
101​
111110
16
100​
10101
16
101​
154540
17
101​
145450
19
101​
121210
20
100​
132320
20
101​
141410
25
101​
111110
26
101​
1131113110
29
101​
137370
30
101​
113130
31
101​
132320
33
100​
11071070
33
101​
112120
34
100​
1171317130
34
101​
156560
35
100​
1141614160
36
100​
1151515150
37
100​
1171317130
38
100​
1141614160
39
100​
1151415140
40
100​
112120
42
100​
110100
ADD 1
100​
122220
ADD 1
101​
123230
ADD 2
101​
16464
DEL 2
100​
12424
3
98​
232320
4
98​
275750
5
98​
240400
6
98​
222220
7
98​
26116110
8
98​
2121212120
9
98​
2141414140
10
98​
21161160
17
98​
201010
21
98​
22323
22
98​
22424
23
98​
27474
24
98​
25555
25
98​
27373
26
98​
23535
27
98​
21212
28
98​
25555
29
98​
26565
30
98​
2127127
31
98​
214161416
32
98​
214161416
33
98​
213131313
34
98​
214161416
35
98​
26969
37
98​
23838
ADD 1
98​
21313
ADD 2
98​
23131
DEL 3
98​
20101
3
101​
335350
4
101​
366660
5
101​
311110
6
100​
31101100
8
100​
354540
8
101​
31181180
9
100​
310100
9
101​
339390
10
101​
366660
13
100​
33131
14
100​
31111
15
101​
321210
18
100​
301010
20
101​
302020
24
101​
310100
25
101​
324240
26
100​
343430
26
101​
302020
27
100​
3151415140
27
101​
312120
28
100​
389890
29
100​
3121012100
29
101​
383830
30
100​
31281280
31
100​
3121312130
32
100​
3181218120
33
100​
367670
33
101​
321210
34
101​
311110
40
100​
3161116110
41
100​
335350
42
100​
353530
ADD 1
101​
31212
ADD 2
101​
31414
DEL 3
101​
32424
3
98​
44134130
4
98​
493930
7
98​
401010
8
98​
413130
10
98​
457570
11
98​
4131713170
12
98​
4191119110
13
98​
4161416140
14
98​
4191119110
15
98​
4131713170
16
98​
4141614160
17
98​
4151015100
18
98​
435350
19
98​
465650
20
98​
421210
21
98​
44747
22
98​
41212
23
98​
43232
28
98​
45454
29
98​
42020
30
98​
45656
33
98​
41111
35
98​
41111
36
98​
44242
ADD 1
98​
41717
ADD 2
98​
41010
DEL 3
98​
40101
3
101​
511110
5
100​
513130
5
101​
58168160
6
100​
51010
6
101​
5171317130
7
101​
534340
8
101​
563630
9
101​
587870
10
100​
51010
10
101​
542420
11
101​
5111011100
12
101​
5121312130
13
101​
5141514150
14
101​
5181218120
15
101​
5111311130
16
100​
511110
16
101​
58128120
17
101​
5111011100
18
101​
5131713170
19
100​
510100
19
101​
545450
21
100​
51010
24
100​
53030
25
100​
50202
26
100​
51010
30
101​
555550
34
101​
523230
41
100​
51391390
42
100​
537370
ADD 1
100​
50303
ADD 1
101​
54747
ADD 2
101​
52020
DEL 2
100​
51111
DEL 3
101​
52020
3
98​
644440
4
98​
642420
5
98​
6151115110
6
98​
69179170
7
98​
666660
8
98​
602020
9
98​
611110
10
98​
601010
17
98​
622220
18
98​
6121012100
19
98​
69109100
20
98​
62222
21
98​
67777
22
98​
611102199
23
98​
6410410
24
98​
6146146
25
98​
6119119
26
98​
610121012
27
98​
614131413
28
98​
65656
29
98​
69898
33
98​
61111
35
98​
6676502
36
98​
6541044
37
98​
69494
ADD 1
98​
6201010
ADD 2
98​
6231112
3
101​
74620260
4
101​
724240
5
101​
731310
6
100​
71111
7
100​
72102100
7
101​
7131013100
8
100​
774740
9
100​
746460
10
100​
711110
11
101​
701010
12
101​
72222000
15
101​
701010
17
100​
70202
18
100​
731310
19
101​
71351350
20
101​
7111211120
21
101​
71713451380
22
101​
715151310250
23
101​
711191013160
24
101​
713161316000
25
101​
791312910
26
101​
731310
27
100​
701010
27
101​
712120
28
100​
785850
28
101​
701010
29
100​
74411330
29
101​
736360
30
100​
72811170
30
101​
76106100
31
100​
73222100
31
101​
71471470
32
101​
748480
33
101​
77545300
34
101​
72111100
ADD 1
100​
70303
ADD 1
101​
72020
ADD 2
101​
7390435
DEL 2
100​
70101
DEL 3
101​
7311021
5
100​
89109100
6
100​
877770
8
100​
801010
14
100​
802020
16
100​
811110
16
101​
810100
17
100​
812120
19
100​
832320
20
100​
8141014100
21
100​
8151415140
22
100​
8181218120
23
100​
8151515150
24
100​
8101710170
25
100​
8111711170
26
100​
88148140
27
101​
8121212120
32
101​
868680
33
101​
848480
DEL 2
100​
81313
3
100​
9131713170
3
101​
910100
4
100​
9121812180
5
100​
934340
6
100​
920200
8
100​
935350
8
101​
920200
9
100​
928280
9
101​
911110
10
100​
9121512150
11
100​
9151515150
11
101​
932320
12
100​
9151515150
13
100​
9121412140
14
100​
9151115110
15
100​
9101710170
16
100​
9131213120
17
100​
9101510150
18
100​
91510231370
19
100​
9111311130
20
100​
901010
31
101​
922220
32
101​
931310
39
100​
90101
ADD 1
101​
92323
ADD 2
101​
92525
DEL 2
100​
92020
 

Attachments

  • Screenshot 2024-04-29 183045.png
    Screenshot 2024-04-29 183045.png
    252 KB · Views: 5
Upvote 0
Hi,

I have play around with the sorting, according to your manually sorted list.
It seems that the sequence of sorting should be Column C -> Column B -> Column D.

Here is the updated code:
VBA Code:
Option Explicit

Sub MergeSheets4()

Dim wsD As Worksheet
Dim ws As Worksheet

Set wsD = ThisWorkbook.Sheets("Wardwise")

'delete previous data
wsD.Range("B6:R10000").Clear

Dim data_lastrow As Long
Dim FoundCell As Range
Dim data_lastrow1 As Long
    
    
For Each ws In ThisWorkbook.Worksheets
If ws.Name = "Wardwise" Then
Else
'unmerge all cells
ws.Range("B8:R10000").UnMerge


'count the lastrow of each sheets
data_lastrow = wsD.Cells(Rows.Count, 4).End(xlUp).Row + 1

'Find net total as last row
Const WHAT_TO_FIND As String = "Net Total"
Set FoundCell = ws.Range("B:B").Find(What:=WHAT_TO_FIND)
data_lastrow1 = FoundCell.Row

'copy sheets into Destination sheet
ws.Range("C8:Q" & data_lastrow1).Copy Destination:=wsD.Range("D" & data_lastrow)
ws.Range("B8:B" & data_lastrow1).Copy Destination:=wsD.Range("B" & data_lastrow)

wsD.Range("C" & data_lastrow & ":C" & data_lastrow + data_lastrow1 - 8).Value = ws.Name

End If
Next ws

data_lastrow = wsD.Cells(Rows.Count, 3).End(xlUp).Row + 1

'remove row contains TOTAL
Dim d As Long 'row number
d = 6
Do Until d = data_lastrow  'loop through each row
    If wsD.Cells(d, 2).Value Like "*" & "Total" & "*" Then
        wsD.Rows(d).EntireRow.Delete
    Else
        d = d + 1
    End If
Loop

'refresh last row of destination sheet
wsD.Range("B5:R" & data_lastrow).Sort Key1:=Range("C5"), Order1:=xlAscending, Header:=xlYes
wsD.Range("B5:R" & data_lastrow).Sort Key1:=Range("B5"), Order1:=xlAscending, Header:=xlYes
wsD.Range("B5:R" & data_lastrow).Sort Key1:=Range("D5"), Order1:=xlAscending, Header:=xlYes

wsD.AutoFilterMode = False

Set wsD = Nothing
Set ws = Nothing

MsgBox "Done merged"

End Sub
 
Upvote 0
Hi there,

I am not sure if you are still looking for solution for this.
As you requested for VBA code, I assume you had basic understanding on setting up a macro in template. (using developer tool, adding module, copy paste module, run macro)

The way I will do it will be using simply copy paste VBA command as below:
  1. Define all related sheets
  2. Clear data in "Wardwise" [I assume you will need to redo the report routinely.]
  3. Update any data in sheet "98", "100", "101" [I assume this is fixed name for those sheet, or else, you can edit the sheet name in the code]
  4. Unmerge all cells in data, to allow filtering in later part of macro / prevent copy-paste errors.
  5. Since all format from "98", "100", "101" will be the same every time, code will find last row for data for each sheet using "Net total"
  6. Then start copy sheet "98" into "Wardwise", using range of columns, and assign Part as 98 (sheet name)
  7. Then start copy sheet "100" into "Wardwise", using range of columns, and assign Part as 100 (sheet name)
  8. Then start copy sheet "101" into "Wardwise", using range of columns, and assign Part as 101 (sheet name)
  9. Sort all data by Ward No, then by Part, then by Page. (you can rearrange this in the code)
  10. Remove autofilter
  11. Msgbox "Done merged"
Try the code:

VBA Code:
Sub MergeSheets()

Dim wsD As Worksheet
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim ws3 As Worksheet

Set wsD = ThisWorkbook.Sheets("Wardwise")
Set ws1 = ThisWorkbook.Sheets("98") 'Change this if you have different named sheet
Set ws2 = ThisWorkbook.Sheets("100") 'Change this if you have different named sheet
Set ws3 = ThisWorkbook.Sheets("101") 'Change this if you have different named sheet

'delete previous data
wsD.Range("B6:R10000").Clear

'unmerge all cells
wsD.Range("B6:R10000").UnMerge
ws1.Range("B8:R10000").UnMerge
ws2.Range("B8:R10000").UnMerge
ws3.Range("B8:R10000").UnMerge

'count the lastrow of each sheets
Dim data_lastrow As Long
data_lastrow = wsD.Cells(Rows.Count, 4).End(xlUp).Row + 1

'Find net total as last row
Dim FoundCell1 As Range
Const WHAT_TO_FIND1 As String = "Net Total"
Set FoundCell1 = ws1.Range("B:B").Find(What:=WHAT_TO_FIND1)
Dim data_lastrow1 As Long
data_lastrow1 = FoundCell1.Row

'Find net total as last row
Dim FoundCell2 As Range
Const WHAT_TO_FIND2 As String = "Net Total"
Set FoundCell2 = ws2.Range("B:B").Find(What:=WHAT_TO_FIND2)
Dim data_lastrow2 As Long
data_lastrow2 = FoundCell2.Row

'Find net total as last row
Dim FoundCell3 As Range
Const WHAT_TO_FIND3 As String = "Net Total"
Set FoundCell3 = ws3.Range("B:B").Find(What:=WHAT_TO_FIND3)
Dim data_lastrow3 As Long
data_lastrow3 = FoundCell3.Row

'copy sheets into Destination sheet
ws1.Range("C8:Q" & data_lastrow1).Copy Destination:=wsD.Range("D" & data_lastrow)
ws1.Range("B8:B" & data_lastrow1).Copy Destination:=wsD.Range("B" & data_lastrow)

wsD.Range("C" & data_lastrow & ":C" & data_lastrow + data_lastrow1 - 8).Value = ws1.Name


'refresh last row of destination sheet
data_lastrow = wsD.Cells(Rows.Count, 4).End(xlUp).Row + 1
ws2.Range("C8:Q" & data_lastrow2).Copy Destination:=wsD.Range("D" & data_lastrow)
ws2.Range("B8:B" & data_lastrow2).Copy Destination:=wsD.Range("B" & data_lastrow)

wsD.Range("C" & data_lastrow & ":C" & data_lastrow + data_lastrow2 - 8).Value = ws2.Name

'refresh last row of destination sheet
data_lastrow = wsD.Cells(Rows.Count, 4).End(xlUp).Row + 1
ws3.Range("C8:Q" & data_lastrow3).Copy Destination:=wsD.Range("D" & data_lastrow)
ws3.Range("B8:B" & data_lastrow3).Copy Destination:=wsD.Range("B" & data_lastrow)

wsD.Range("C" & data_lastrow & ":C" & data_lastrow + data_lastrow3 - 8).Value = ws3.Name

'refresh last row of destination sheet
data_lastrow = wsD.Cells(Rows.Count, 4).End(xlUp).Row + 1
wsD.Range("B5:R" & data_lastrow).Sort Key1:=Range("D5"), Order1:=xlAscending, Header:=xlYes
wsD.Range("B5:R" & data_lastrow).Sort Key1:=Range("C5"), Order1:=xlAscending, Header:=xlYes
wsD.Range("B5:R" & data_lastrow).Sort Key1:=Range("B5"), Order1:=xlAscending, Header:=xlYes

wsD.AutoFilterMode = False

Set wsD = Nothing
Set ws1 = Nothing
Set ws2 = Nothing
Set ws3 = Nothing

MsgBox "Done merged"

End Sub
Thank you for the reply. It shows an runtime error. Run Time Error: 91. I have attached the image here with.
 

Attachments

  • Screenshot 2024-05-02 172032.png
    Screenshot 2024-05-02 172032.png
    134.9 KB · Views: 8
Upvote 0
Hi,

I have play around with the sorting, according to your manually sorted list.
It seems that the sequence of sorting should be Column C -> Column B -> Column D.

Here is the updated code:
VBA Code:
Option Explicit

Sub MergeSheets4()

Dim wsD As Worksheet
Dim ws As Worksheet

Set wsD = ThisWorkbook.Sheets("Wardwise")

'delete previous data
wsD.Range("B6:R10000").Clear

Dim data_lastrow As Long
Dim FoundCell As Range
Dim data_lastrow1 As Long
   
   
For Each ws In ThisWorkbook.Worksheets
If ws.Name = "Wardwise" Then
Else
'unmerge all cells
ws.Range("B8:R10000").UnMerge


'count the lastrow of each sheets
data_lastrow = wsD.Cells(Rows.Count, 4).End(xlUp).Row + 1

'Find net total as last row
Const WHAT_TO_FIND As String = "Net Total"
Set FoundCell = ws.Range("B:B").Find(What:=WHAT_TO_FIND)
data_lastrow1 = FoundCell.Row

'copy sheets into Destination sheet
ws.Range("C8:Q" & data_lastrow1).Copy Destination:=wsD.Range("D" & data_lastrow)
ws.Range("B8:B" & data_lastrow1).Copy Destination:=wsD.Range("B" & data_lastrow)

wsD.Range("C" & data_lastrow & ":C" & data_lastrow + data_lastrow1 - 8).Value = ws.Name

End If
Next ws

data_lastrow = wsD.Cells(Rows.Count, 3).End(xlUp).Row + 1

'remove row contains TOTAL
Dim d As Long 'row number
d = 6
Do Until d = data_lastrow  'loop through each row
    If wsD.Cells(d, 2).Value Like "*" & "Total" & "*" Then
        wsD.Rows(d).EntireRow.Delete
    Else
        d = d + 1
    End If
Loop

'refresh last row of destination sheet
wsD.Range("B5:R" & data_lastrow).Sort Key1:=Range("C5"), Order1:=xlAscending, Header:=xlYes
wsD.Range("B5:R" & data_lastrow).Sort Key1:=Range("B5"), Order1:=xlAscending, Header:=xlYes
wsD.Range("B5:R" & data_lastrow).Sort Key1:=Range("D5"), Order1:=xlAscending, Header:=xlYes

wsD.AutoFilterMode = False

Set wsD = Nothing
Set ws = Nothing

MsgBox "Done merged"

End Sub
Thank you for the reply. It shows an runtime error. Run Time Error: 91. I have attached the image here with.
 

Attachments

  • Screenshot 2024-04-29 183045.png
    Screenshot 2024-04-29 183045.png
    252 KB · Views: 7
Upvote 0

Forum statistics

Threads
1,223,898
Messages
6,175,272
Members
452,628
Latest member
dd2

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