使用VBAmacros在Excel中行select和放置边界
我需要从第13行find工作表中的非空行,并将最上面的粗边框放到所选的非空行,直到表单的最后一个使用行。 从列C我需要find非空行。 我试过这个代码,但它不工作。 你能帮我解决吗?
Sub rowfind3() Dim cell As Range Dim r1 As Range For Each cell In ActiveSheet.Range("C:C") If (cell.Value <> "") Then Set r1 = Range("A" & ActiveCell.Row & ":AV" & ActiveCell.Row) r1.Select 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 = xlMedium 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(xlInsideHorizontal).LineStyle = xlNone End If Next cell End Sub
在这个代码中只显示边框的第一行,但是对于连续的行,边框不会到来。
此外,我尝试了上述情况的另一个代码,但同样的第一行是只显示边框。
Sub rowfind1() ' ' rowfind Macro ' ' Dim r1 As Range Dim lr As Variant Dim i As Integer lr = ActiveSheet.UsedRange.Rows.Count i = 0 For i = 13 To lr - 11 If (Not (IsEmpty(Cells(i, 3).Value))) Then Set r1 = Range("A" & ActiveCell.Row & ":AV" & ActiveCell.Row) r1.Select 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 = xlMedium 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(xlInsideHorizontal).LineStyle = xlNone End If Next i End Sub
首先,不需要使用select。 所有这些最终都会使代码混淆(就像在这种情况下)。 其次,无需重新声明循环内的范围。 那是什么循环在那里。
这是它应该看起来如何:
Sub rowfind3() Dim cell As Range For Each cell In ActiveSheet.Range("C:C") If (cell.Value <> "") Then With cell.Borders(xlEdgeLeft) .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlThin End With With cell.Borders(xlEdgeTop) .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlMedium End With With cell.Borders(xlEdgeBottom) .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlThin End With With cell.Borders(xlEdgeRight) .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlThin End With End If Next cell End Sub
我会考虑改变Activesheet引用你想要的实际工作表,只看UsedRange以及加快一点,但现在代码将至less让你在那里。
基于意见澄清的增编:
lr = ActiveSheet.Cells(ActiveSheet.Rows.Count, "C").End(xlUp).Row With ActiveSheet.Range("C13:C" & lr) With .Borders(xlEdgeLeft) .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlThin End With With .Borders(xlEdgeTop) .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlMedium End With With .Borders(xlEdgeBottom) .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlThin End With With .Borders(xlEdgeRight) .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlThin End With .Borders(xlInsideHorizontal).LineStyle = xlNone End With
你有没有考虑使用条件格式? 例如在列$A:$AV
公式是=$A1<>""
,在格式化select边框。