Insert Blank Rows and Headers between Grouped data

Kel09

New Member
Joined
Jul 28, 2014
Messages
23
Hi,
I have a code shown below that sets out to do two things:

1.
Copy and paste data from a source workbook to a destination workbook.

2.
Group the data by Type (Small/ Large) by inserting 5 blank rows after the first empty row of Type1(Small) and then inserting similar headers for Type2(Large). The headers for Type1(Small) are set

Stage 1 of the code works OK. However I am having problems with Stage2. Nothing happens! I have been tinkering around with it pulling snippets of code found on the forum to no avail. Any help would be much appreciated.

I have highlighted the problem area of the code in red and provided some sample data of what I am trying to achieve immediately below.


Sample data:

Stage 1
GroupType
BSmall
BSmall
BLarge
BLarge
BLarge
BLarge
BLarge
BLarge
BLarge
BLarge

<tbody>
</tbody>

Stage 2


GroupType
BSmall
BSmall
GroupType
BLarge
BLarge
BLarge
BLarge
BLarge
BLarge
BLarge
BLarge

<tbody>
</tbody>

Here is my code
(Problem area highlighted in red)


Sub Test()
Application.DisplayAlerts = False
Application.EnableEvents = True


Dim Grp As Workbook
Dim Lastrow As Integer
Dim fnRange As Range
Dim i As Integer
Dim erow As Integer
Dim Month As String
Dim finalRow As Integer
Month = "Nov"
Lastrow = ActiveSheet.Range("B" & Rows.Count).End(xlUp).Row
For i = 1 To Lastrow
Set fnRange = Cells.Find("Small", Cells(8), xlValues, , xlByColumns)


If Cells(i, 1) = 2 Then
Worksheets("Grp").Range(Cells(i, 2), Cells(i, 9)).Select
Selection.Copy
Workbooks.Open Filename:="C:\Users\YV\Desktop\VBA\TestBook.xlsx"
Worksheets(Month).Select
erow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Offset(1, 1).Row
ActiveSheet.Cells(erow, 1).Select
ActiveSheet.PasteSpecial
Set fnRange = Cells.Find("Small", Cells(8), xlValues, , xlByColumns)
Rows(fnRange.Row + 1 & "1:1" & fnRange.Row + 5).Insert
ActiveWorkbook.Save
ActiveWorkbook.Close
Application.CutCopyMode = False
End If


Next i




End Sub
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
More tinkering and I have now got the code to partially execute stage 2(see explanation below). However it is still inserting too many rows. I would like the rows between the Groups limited to 3 one of which should contain the headers copied down, so the data shown exists as two separate tables.

Any help on this would be much appreciated.

Updated code:

If Cells(i, 1) = 2 Then
Worksheets("Grp").Range(Cells(i, 2), Cells(i, 9)).Select
Selection.Copy
Workbooks.Open Filename:="C:\Users\YV\Desktop\VBA\TestBook.xlsx"
Worksheets(Month).Select
erow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Offset(1, 1).Row
ActiveSheet.Cells(erow, 1).Select
ActiveSheet.PasteSpecial
With Range("H1", Range("H" & Rows.Count).End(xlUp))
.Subtotal GroupBy:=1, Function:=xlSum, TotalList:=Array(1), _
Replace:=True, PageBreaks:=False, SummaryBelowData:=True
.Offset(, -1).AutoFilter Field:=1, Criteria1:="=*total*"
.Offset(4).SpecialCells(xlCellTypeVisible).ClearContents
.AutoFilter
.Offset(, -1).EntireColumn.Delete
.EntireColumn.RemoveSubtotal
End With
ActiveWorkbook.Save
ActiveWorkbook.Close
Application.CutCopyMode = False
End If


Hi,
I have a code shown below that sets out to do two things:

1.
Copy and paste data from a source workbook to a destination workbook.

2.
Group the data by Type (Small/ Large) by inserting 5 blank rows after the first empty row of Type1(Small) and then inserting similar headers for Type2(Large). The headers for Type1(Small) are set

Stage 1 of the code works OK. However I am having problems with Stage2. Nothing happens! I have been tinkering around with it pulling snippets of code found on the forum to no avail. Any help would be much appreciated.

I have highlighted the problem area of the code in red and provided some sample data of what I am trying to achieve immediately below.


Sample data:

Stage 1
GroupType
BSmall
BSmall
BLarge
BLarge
BLarge
BLarge
BLarge
BLarge
BLarge
BLarge

<tbody>
</tbody>

Stage 2


GroupType
BSmall
BSmall
GroupType
BLarge
BLarge
BLarge
BLarge
BLarge
BLarge
BLarge
BLarge

<tbody>
</tbody>

Here is my code
(Problem area highlighted in red)


Sub Test()
Application.DisplayAlerts = False
Application.EnableEvents = True


Dim Grp As Workbook
Dim Lastrow As Integer
Dim fnRange As Range
Dim i As Integer
Dim erow As Integer
Dim Month As String
Dim finalRow As Integer
Month = "Nov"
Lastrow = ActiveSheet.Range("B" & Rows.Count).End(xlUp).Row
For i = 1 To Lastrow
Set fnRange = Cells.Find("Small", Cells(8), xlValues, , xlByColumns)


If Cells(i, 1) = 2 Then
Worksheets("Grp").Range(Cells(i, 2), Cells(i, 9)).Select
Selection.Copy
Workbooks.Open Filename:="C:\Users\YV\Desktop\VBA\TestBook.xlsx"
Worksheets(Month).Select
erow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Offset(1, 1).Row
ActiveSheet.Cells(erow, 1).Select
ActiveSheet.PasteSpecial
Set fnRange = Cells.Find("Small", Cells(8), xlValues, , xlByColumns)
Rows(fnRange.Row + 1 & "1:1" & fnRange.Row + 5).Insert
ActiveWorkbook.Save
ActiveWorkbook.Close
Application.CutCopyMode = False
End If


Next i




End Sub
 
Upvote 0

Forum statistics

Threads
1,214,632
Messages
6,120,655
Members
448,975
Latest member
sweeberry

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