簡體   English   中英

需要幫助循環宏

[英]Need help looping macro

我不相信這很困難,但我無法弄清楚......

在 B 列中,我列出了“原始”或“添加”。 從B79開始向上移動,第一次顯示“Original”我想從底部的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

使用 for next 循環並且不要選擇,這應該可以滿足您的需求。 請務必閱讀此代碼並了解它與原始代碼的關系。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM