需要帮助循环宏

时间:2018-11-12 17:56:38

标签: excel vba loops

我不认为这很困难,但是我不知道...

在B列中,列出了“原始”或“添加”。从B79开始向上移动,第一次显示“原始”,我想从底部的B#:N#绘制边框。

我不知道如何在VBA中运行正确的循环,所以下面是我到目前为止所缺少的东西。

Sub Test()    
Range("B79").Select
If Range("B79") = "Original" Then
Selection.End(xlToRight).Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
Selection.Borders(xlEdgeLeft).LineStyle = xlNone
With Selection.Borders(xlEdgeTop)
    .LineStyle = xlDot
    .ColorIndex = xlAutomatic
    .TintAndShade = 0
    .Weight = xlThin
End With
Selection.Borders(xlEdgeBottom).LineStyle = xlNone
Selection.Borders(xlEdgeRight).LineStyle = xlNone
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone

Else: ActiveCell.Offset(-1, 0).Select
End If

End Sub

这是我目前的尝试。我只是想让它突出显示单元格。

Sub Test()

Let x = 79
Do While x > 7
If ("B" & x) = "Original" > 0 Then
Selection.End(xlToRight).Select
Else: x = x - 1
End If
Loop

End Sub

1 个答案:

答案 0 :(得分:1)

在下一个循环中使用a而不选择,这应该可以满足您的需求。确保您已阅读此代码,并了解它与原始代码的关系。

Sub Test()
Dim X As Long
For X = 79 To 1 Step -1 'Step -1 makes it go backwards
    If Range("B" & X).Text = "Original" Then 'Notice I am not actually selecting anything in this code, I don't need to in order to manipulate it
        With Range("B" & X).End(xlToRight)
            For Each Border In .Borders 'Loop the borders so you don't have to name each one
                Border.LineStyle = xlNone
            Next
            With .Borders(xlEdgeTop)
                .LineStyle = xlDot
                .ColorIndex = xlAutomatic
                .TintAndShade = 0
                .Weight = xlThin
            End With
        End With
    End If
Next
End Sub