Arrays 在特定列条件的范围内创建边框
检查每行的特定列,并根据该列和单元格(工作表(WS).Cells(5,3).value)中的值将边框更改为更厚。是否有一种方法可以在使用的整个单元格范围内生成轮廓边框,并产生预期的结果 当前 预期结果 欢迎您提出意见/建议。谢谢你的帮助Arrays 在特定列条件的范围内创建边框,arrays,excel,vba,loops,Arrays,Excel,Vba,Loops,检查每行的特定列,并根据该列和单元格(工作表(WS).Cells(5,3).value)中的值将边框更改为更厚。是否有一种方法可以在使用的整个单元格范围内生成轮廓边框,并产生预期的结果 当前 预期结果 欢迎您提出意见/建议。谢谢你的帮助 Sub AddBorderColour(Col As Integer) Dim WS As String Dim i As Integer, r As Integer WS = ActiveSheet.Name ' Fin
Sub AddBorderColour(Col As Integer)
Dim WS As String
Dim i As Integer, r As Integer
WS = ActiveSheet.Name
' Find the maximum rows used in each worksheet.
r = Worksheets(WS).UsedRange.Columns.Count
For i = 29 To r
If Not (Worksheets(WS).Cells(5, 3).value = "") Then
If Worksheets(WS).Cells(i, Col + 6).value = Worksheets(WS).Cells(5, 3).value Then
Worksheets(WS).Range(Cells(i, Col).Address(), Cells(i, Col + 10).Address()).BorderAround LineStyle:=xlContinuous, Color:=vbBlack, Weight:=xlThick
End If
End If
Next i
End Sub
如果您将其记录为宏,它将为您提供线索。在Excel中手动执行此操作时,Excel的实际操作是将上边框应用于所选单元格的顶行,将右边框应用于所选单元格的右列,依此类推 我录制的宏中的代码是:
Sub border()
'
' border Macro
'
'
Range("C3:G12").Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
End Sub
与通常录制的宏一样,其中的许多代码实际上并不需要,但您已经明白了 几天前我写了一个宏,它必须“框定”某些单元格/区域 我写道:
Sub Framer(ws As Worksheet, sAdd As String)
With ws.Range(sAdd)
.Borders(xlEdgeTop).Weight = xlThick
.Borders(xlEdgeRight).Weight = xlThick
.Borders(xlEdgeBottom).Weight = xlThick
.Borders(xlEdgeLeft).Weight = xlThick
End With
End Sub
然后对于我们需要构建的每个单元/区域,只需使用单元/区域所在的WS的名称和单元/区域的地址来调用它
在您的示例中,我的方法是编写一个单独的子部分,其中我Dim
2个范围引用,比如rStart
和rEnd
,并想出一种方法来确定它们将引用哪些单元格。
我会在最左边的列中循环一个for
,该列包含对此感兴趣的数据类型。从第一行到最后一行,如果字体颜色为黑色,而上面单元格的字体颜色为红色,请执行以下操作:
For i = [whatever] to lastrow
'[black/red], obviously you will need to find the numbers and put those here
If Range("A" & i).font.color = [black] AND Range("A" & i-1).font.color = [red]
Set rStart = Range("A" & i)
Exit For
End if
Next i
在此之后,我将从第一行blackfont开始,查看第“i+1”行的字体颜色:
'+100, make sure it's a number larger than the count of potential black rows in-a-row
For i = rStart.Row to rStart.Row+100
If Range("A" & i+1).font.color = [red] then
Set rEnd = Range("K" & i)
Exit For
End if
Next i
(我喜欢对循环执行,而不是在期间执行操作,在该循环中,我对结束值进行了超调,只对执行一个退出操作)
一旦分配了rStart
和rEnd
,您就可以开始了
Call Framer(ws, Range(rStart, rEnd).Address)
然后重复这个直到桌子的底部。我希望这是有意义的。在尝试和错误得到回报后,我很高兴与大家分享以下可以解决这个问题的代码
如果工作表(WS).Cells(i,Col+6).value=工作表(WS).Cells(i-1,Col+6).value然后
工作表(WS).Range(单元格(i,列).Address(),单元格(i,列+10).Address())。边框线条样式:=xlContinuous,颜色:=vbBlack,重量:=xlThick
工作表(WS).Range(单元格(i,列).Address(),单元格(i,列+10).Address()).Borders(xlEdgeTop).LineStyle=xlContinuous
工作表(WS).Range(单元格(i,列).Address(),单元格(i,列+10).Address()).Borders(xlEdgeTop).Color=vbBlack
工作表(WS).Range(单元格(i,列).Address(),单元格(i,列+10).Address()).Borders(xlEdgeTop).Weight=xlThin
其他的
工作表(WS).Range(单元格(i,列).Address(),单元格(i,列+10).Address())。边框线条样式:=xlContinuous,颜色:=vbBlack,重量:=xlThick
如果