SOLVED
Home

Inserting Rows Automatically

%3CLINGO-SUB%20id%3D%22lingo-sub-832180%22%20slang%3D%22en-US%22%3EInserting%20Rows%20Automatically%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-832180%22%20slang%3D%22en-US%22%3E%3CP%3EI%20have%20created%20a%20table%20and%20in%20column%20A%20I%20have%20the%20departments%20and%20in%20column%20B%20I%20have%20item%20numbers.%20What%20i%20want%20to%20be%20able%20to%20do%20automatically%20is%20when%20I%20enter%20an%20additional%20number%20onto%20column%20B%20it%20inserts%20a%20row%20so%20that%20I%20can%20then%20increase%20the%20amount%20of%20numbers%20I%20want%20to%20put%20into%20column%20B%20under%20the%20same%20department.%3C%2FP%3E%3CP%3EThis%20way%20I%20can%20keep%20adding%20numbers%20to%20column%20B%20under%20each%20department.%20This%20will%20expand%20the%20spreadsheet.%3C%2FP%3E%3CP%3EAny%20help%20would%20be%20much%20appreciated.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-832180%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-832225%22%20slang%3D%22en-US%22%3ERe%3A%20Inserting%20Rows%20Automatically%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-832225%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F401398%22%20target%3D%22_blank%22%3E%40adstristar%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWhy%20not%20upload%20a%20sample%20file%20with%20some%20dummy%20data%20to%20show%20us%20what%20you%20have%20and%20mock%20up%20the%20desired%20output%20manually%20on%20another%20sheet%20and%20explain%20about%20the%20steps%20you%20would%20like%20to%20automate%20to%20get%20the%20desired%20output%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-832228%22%20slang%3D%22en-US%22%3ERe%3A%20Inserting%20Rows%20Automatically%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-832228%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F401398%22%20target%3D%22_blank%22%3E%40adstristar%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHi%3C%2FP%3E%3CP%3EI%20can%20understand%20your%20question%20in%202%20different%20ways%3A%3C%2FP%3E%3CP%3E1-%20when%20you%20add%20a%20number%20ANYWHERE%26nbsp%3B%20in%20column%20B%20you%20want%20a%20new%20row%20inserted%20at%20the%20bottom%20of%20the%20Table.%3C%2FP%3E%3CP%3E2-%20When%20you%20add%20a%20number%20in%20the%20LAST%20empty%20cell%20in%20column%20B%20in%20your%20table%20you%20need%20a%20new%20row%20to%20be%20added%20to%20the%20table.%3C%2FP%3E%3CP%3EFor%20option%201%2C%20you%20can%20add%20a%20spin%20button%20that%20Inserts%20or%20delete%20rows.%20If%20you%20are%20familiar%20with%20VBA%20and%20inserting%20controls%20then%20insert%20a%20spin%20button%20control%20and%20write%20this%20code%20for%20the%20button.%20It%20will%20insert%2FDelete%20a%20row%20with%20each%20click%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EPrivate%20Sub%20SpinButton1_SpinDown()%3C%2FP%3E%3CP%3E%26nbsp%3B%20Dim%20LastRow%20As%20Long%3C%2FP%3E%3CP%3E%26nbsp%3B%20%26nbsp%3B%20LastRow%20%3D%20Sheets(1).Cells(Rows.Count%2C%202).End(xlUp).Row%3C%2FP%3E%3CP%3E%26nbsp%3B%20%26nbsp%3B%20Rows(LastRow%20-%201).EntireRow.Delete%3C%2FP%3E%3CP%3EEnd%20Sub%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EPrivate%20Sub%20SpinButton1_SpinUp()%3C%2FP%3E%3CP%3E%26nbsp%3B%20Dim%20LastRow%20As%20Long%3C%2FP%3E%3CP%3E%26nbsp%3B%20%26nbsp%3B%20LastRow%20%3D%20Sheets(1).Cells(Rows.Count%2C%202).End(xlUp).Row%3C%2FP%3E%3CP%3E%26nbsp%3B%20%26nbsp%3B%20Rows(LastRow).EntireRow.Insert%3C%2FP%3E%3CP%3EEnd%20Sub%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EFor%20Option%202%20you%20also%20need%20to%20write%20a%20code%20for%20inserting%20a%20row%20but%20this%20time%2C%20attach%20it%20to%20the%20%3CSTRONG%3EChange%20Event%20of%20the%20first%20empty%20cell%20in%20column%20B.%3C%2FSTRONG%3E..%26nbsp%3B%3C%2FP%3E%3CP%3EIn%20either%20case%20I%20would%20have%20a%20preference%20to%20do%20it%20manually%20using%20the%20regular%20functionality%20of%20Excel.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EGood%20Luck%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-832449%22%20slang%3D%22en-US%22%3ERe%3A%20Inserting%20Rows%20Automatically%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-832449%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F365248%22%20target%3D%22_blank%22%3E%40nabilmourad%3C%2FA%3Ethank%20you%20for%20your%20reply....%20I%20am%20not%20wanting%20to%20insert%20another%20row%20at%20the%20bottom...%20So%20if%20you%20take%20column%20A%26nbsp%3B%20in%20each%20row%20there%20is%20a%20department%20lets%20say%20for%20instance%20it%20is%20department%201%20to%2010%20.%20What%20I%20want%20to%20be%20able%20to%20do%20is%20to%20put%20info%20into%20column%20B%20next%20to%20the%20department%20number%20and%20then%20be%20able%20to%20press%20enter%20and%20it%20will%20give%20me%20another%20row%20under%20it.%20This%20means%20I%20can%20add%20more%20info%20onto%20department%201.%20It%20will%20end%20up%20looking%20like%20say%2010%20bits%20of%20info%20under%20dept1%20and%20maybe%2020%20bits%20of%20info%20under%20department%202%20and%20so%20on...%20Does%20that%20make%20sense.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIf%20not%2C%20lets%20say%20column%20A%20row%201%20I%20will%20put%20a%20department%20number%20in%20it.%20I%20want%20to%20add%20info%20into%20column%20B%20row%201%20and%20when%20I%20press%20enter%20it%20will%20give%20me%20another%20row%20which%20would%20be%20row%202%20but%20there%20will%20already%20be%20in%20Column%20A%20row%202%20another%20department%20number%20but%20by%20pressing%20enter%20after%20the%20former%20I%20have%20mentioned%20it%20will%20move%20that%20one%20to%20Column%20A%20row%203%20and%20so%20on.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-832450%22%20slang%3D%22en-US%22%3ERe%3A%20Inserting%20Rows%20Automatically%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-832450%22%20slang%3D%22en-US%22%3ESorry%20forgot%20to%20mention%20I%20am%20pretty%20na%C3%AFve%20and%20do%20not%20know%20about%20VBA%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-832451%22%20slang%3D%22en-US%22%3ERe%3A%20Inserting%20Rows%20Automatically%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-832451%22%20slang%3D%22en-US%22%3Eplease%20see%20my%20response%20to%20other%20reply.Thank%20you%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-832528%22%20slang%3D%22en-US%22%3ERe%3A%20Inserting%20Rows%20Automatically%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-832528%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F401398%22%20target%3D%22_blank%22%3E%40adstristar%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EPlease%20place%20the%20following%20code%20on%20Sheet%20Module%20of%20the%20Sheet%20in%20which%20you%20want%20this%20functionality.%3C%2FP%3E%3CP%3ETo%20do%20so%2C%20right%20click%20on%20Sheet%20Tab%20--%26gt%3B%20View%20Code%20and%20paste%20the%20code%20given%20below%20into%20the%20opened%20code%20window%20--%26gt%3B%20Close%20the%20VB%20Editor%20--%26gt%3B%20Save%20your%20Workbook%20as%20Macro-Enabled%20Workbook%20or%20with%20any%20file%20format%20which%20supports%20Macros%20e.g.%20.xlsm%20or%20.xls%20or%20.xlsb.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3EPrivate%20Sub%20Worksheet_Change(ByVal%20Target%20As%20Range)%0AIf%20Target.CountLarge%20%26gt%3B%201%20Then%20Exit%20Sub%0AOn%20Error%20GoTo%20Skip%3A%0AIf%20Not%20Intersect(Target%2C%20Range(%22B%3AB%22))%20Is%20Nothing%20Then%0A%20%20%20%20Application.EnableEvents%20%3D%20False%0A%20%20%20%20If%20Target.Offset(-1%2C%200)%20%26lt%3B%26gt%3B%20%22%22%20Then%0A%20%20%20%20%20%20%20%20If%20Target%20%26lt%3B%26gt%3B%20%22%22%20Then%0A%20%20%20%20%20%20%20%20%20%20%20%20Rows(Target.Row%20%2B%201).Insert%0A%20%20%20%20%20%20%20%20%20%20%20%20Target.Offset(1%2C%20-1)%20%3D%20Target.Offset(0%2C%20-1)%0A%20%20%20%20%20%20%20%20Else%0A%20%20%20%20%20%20%20%20%20%20%20%20If%20Application.CountIf(Columns(1)%2C%20Target.Offset(0%2C%20-1).Value)%20%26gt%3B%201%20Then%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Rows(Target.Row).Delete%0A%20%20%20%20%20%20%20%20%20%20%20%20End%20If%0A%20%20%20%20%20%20%20%20End%20If%0A%20%20%20%20End%20If%0AEnd%20If%0A%0ASkip%3A%0AApplication.EnableEvents%20%3D%20True%0AEnd%20Sub%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3EPlease%20find%20the%20attached%20with%20the%20code%20on%20Sheet1%20Module%20and%20to%20test%20the%20code%2C%20start%20entering%20the%20Info%20in%20column%20B%20for%20each%20department%20in%20column%20A%20and%20see%20if%20this%20is%20what%20you%20were%20trying%20to%20achieve.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-832686%22%20slang%3D%22en-US%22%3ERe%3A%20Inserting%20Rows%20Automatically%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-832686%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F394231%22%20target%3D%22_blank%22%3E%40Subodh_Tiwari_sktneer%3C%2FA%3E%26nbsp%3B%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%2052px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F129573iFD3D26F7134B363A%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22Macro.PNG%22%20title%3D%22Macro.PNG%22%20%2F%3E%3C%2FSPAN%3EThank%20you%20for%20your%20reply.%20It%20is%20very%20much%20what%20I%20was%20looking%20for%20however%20if%20you%20look%20on%20the%20picture%20(%20hope%20you%20can%20see%20it)%20there%20are%20certain%20rows%20that%20it%20will%20not%20work%20on%20.%20So%20by%20looking%20at%20column%20A%20row%202%20has%20worked%20but%20(row1)%2C%20row%204%2Crow%207%2C%20row10%2Crow%2013%2Crow16%2Crow%2019%2C%20and%20every%20third%20row%20there%20on%20in%20does%20not%20work.%20I%20used%20Row%201%20for%20a%20heading%20although%20I%20can%20use%20the%20column%20heading%20for%20that.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIs%20there%20a%20way%20of%20amending%20this%20macro%20so%20that%20it%20will%20work%20with%20every%20line%3F%20Also%20can%20I%20format%20this%20into%20a%20table%20%3FNot%20to%20worries%20about%20the%20latter%20though.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-832703%22%20slang%3D%22en-US%22%3ERe%3A%20Inserting%20Rows%20Automatically%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-832703%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F401398%22%20target%3D%22_blank%22%3E%40adstristar%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20have%20tweaked%20the%20code.%20Please%20replace%20the%20existing%20code%20with%20the%20following%20code%20and%20let%20me%20know%20if%20this%20works%20as%20desired%20now.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3EPrivate%20Sub%20Worksheet_Change(ByVal%20Target%20As%20Range)%0AIf%20Target.CountLarge%20%26gt%3B%201%20Then%20Exit%20Sub%0AOn%20Error%20GoTo%20Skip%3A%0AIf%20Not%20Intersect(Target%2C%20Range(%22B%3AB%22))%20Is%20Nothing%20Then%0A%20%20%20%20Application.EnableEvents%20%3D%20False%0A%20%20%20%20If%20Target.Offset(0%2C%20-1)%20%26lt%3B%26gt%3B%20%22%22%20Then%0A%20%20%20%20%20%20%20%20If%20Target%20%26lt%3B%26gt%3B%20%22%22%20Then%0A%20%20%20%20%20%20%20%20%20%20%20%20Rows(Target.Row%20%2B%201).Insert%0A%20%20%20%20%20%20%20%20%20%20%20%20Target.Offset(1%2C%20-1)%20%3D%20Target.Offset(0%2C%20-1)%0A%20%20%20%20%20%20%20%20Else%0A%20%20%20%20%20%20%20%20%20%20%20%20If%20Application.CountIf(Columns(1)%2C%20Target.Offset(0%2C%20-1).Value)%20%26gt%3B%201%20Then%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Rows(Target.Row).Delete%0A%20%20%20%20%20%20%20%20%20%20%20%20End%20If%0A%20%20%20%20%20%20%20%20End%20If%0A%20%20%20%20End%20If%0AEnd%20If%0A%0ASkip%3A%0AApplication.EnableEvents%20%3D%20True%0AEnd%20Sub%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIf%20that%20takes%20care%20of%20your%20original%20question%2C%20please%20mark%20your%20question%20as%20Solved%20by%20accepting%20the%20Answer.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-832739%22%20slang%3D%22en-US%22%3ERe%3A%20Inserting%20Rows%20Automatically%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-832739%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F394231%22%20target%3D%22_blank%22%3E%40Subodh_Tiwari_sktneer%3C%2FA%3EThank%20you%20it%20resolves%20my%20original%20question%20yes%20%E2%80%A6%20am%20I%20able%20to%20use%20this%20in%20a%20table%20as%20well%3F%20Also%20how%20do%20I%20mark%20it%20as%20solved%20please%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-832746%22%20slang%3D%22en-US%22%3ERe%3A%20Inserting%20Rows%20Automatically%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-832746%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F401398%22%20target%3D%22_blank%22%3E%40adstristar%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EGreat!%20Glad%20it%20worked%20as%20desired.%3C%2FP%3E%3CP%3EYes%2C%20it%20will%20work%20even%20if%20the%20data%20is%20in%20an%20Excel%20Table.%20But%20right%20now%2C%20the%20code%20is%20inserting%20the%20entire%20row%20whereas%20in%20case%20of%20a%20table%20only%20a%20table%20row%20can%20be%20inserted%20instead%20of%20inserting%20entire%20row.%20If%20you%20have%20any%20such%20requirement%2C%20you%20may%20open%20a%20New%20Question.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EA%20question%20is%20marked%20as%20Solved%20once%20a%20post%20is%20accepted%20as%20an%20Answer%20or%20a%20Solution%20and%20you%20have%20successfully%20done%20that.%3C%2FP%3E%3C%2FLINGO-BODY%3E
adstristar
Occasional Contributor

