简体   繁体   English

Excel VBA根据列名称和单元格值将边框分配给组

[英]Excel VBA to Assign Border to a group based on Column name and Cell Value

I have the below code and I want to do the following: 我有以下代码,我想执行以下操作:

Go through a specific column based on its name (example "Company") and to change the border based on the values in that column (example "CompanyA","CompanyB", "CompanyC" etc.) to be Thick Box Border. 根据名称(例如“ Company”)浏览特定的一列,并根据该列中的值(例如“ CompanyA”,“ CompanyB”,“ CompanyC”等)将边框更改为厚框边框。 This means "Company A" (50 rows) would get a border and "Company B" (5 rows) would get a border and so on. 这意味着“公司A”(50行)将获得边框,而“公司B”(5行)将获得边框,依此类推。

Can this be done? 能做到吗? Ty in advance! 提前输入!

Sub DrawBorders() 

Dim rCell As Range 
Dim rRange As Range 

Set rRange = Range("A1", Range("A65536").End(xlUp)) 

For Each rCell In rRange 
    If Not IsEmpty(rCell) And _ 
    Not IsEmpty(rCell.Offset(1, 0)) Then 
        With rCell 
            If .Value <> .Offset(1, 0).Value Then 
                With .EntireRow.Borders(xlEdgeBottom) 
                    .LineStyle = xlContinuous 
                    .Weight = xlMedium 
                    .ColorIndex = xlAutomatic 
                End With 
            End If 
        End With 
    End If 
Next rCell 

End Sub 

I have adjusted my code to fit your request. 我已调整代码以适应您的要求。 This will only put borders around desired company types. 这只会在所需公司类型周围设置边界。 You may need to add further error catching to the IF-Statements depending on your data you're processing. 您可能需要根据要处理的数据向IF语句添加更多错误捕获。

Sub DrawBoarders()
    Dim rCell As Range
    Dim rRange As Range
    Dim Prev As String
    Dim MyCell As String
    Prev = ""

    Set rRange = Range("A2", Range("A65536").End(xlUp))
    Dim SpecificCompany(3) As String 'Using 3 companies (Company A, B, & C)
    'Array of desired company names
    SpecificCompany(0) = "CompanyA"
    SpecificCompany(1) = "CompanyB"
    SpecificCompany(2) = "CompanyC"


    If IsInArray(Range("A1"), SpecificCompany) Then 'Check 1st row
            With Range("A1").EntireRow.Borders(xlEdgeTop)
                .LineStyle = xlContinuous
                .Weight = xlMedium
                .ColorIndex = xlAutomatic
            End With
    End If

    For Each rCell In rRange
        If IsInArray(rCell.Value, SpecificCompany) And rCell.Value <> rCell.Offset(-1, 0).Value Then
            With rCell.EntireRow.Borders(xlEdgeTop)
                .LineStyle = xlContinuous
                .Weight = xlMedium
                .ColorIndex = xlAutomatic
            End With
        End If
        If Not IsEmpty(rCell) And _
        Not IsEmpty(rCell.Offset(1, 0)) Then
        If rCell.Value <> rCell.Offset(1, 0).Value Then
            With rCell.EntireRow.Borders(xlEdgeBottom)
                .LineStyle = xlContinuous
                .Weight = xlMedium
                .ColorIndex = xlAutomatic
            End With
        End If
        End If
    Next rCell
End Sub

Function IsInArray(stringToBeFound As String, arr As Variant) As Boolean
    IsInArray = (UBound(Filter(arr, stringToBeFound)) > -1)
End Function

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Excel Vba宏根据单元格值和列名称删除行或列 - Excel Vba Macro to Delete Row or Column Based on Cell Value and Column Name Excel 中的 VBA - 更改列中的 VLOOKUP 公式以根据静态单元格值插入工作表名称 - VBA in Excel - Changing a VLOOKUP formula in a column to insert name of sheet based on static cell value 如何使用VBA为Excel单元格分配名称? - How to assign a name to an Excel cell using VBA? Excel VBA遍历列并根据单元格值插入行 - Excel VBA loop through column and insert rows based on cell value 根据另一列上的 IF 语句在 Msgbox 中显示单元格值 - Excel VBA - Show cell value in a Msgbox based on IF statement on another column - Excel VBA 如何在excel VBA中根据行和列ID查找单元格值 - How to find cell value based on row and Column ID in excel VBA VBA Excel-根据单元格值保护工作表中的列 - VBA Excel - Protect column in a sheet based on a cell value Excel VBA:如何根据数据库名称对行进行分组并根据特定的组名称添加值? - Excel VBA: How to group rows based on database name and add value based on specific group name? 使用matlab和excel根据单元格值检索列名 - Retrieve column name based on cell value using matlab and excel 根据JAVA中的列名从Excel文件获取单元格值 - Get cell value from Excel file based on the column name in JAVA
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM