簡體   English   中英

在特定列中查找上次使用的單元格,並在一定范圍內邊框

[英]Find last used cell in a specific column and border around with a range

我有這個Excel與K3(把用戶從細胞開始他們的價值觀,所以我不知道有多少會被長排3),我想從K3邊框到該行的最后一個值(ROW3起始值)以及從Row10Row3中使用的最后一個值(對不起,我真的不知道怎么說,我將一個示例讓您更好地理解)

這是一張照片

在此處輸入圖片說明

我發現的唯一代碼是此代碼,但它僅適用於靜態范圍。

Sub Sample()
    Dim rng As Range

    Set rng = Range("K3:P10")

    With rng.Borders
        .LineStyle = xlContinuous
        .Weight = xlThin
    End With
End Sub

只是動態輸入的一個示例(可以改進):

Private Sub Worksheet_Change(ByVal Target As Range)

Application.EnableEvents = False

Dim rng1 As Range, rng2 As Range
Dim lc As Long

If Not Intersect(Range("3:3"), Target) Is Nothing Then
    lc = Range("K3").CurrentRegion.Columns.Count 'No xlToLeft just in case far to the right there might be other cells
    Set rng1 = Range(Cells(3, 11), Cells(10, 11 + lc))
    Set rng2 = Range(Cells(3, 11), Cells(10, 11 + lc - 1))
    rng1.Borders.LineStyle = xlNone
    rng2.BorderAround ColorIndex:=1
End If

Application.EnableEvents = True

End Sub

在此處輸入圖片說明

到目前為止,這僅在每列添加/刪除時有效...如上所述,可以在;)上進行改進

應該這樣做:

Sub Sample()

    Dim rng As Range
    Dim LastCol As Long

    With ThisWorkbook.Sheets("MySheet") 'Change MySheet for your sheet name
        LastCol = .Cells(3, .Columns.Count).End(xlToLeft).Column 'this will find the last used column on Row 3
        Set rng = Range("K3", .Cells(10, LastCol))
    End With

    With rng
        .Borders(xlEdgeLeft).LineStyle = xlContinuous
        .Borders(xlEdgeLeft).Weight = xlThin
        .Borders(xlEdgeTop).LineStyle = xlContinuous
        .Borders(xlEdgeTop).Weight = xlThin
        .Borders(xlEdgeBottom).LineStyle = xlContinuous
        .Borders(xlEdgeBottom).Weight = xlThin
        .Borders(xlEdgeRight).LineStyle = xlContinuous
        .Borders(xlEdgeRight).Weight = xlThin
    End With

End Sub

暫無
暫無

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

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