SOLVED
Home

Changing no. of rows in table based on value in drop down menu

%3CLINGO-SUB%20id%3D%22lingo-sub-663552%22%20slang%3D%22en-US%22%3EChanging%20no.%20of%20rows%20in%20table%20based%20on%20value%20in%20drop%20down%20menu%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-663552%22%20slang%3D%22en-US%22%3E%3CP%3EHello%2C%26nbsp%3B%3C%2FP%3E%3CP%3EI%20have%20been%20looking%20for%20a%20way%20to%20change%20the%20no.%20of%20rows%20in%20the%20table%20dependent%20on%20the%20no.%20of%20factors%20selected%20(see%20attached%20image).%20I.e.%20if%20you%20select%204%20from%20the%20drop%20down%20bar%2C%20I%20want%20another%20row%20to%20appear%20below%20the%20'cycle'%20row.%20I've%20googled%20for%20about%202%20hrs%20and%20looked%20in%20conditional%20formatting%2C%20but%20i%20assume%20its%20an%20'If'%20rule.%20If%20anyone%20knows%20how%20to%20do%20it%20please%20let%20me%20know%20and%20thanks%20in%20advance!%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20556px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F116916iE378C7DB4CBD7BDD%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22Screen%20Shot%202019-06-03%20at%2011.45.38.png%22%20title%3D%22Screen%20Shot%202019-06-03%20at%2011.45.38.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-663552%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EExcel%20on%20Mac%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EFormulas%20and%20Functions%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-663625%22%20slang%3D%22en-US%22%3ERe%3A%20Changing%20no.%20of%20rows%20in%20table%20based%20on%20value%20in%20drop%20down%20menu%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-663625%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F353367%22%20target%3D%22_blank%22%3E%40leena2270%3C%2FA%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHi%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EYou%20need%20to%20VBA%20code%20to%20do%20that!%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20suggest%20using%20this%20one%3A%3C%2FP%3E%3CPRE%3EPrivate%20Sub%20Worksheet_Change(ByVal%20Target%20As%20Range)%3CBR%20%2F%3E%20If%20Target.Address(False%2C%20False)%20%3D%20%22B1%22%20Then%3CBR%20%2F%3E%20%20%20Dim%20no%20As%20Integer%2C%20counter%20As%20Integer%3CBR%20%2F%3E%20%20%20no%20%3D%20Range(%22B1%22).Value%3CBR%20%2F%3E%3CBR%20%2F%3E%20%20%20If%20no%20%3D%200%20Then%20Exit%20Sub%3CBR%20%2F%3E%3CBR%20%2F%3E%20%20%20Cells.Find(What%3A%3D%22factor%20name%22%2C%20After%3A%3DActiveCell%2C%20LookIn%3A%3DxlFormulas%2C%20_%3CBR%20%2F%3E%20%20%20LookAt%3A%3DxlPart%2C%20SearchOrder%3A%3DxlByRows%2C%20SearchDirection%3A%3DxlNext%2C%20_%3CBR%20%2F%3E%20%20%20MatchCase%3A%3DFalse%2C%20SearchFormat%3A%3DFalse).Activate%3CBR%20%2F%3E%3CBR%20%2F%3E%20%20%20ActiveCell.End(xlDown).Range(%22A1%3AD1%22).Select%3CBR%20%2F%3E%20%20%20Selection.Copy%3CBR%20%2F%3E%20%20%20ActiveCell.Offset(1%2C%200).Range(%22A1%3AD%22%20%26amp%3B%20no).Select%3CBR%20%2F%3E%20%20%20Selection.PasteSpecial%20Paste%3A%3DxlPasteFormats%2C%20Operation%3A%3DxlNone%2C%20_%3CBR%20%2F%3E%20%20%20SkipBlanks%3A%3DFalse%2C%20Transpose%3A%3DFalse%3CBR%20%2F%3E%20%20%20Application.CutCopyMode%20%3D%20False%3CBR%20%2F%3E%20%20%20ActiveCell.Select%3CBR%20%2F%3E%20End%20If%3CBR%20%2F%3EEnd%20Sub%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EPlease%20find%20it%20in%20the%20attached%20file.%3C%2FP%3E%3CP%3EThe%20code%20is%20stored%20in%20the%20worksheet%20module%2C%20to%20see%20it%3A%3C%2FP%3E%3CP%3EHover%20the%20mouse%20over%20the%20worksheet%20tab%2C%20right-click%2C%20and%20select%20View%20Code.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHope%20that%20helps%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-663700%22%20slang%3D%22en-US%22%3ERe%3A%20Changing%20no.%20of%20rows%20in%20table%20based%20on%20value%20in%20drop%20down%20menu%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-663700%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F35679%22%20target%3D%22_blank%22%3E%40Haytham%20Amairah%3C%2FA%3E%26nbsp%3Bthank%20you%20so%20much%2C%20greatly%20appreciated!%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-663767%22%20slang%3D%22en-US%22%3ERe%3A%20Changing%20no.%20of%20rows%20in%20table%20based%20on%20value%20in%20drop%20down%20menu%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-663767%22%20slang%3D%22en-US%22%3E%3CP%3EHi%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F35679%22%20target%3D%22_blank%22%3E%40Haytham%20Amairah%3C%2FA%3E%2C%26nbsp%3B%3C%2FP%3E%3CP%3EI'm%20still%20having%20some%20struggles%2C%20the%20page%20that%20i%20am%20talking%20about%20is%20titled%20instructions.%20When%20I%20open%20'Visual%20Basic'%20and%20copy%20the%20given%20code%2C%20adjusting%20'B1'%20to%20D5%20i%20get%20errors%20at%3A%26nbsp%3B%3C%2FP%3E%3CPRE%3E%20ActiveCell.End(xlDown).Range(%22A1%3AD1%22).Select%3CBR%20%2F%3E%20%20%20Selection.Copy%3CBR%20%2F%3E%20%20%20ActiveCell.Offset(1%2C%200).Range(%22A1%3AD%22%20%26amp%3B%20no).%26nbsp%3B%3C%2FPRE%3E%3CP%3E%26nbsp%3Beven%20when%20I%20change%20A1%3AD1%20to%20A5%3AD5.%20My%20apologies%20to%20trouble%20you%20again%2C%20but%20if%20you%20do%20have%20time%2C%20do%20you%20have%20any%20suggestions%20on%20how%20to%20fix%20this%3F%26nbsp%3B%3C%2FP%3E%3CP%3EKind%20regards%2C%26nbsp%3B%3C%2FP%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F353367%22%20target%3D%22_blank%22%3E%40leena2270%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-663769%22%20slang%3D%22en-US%22%3ERe%3A%20Changing%20no.%20of%20rows%20in%20table%20based%20on%20value%20in%20drop%20down%20menu%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-663769%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F353367%22%20target%3D%22_blank%22%3E%40leena2270%3C%2FA%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI've%20adjusted%20the%20code%20to%20fit%20with%20your%20worksheet.%3C%2FP%3E%3CP%3EPlease%20find%20the%20attached%20file.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ERegards%2C%3C%2FP%3E%3CP%3EHaytham%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-664325%22%20slang%3D%22en-US%22%3ERe%3A%20Changing%20no.%20of%20rows%20in%20table%20based%20on%20value%20in%20drop%20down%20menu%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-664325%22%20slang%3D%22en-US%22%3E%3CP%3EHi%2C%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F35679%22%20target%3D%22_blank%22%3E%40Haytham%20Amairah%3C%2FA%3E%26nbsp%3BI%20am%20back%20again%20with%20another%20question.%3C%2FP%3E%3CP%3EI%20wanted%20to%20remove%20the%20first%20three%20rows%20because%20they%20were%20just%20example%20factors%20for%20the%20customer%20to%20put%20in.%20But%20if%20i%20remove%20the%20three%20rows%20and%20paste%20the%20same%20code%20a%20bug%20error%20comes%20up.%20I%20have%20also%20tried%20to%20find%20a%20code%20to%20clear%20the%20first%20cells%20but%20that%20hasn't%20worked%20either.%20Could%20you%20explain%20to%20me%20how%20you%20select%20the%20the%20range%20(A1%3AD1)%20and%20the%20ActiveCell.Offset%20range%3F%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-664640%22%20slang%3D%22en-US%22%3ERe%3A%20Changing%20no.%20of%20rows%20in%20table%20based%20on%20value%20in%20drop%20down%20menu%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-664640%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F353367%22%20target%3D%22_blank%22%3E%40leena2270%3C%2FA%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI've%20fixed%20the%20code%20to%20handle%20that%20errors.%3C%2FP%3E%3CP%3EI've%20also%20added%20a%20button%20to%20clear%20all%20the%20factors%20to%20start%20over.%3C%2FP%3E%3CP%3EPlease%20note%20that%20you%20have%20to%20keep%20the%20workbook%20saved%20in%20(.xlsm)%20extension%20in%20order%20to%20keep%20the%20codes%20stored%20in%20it.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWith%20regards%20to%20%3CSTRONG%3EActiveCell.Offset%3C%2FSTRONG%3E%2C%20the%20Offset%20is%20one%20of%20ActiveCell%20properties%2C%20we%20can%20use%20it%20to%20move%20away%20from%20the%20active%20cell%20in%20a%20certain%20number%20of%20rows%20and%20columns.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWhile%20%3CSTRONG%3ERange(%22A1%3AD1%22).Select%3C%2FSTRONG%3E%20is%20to%20tell%20the%20VBA%20compiler%20to%20select%20a%20range%20relative%20to%20the%20active%20cell%20but%20make%20the%20selected%20range%20starting%20from%20the%20active%20cell%20in%20the%20same%20size%20as%20if%20you%20start%20from%20cell%20A1%20to%20cell%20D1.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20hope%20that%20makes%20sense.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-664692%22%20slang%3D%22en-US%22%3ERe%3A%20Changing%20no.%20of%20rows%20in%20table%20based%20on%20value%20in%20drop%20down%20menu%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-664692%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F353367%22%20target%3D%22_blank%22%3E%40leena2270%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ESorry!%3C%2FP%3E%3CP%3EIt%20seems%20that%20there%20is%20another%20bug.%3C%2FP%3E%3CP%3EBut%20I've%20fixed%20it%20in%20the%20attached%20workbook.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-665937%22%20slang%3D%22en-US%22%3ERe%3A%20Changing%20no.%20of%20rows%20in%20table%20based%20on%20value%20in%20drop%20down%20menu%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-665937%22%20slang%3D%22en-US%22%3E%3CP%3EHi%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F35679%22%20target%3D%22_blank%22%3E%40Haytham%20Amairah%3C%2FA%3E%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIs%20it%20correct%20to%20assume%20that%20neither%20the%20ActiveCell.Offset%20or%20the%20range%20determine%20the%20placement%20of%20the%20rows%20in%20the%20table%3F%26nbsp%3B%3C%2FP%3E%3CP%3EI%20thank%20you%20very%20much%20for%20your%20time%20and%20patience%20to%20explain%20this%20to%20me%2C%20and%20for%20your%20help.%20I%20really%20appreciate%20it%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-667227%22%20slang%3D%22en-US%22%3ERe%3A%20Changing%20no.%20of%20rows%20in%20table%20based%20on%20value%20in%20drop%20down%20menu%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-667227%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F353367%22%20target%3D%22_blank%22%3E%40leena2270%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIf%20you%20looking%20at%20the%20code%20you%20will%20see%20these%20lines%3A%3C%2FP%3E%3CPRE%3ECells.Find(What%3A%3D%22factor%20name%22%2C%20After%3A%3DCells(1%2C%201)%2C%20LookIn%3A%3DxlFormulas%2C%20_%3CBR%20%2F%3ELookAt%3A%3DxlPart%2C%20SearchOrder%3A%3DxlByRows%2C%20SearchDirection%3A%3DxlNext%2C%20_%3CBR%20%2F%3EMatchCase%3A%3DFalse%2C%20SearchFormat%3A%3DFalse).Activate%3C%2FPRE%3E%3CP%3EThese%20lines%20will%20move%20the%20cursor%20to%20the%20starting%20cell%20and%20activated%20it%20which%20is%20the%20cell%20that%20holds%20the%20name%20of%20(Factor%20Name).%3C%2FP%3E%3CP%3EAfter%20we%20define%20the%20starting%20cell%2C%20we%20can%20use%20the%20other%20properties%20such%20as%20%3CSTRONG%3EOffset%3C%2FSTRONG%3E%20to%20move%20%3CSPAN%20class%3D%22tlid-translation%20translation%22%3E%3CSPAN%20class%3D%22%22%3Ein%20any%20direction%20and%20execute%20the%20operations%20that%20we%20want%3C%2FSPAN%3E%3C%2FSPAN%3E.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHope%20that%20helps%3C%2FP%3E%3C%2FLINGO-BODY%3E
leena2270
Occasional Contributor

