Add rows based on variable criteria

raeannb

Board Regular
Joined
Jun 21, 2011
Messages
86
Hi! My problem is a little complicated (for me! ...noob). I'll try to simplify my explanation a little, but feel free to ask questions if I do a cruddy job. Here it is:

I have a workbook open with four worksheets in it. Each worksheet has several tables, one on top of the other with a blank row in between, all starting in column A. I have an array I would like to use to populate the various columns of the tables, and the array is updated by a separate program (which actually works).

The number of columns in each table will always be the same; however the number of rows can change. The number of rows in the table should equal the number of rows in the array from the first program. I'm currently writing a Display() sub which should populate the tables using my array. I don't want to recreate the table headings every time I run the program, so I would like to have Display() detect and accomplish several things before writing any information. First, I want it to find the appropriate table by its title ("Schedule A", "Schedule B", and so on). Then I want it to find the next table underneath it (if one exists), and count the number of rows between the two. If the number of rows between them does not equal the number of rows in the array, I would like the program to insert blank rows until they are equal. This way it will not overwrite the title or headings of the following table.

I was able to get this to work when I tried filling in just one schedule in isolation, but once I wrote it for the additional tables, the program added infinite rows. Here's what I was using:

Code:
'Find first heading row for desired table, name the row "displayrow"
displayrow = .Columns(1).Find(What:=WS & ": Schedule " & Sch, After:=.Cells(2, 1), LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, searchdirection:=xlNext, MatchCase:=False, searchformat:=False).Row
 
'Make correct number of rows between schedules
Do Until nextrow - displayrow = k + 3  
'(the first table begins in row 3, k is the number of rows I want in the table)
nextrow = .Columns(1).Find(What:="Schedule ", After:=.Cells(displayrow, 1), LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, searchdirection:=xlNext, MatchCase:=False, searchformat:=False).Row
If nextrow - displayrow < k + 3 Then
.Rows(displayrow + 2).EntireRow.Insert
Else: Exit Do
End If
Loop

What can I do? There has to be a better way. Thank you in advance!! Any help is suuuuuuper appreciated.
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.

Forum statistics

Threads
1,224,583
Messages
6,179,671
Members
452,937
Latest member
Bhg1984

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