Insert rows based on countif value

Jmac2604

New Member
Joined
Jun 11, 2021
Messages
15
Office Version
  1. 365
Platform
  1. Windows
I have the follwing code which will the data from sheet1 and count the occurence in sheet 2 and then copy the data multiple times based on that countif value. But Instead of just copying it needs to insert rows and then copy the data based on the countif value from sheet 2. Can anyone help me on this?

Sub test()
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim i As Byte, StartCell As Range
Dim nList As Range, Ce As Range
Dim LastRow As Range

Set ws1 = Sheets("Sheet2")
Set ws2 = Sheets("Sheet1")
Set nList = ws2.Range("C5:C9")
Set StartCell = ws2.Range("D5")

For Each Ce In nList
i = Application.WorksheetFunction.CountIf(ws1.Columns(1), Ce)
If i > 0 Then StartCell.Rows.Resize(i, 1) = Ce
Set StartCell = StartCell.Offset(i, 0)

Next Ce
End Sub


How I am getting the result is:

1623416519775.png


Expected is :

1623416628964.png
 

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.

Forum statistics

Threads
1,214,787
Messages
6,121,569
Members
449,038
Latest member
Guest1337

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