I have created a table and in column A I have the departments and in column B I have item numbers. What i want to be able to do automatically is when I enter an additional number onto column B it inserts a row so that I can then increase the amount of numbers I want to put into column B under the same department.

This way I can keep adding numbers to column B under each department. This will expand the spreadsheet.

Any help would be much appreciated.

10 Replies

@adstristar 

Why not upload a sample file with some dummy data to show us what you have and mock up the desired output manually on another sheet and explain about the steps you would like to automate to get the desired output?

@adstristar 

Hi

I can understand your question in 2 different ways:

1- when you add a number ANYWHERE  in column B you want a new row inserted at the bottom of the Table.

2- When you add a number in the LAST empty cell in column B in your table you need a new row to be added to the table.

For option 1, you can add a spin button that Inserts or delete rows. If you are familiar with VBA and inserting controls then insert a spin button control and write this code for the button. It will insert/Delete a row with each click

 

Private Sub SpinButton1_SpinDown()

  Dim LastRow As Long

    LastRow = Sheets(1).Cells(Rows.Count, 2).End(xlUp).Row

    Rows(LastRow - 1).EntireRow.Delete

End Sub

 

Private Sub SpinButton1_SpinUp()

  Dim LastRow As Long

    LastRow = Sheets(1).Cells(Rows.Count, 2).End(xlUp).Row

    Rows(LastRow).EntireRow.Insert

