Macro problems, automatic bordering


Posted by Michael Winters on December 27, 2001 3:53 AM

To the resident macro expert:

I have been trying to fix this problem for a while now, it is one that seems simple, but is apparently beyond my abilities. Here is the situation: The first worksheet of this spreadsheet ("Training Matrix") has all the data for soldiers in my unit (PT, weapons qualifications, etc......) I have a macro (with a great deal of code based on a routine written by Juan Pablo) that is a fairly simple If..Then loop that sorts through the data on Training Matrix and looks for expired qualifications, missing data, etc.. It then takes the sorted data and puts it on another worksheet ("Deficiencies Roster") in the following format: each set of data is three columns. There are two sets of data on a page, so columns A,B,C have data, D is blank, E,F,G,H,I,J have data, K is blank, L,M,N,O,P,Q have data. Here is the problem: in the loop, the line
Rng.EntireRow.Insert Shift:=ShiftxlDown
inserts an entire row every time it needs to put in more data. This becomes a problem as each additional set of data then bumps down every previous set of data, so by the time the macro is finished, the first set of data (A,B,C) has two pages of blank data, then numerous blank rows between data. When i change that line to read
Rng.Insert xlShiftDown
the data doesn't display properly. So the first question is: how do I get it to not insert entire rows, but still display all the data properly? Second problem: Once all this sorting is done, i want to border each set of data. The problem I encountered here is that each set of data is a different length, and can change daily, so the macro must find the last used cell in the first column of each data set (IE A, E,H,L,O) and border that set of data. For example, if the first data set had twenty entries (last non-blank cell was A20), it would border A1:C20. If the second data set then had 30 entries (last non-blank cell was E30) then it would border E1:G30. This seems like it would be fairly simple to do, but I cannot figure out how to dynamically border a range. Hopefully one of you will see the solution that has so far eluded me. Thanks in advance for any help provided.

Sincerely,

Mike Winters
United States Army



Posted by Mike on December 27, 2001 1:00 PM

So it sounds like you need to just shift down the entries in A,B,C one spot when new data comes in for that set but not E-J and L-Q right? I would have to see all the code to fix that.

To create the borders I would put this code in the Worksheet_Change event for that sheet:

Bottom = 1
Do
Bottom = Bottom + 1
Loop Until Worksheets("Sheet1").Cells(Bottom, 1).Text = ""

Bottom = Bottom - 1

Worksheets("Sheet1").Range(Cells(1, 1), Cells(Bottom, 3)).Borders(xlEdgeBottom).LineStyle = xlDouble
Worksheets("Sheet1").Range(Cells(1, 1), Cells(Bottom, 3)).Borders(xlEdgeTop).LineStyle = xlDouble
Worksheets("Sheet1").Range(Cells(1, 1), Cells(Bottom, 3)).Borders(xlEdgeLeft).LineStyle = xlDouble
Worksheets("Sheet1").Range(Cells(1, 1), Cells(Bottom, 3)).Borders(xlEdgeRight).LineStyle = xlDouble

This should Create a boarder for your first data set. It won't clean up your old border though.

-Mike