Hello, 

I have been looking for a way to change the no. of rows in the table dependent on the no. of factors selected (see attached image). I.e. if you select 4 from the drop down bar, I want another row to appear below the 'cycle' row. I've googled for about 2 hrs and looked in conditional formatting, but i assume its an 'If' rule. If anyone knows how to do it please let me know and thanks in advance!Screen Shot 2019-06-03 at 11.45.38.png

9 Replies
Solution

@leena2270

 

Hi,

 

You need to VBA code to do that!

 

I suggest using this one:

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address(False, False) = "B1" Then
Dim no As Integer, counter As Integer
no = Range("B1").Value

If no = 0 Then Exit Sub

Cells.Find(What:="factor name", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate

ActiveCell.End(xlDown).Range("A1:D1").Select
Selection.Copy
ActiveCell.Offset(1, 0).Range("A1:D" & no).Select
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
ActiveCell.Select
End If
End Sub

 

Please find it in the attached file.

The code is stored in the worksheet module, to see it:

Hover the mouse over the worksheet tab, right-click, and select View Code.

 

Hope that helps

@Haytham Amairah thank you so much, greatly appreciated! 

Hi @Haytham Amairah

I'm still having some struggles, the page that i am talking about is titled instructions. When I open 'Visual Basic' and copy the given code, adjusting 'B1' to D5 i get errors at: 

 ActiveCell.End(xlDown).Range("A1:D1").Select
Selection.Copy
ActiveCell.Offset(1, 0).Range("A1:D" & no). 

 even when I change A1:D1 to A5:D5. My apologies to trouble you again, but if you do have time, do you have any suggestions on how to fix this? 

Kind regards, 

@leena2270 

@leena2270

 

I've adjusted the code to fit with your worksheet.

Please find the attached file.

 

Regards,

Haytham

Hi, @Haytham Amairah I am back again with another question.

I wanted to remove the first three rows because they were just example factors for the customer to put in. But if i remove the three rows and paste the same code a bug error comes up. I have also tried to find a code to clear the first cells but that hasn't worked either. Could you explain to me how you select the the range (A1:D1) and the ActiveCell.Offset range? 

@leena2270

 

I've fixed the code to handle that errors.

I've also added a button to clear all the factors to start over.

Please note that you have to keep the workbook saved in (.xlsm) extension in order to keep the codes stored in it.

 

With regards to ActiveCell.Offset, the Offset is one of ActiveCell properties, we can use it to move away from the active cell in a certain number of rows and columns.

 

While Range("A1:D1").Select is to tell the VBA compiler to select a range relative to the active cell but make the selected range starting from the active cell in the same size as if you start from cell A1 to cell D1.

 

I hope that makes sense.

@leena2270 

 

Sorry!

It seems that there is another bug.

But I've fixed it in the attached workbook.

Hi@Haytham Amairah,

 

Is it correct to assume that neither the ActiveCell.Offset or the range determine the placement of the rows in the table? 

I thank you very much for your time and patience to explain this to me, and for your help. I really appreciate it 

@leena2270 

 

If you looking at the code you will see these lines:

Cells.Find(What:="factor name", After:=Cells(1, 1), LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate

These lines will move the cursor to the starting cell and activated it which is the cell that holds the name of (Factor Name).

After we define the starting cell, we can use the other properties such as Offset to move in any direction and execute the operations that we want.

 

Hope that helps

Related Conversations
Tabs and Dark Mode
cjc2112 in Discussions on
46 Replies
Extentions Synchronization
Deleted in Discussions on
3 Replies
Stable version of Edge insider browser
HotCakeX in Discussions on
35 Replies
flashing a white screen while open new tab
Deleted in Discussions on
14 Replies
How to Prevent Teams from Auto-Launch
chenrylee in Microsoft Teams on
29 Replies
Security Community Webinars
Valon_Kolica in Security, Privacy & Compliance on
13 Replies