End Sub

 

 

For Option 2 you also need to write a code for inserting a row but this time, attach it to the Change Event of the first empty cell in column B... 

In either case I would have a preference to do it manually using the regular functionality of Excel.

 

Good Luck

@nabilmouradthank you for your reply.... I am not wanting to insert another row at the bottom... So if you take column A  in each row there is a department lets say for instance it is department 1 to 10 . What I want to be able to do is to put info into column B next to the department number and then be able to press enter and it will give me another row under it. This means I can add more info onto department 1. It will end up looking like say 10 bits of info under dept1 and maybe 20 bits of info under department 2 and so on... Does that make sense.

 

If not, lets say column A row 1 I will put a department number in it. I want to add info into column B row 1 and when I press enter it will give me another row which would be row 2 but there will already be in Column A row 2 another department number but by pressing enter after the former I have mentioned it will move that one to Column A row 3 and so on.

 

Sorry forgot to mention I am pretty naïve and do not know about VBA
please see my response to other reply.Thank you

@adstristar 

 

Please place the following code on Sheet Module of the Sheet in which you want this functionality.

To do so, right click on Sheet Tab --> View Code and paste the code given below into the opened code window --> Close the VB Editor --> Save your Workbook as Macro-Enabled Workbook or with any file format which supports Macros e.g. .xlsm or .xls or .xlsb.

 

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.CountLarge > 1 Then Exit Sub
On Error GoTo Skip:
If Not Intersect(Target, Range("B:B")) Is Nothing Then
    Application.EnableEvents = False
    If Target.Offset(-1, 0) <> "" Then
        If Target <> "" Then
            Rows(Target.Row + 1).Insert
            Target.Offset(1, -1) = Target.Offset(0, -1)
        Else
            If Application.CountIf(Columns(1), Target.Offset(0, -1).Value) > 1 Then
                Rows(Target.Row).Delete
            End If
        End If
    End If
