简体   繁体   中英

Excel VBA Macros Format rows based on Values

Hey guys I have a table that I want to format with vba as it is a lot of values and it takes way too long formatting one by one.

example

Client ID ---------- Employee ---------- Hours

0001 ------------------Josh -------------5

0001 ------------------Carl -------------5

0001 ------------------Joe --------------5

0005 ------------------Ken --------------5

0005 ------------------Jeff --------------5

0008 ------------------Joe --------------5

0008 ------------------Josh -------------5

0009 ------------------Carl -------------5

0011 ------------------Joe --------------5

0011 ------------------Carl --------------5

I want to apply a border underneath the last of the same ClientID to separate them from the rest of the ClientID's so that it is easier to read when looking at information so the output should be something like this:

Client ID ---------- Employee ---------- Hours

0001 ------------------Josh -------------5

0001 ------------------Carl -------------5

0001 ------------------Joe --------------5


0005 ------------------Ken --------------5

0005 ------------------Jeff --------------5


0008 ------------------Joe --------------5

0008 ------------------Josh -------------5


0009 ------------------Carl -------------5


0011 ------------------Joe --------------5

0011 ------------------Carl --------------5


I am guessing I need some sort of loop that goes down ClientID Column and checks each ClientID..as soon as a new ClientID shows up then Add a border to the top of that Row or Bottom of previous one. I know I should have some code, if anyone can point me in the right direction or give me some clues I can continue to write something up.

屏幕截图

If you have data that will be in the format of the picture below:

在此处输入图片说明

Then following macro will add a border when the Client ID changes. This macro assumes that the Client IDs are already sorted, either ascending or descending.

Macro

Sub border()
'Macro valid & comments valid for Excel 2007 or greater

'Dimensions all the initial variables; it is good practice to declare all your variables at top and to capitalize one letter so that when VBA interprets the variable when you exit the row, it will automatically make the variable uppercase and you know you spelled it correctly

'variable for current row number when looping through all the data
Dim Ro As Integer
'variable for the maximum number of rows in the dataset
Dim mRo As Integer
'variable for the maximum number of columns in the dataset
Dim mCo As Integer

    'selects the first sheet in the active workbook to use as the parent object for all code contained in the WITH statement.  VBA is object oriented meaning there are a lot of parent-child relationships.  In this case, all the code inside the WITH statement is the parent, or containing object, and the internal code are the children objects
    With Sheets(1)
        'Determines last row in the range of all data
        'Uses .Rows.Count to find the last row in Excel (1,048,576 for Excel 2007+) and then goes all the way up (xlUp) until it hits a "different" type of cell.  Almost 99.9999% of the time, row 1,048,576 will be blank, so this will traverse up until it reaches the first non-blank row, or the bottom of our dataset.  This is similar to starting out at A1048576 and pressing Ctrl+Up Arrow
        mRo = .Cells(.Rows.Count, 1).End(xlUp).Row
        'Same action as the row, but goes to the left until it reaches non-blank data
        mCo = .Cells(1, .Columns.Count).End(xlToLeft).Column

        'Removes borders from original table
        .Range(.Cells(1, 1), .Cells(mRo, mCo)).Borders.LineStyle = xlNone            

        'loops through data to compare when a Client ID changes
        'Starts are row 2 and performs a for loop until it reaches the end of the dataset at the mRo row
        For Ro = 2 To mRo
            'Checks to see if the client ID changes & applies a border
            'Looks to see if the row below (Ro + 1) in the first column (Client ID) is different from the current row's (Ro) first column.  If there is a difference, then the Client ID just changed and we want to add a border
            If .Cells(Ro, 1).Value <> .Cells(Ro + 1, 1).Value Then
                'Adds border to bottom of rows
                'The range is selected from the current row (Ro) first column to the current row (Ro) last column (mCo).  The formatting of the border was actually determined from using the VBA "Record Macro" function (if you don't see "Developer" in your ribbon bar, click on either FILE or the Office symbol in the upper left hand corner of Excel, go to OPTIONS, CUSTOMIZE RIBBON and check the DEVELOPER checkbox on the right hand side.  Then, you will see the DEVELOPER tab in the ribbon bar and you can use the "Record Macro" feature
                With .Range(.Cells(ro, 1), .Cells(ro, mCo)).Borders(xlEdgeBottom)
                    .LineStyle = xlContinuous
                    .Weight = xlThick
                End With
            End If
        Next
    End With
End Sub

Result

在此处输入图片说明

With a Conditional Formatting formula rule of:

=$A1<>$A2  

applied to A:C, or possibly by adding a blank row as a separator (for example with Subtotal at each change in Client ID - which could then be changed to a border).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM