获得最后一个细胞

时间:2014-03-13 17:46:23

标签: vba excel-vba excel

我正在尝试根据从“实际”的单元格值(始终在列B中)到最终列(AX)的范围来更改行的颜色。我试图在LastCol线计算这个,因为我不知道如何开始。目前,lastcol线正在计算我理解的所有列。我怎样才能将其限制在我所需的范围内?

Function Format()
Dim Rng, Rng2, cell, rngtocopy, rngtofinal As Range
Dim LastRow, LastCol As Long
Dim i As Long
Dim ws1 As Worksheet

Set ws1 = Sheets("Cost Sheet")
LastRow = ws1.Cells(ws1.Rows.Count, "B").End(xlUp).Row
LastCol = ws1.Cells(ws1.Columns.Count).End(xlRight).Column
Set Rng = ws1.Range("B12", ws1.Cells(LastRow, "B"))
Set Rng2 = ws1.Range(cell, cell & LastCol)

 For Each cell In Rng
        If cell.Value = "Actual" Then
            cell.EntireRow.Locked = True
                With cell
                    .Interior
                    .Pattern = xlSolid
                    .PatternColorIndex = xlAutomatic
                    .Color = 12632256
                    .TintAndShade = 0
                    .PatternTintAndShade = 0
                End With
        End If
 Next cell

End Function

1 个答案:

答案 0 :(得分:0)

找到我的答案,谢谢大家帮助我解决问题。

LastColumn = Cells.Find(What:="*", After:=[A1], _
                   SearchOrder:=xlByColumns, _
                   SearchDirection:=xlPrevious).Column