End If

Skip:
Application.EnableEvents = True
End Sub

Please find the attached with the code on Sheet1 Module and to test the code, start entering the Info in column B for each department in column A and see if this is what you were trying to achieve.

 

 

 

@Subodh_Tiwari_sktneer Macro.PNG Thank you for your reply. It is very much what I was looking for however if you look on the picture ( hope you can see it) there are certain rows that it will not work on . So by looking at column A row 2 has worked but (row1), row 4,row 7, row10,row 13,row16,row 19, and every third row there on in does not work. I used Row 1 for a heading although I can use the column heading for that. 

 

Is there a way of amending this macro so that it will work with every line? Also can I format this into a table ?Not to worries about the latter though.

 

Solution

@adstristar 

I have tweaked the code. Please replace the existing code with the following code and let me know if this works as desired now.

 

 

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.CountLarge > 1 Then Exit Sub
On Error GoTo Skip:
If Not Intersect(Target, Range("B:B")) Is Nothing Then
    Application.EnableEvents = False
    If Target.Offset(0, -1) <> "" Then
        If Target <> "" Then
            Rows(Target.Row + 1).Insert
            Target.Offset(1, -1) = Target.Offset(0, -1)
        Else
            If Application.CountIf(Columns(1), Target.Offset(0, -1).Value) > 1 Then
                Rows(Target.Row).Delete
            End If
        End If
    End If
End If

Skip:
Application.EnableEvents = True
End Sub

 

 

If that takes care of your original question, please mark your question as Solved by accepting the Answer.

 

@Subodh_Tiwari_sktneerThank you it resolves my original question yes … am I able to use this in a table as well? Also how do I mark it as solved please?

@adstristar 

Great! Glad it worked as desired.

Yes, it will work even if the data is in an Excel Table. But right now, the code is inserting the entire row whereas in case of a table only a table row can be inserted instead of inserting entire row. If you have any such requirement, you may open a New Question.

 

A question is marked as Solved once a post is accepted as an Answer or a Solution and you have successfully done that.

Related Conversations
Tabs and Dark Mode
cjc2112 in Discussions on
16 Replies
Stable version of Edge insider browser
HotCakeX in Discussions on
35 Replies
flashing a white screen while open new tab
cntvertex in Discussions on
11 Replies
How to Prevent Teams from Auto-Launch
chenrylee in Microsoft Teams on
28 Replies