Excel VBA宏根据值格式化行

时间:2014-12-09 18:07:14

标签: excel vba excel-vba

嘿伙计们我有一个表格,我想用vba格式化,因为它有很多值,并且需要一个接一个地格式化。

例如

客户ID ----------员工----------小时

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

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

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

0005 ------------------肯-------------- 5

0005 ------------------杰夫-------------- 5

0008 ------------------乔-------------- 5

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

<000> ------------------卡尔------------- 5

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

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

我想在同一个ClientID的最后一个下面应用一个边框,将它们与ClientID的其余部分分开,以便在查看信息时更容易阅读,因此输出应该是这样的:< / p>

客户ID ----------员工----------小时

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

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

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


0005 ------------------肯-------------- 5

0005 ------------------杰夫-------------- 5


0008 ------------------乔-------------- 5

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


<000> ------------------卡尔------------- 5


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

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


我猜我需要某种类型的循环,它会关闭ClientID列并检查每个ClientID ......一旦新的ClientID出现,然后将一个边框添加到该行的顶部或前一行的底部。我知道我应该有一些代码,如果有人能指出我正确的方向或给我一些线索我可以继续写一些东西。

screenshot

2 个答案:

答案 0 :(得分:0)

如果您的数据格式如下:

enter image description here

然后,当客户端ID更改时,跟随宏将添加边框。此宏假定客户端ID已经按升序或降序排序。

<强>宏

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

<强>结果

enter image description here

答案 1 :(得分:0)

使用条件格式公式规则:

=$A1<>$A2  

应用于A:C,或者可能通过添加空行作为分隔符(例如,在客户端ID的每次更改时使用小计 - 然后可以将其更改为边框)。