选择范围的问题可能是因为最后一列和最后一行设置不正确

时间:2019-03-26 09:57:19

标签: excel vba

我正在尝试选择范围,并为所有颜色单元格粘贴0。

我不明白为什么此代码无法正常工作。我在论坛上做了几个选择,但仍然无法正常工作。我很确定这与最后一行和最后一列有关,而且很明显。

您能告诉我我在做什么错吗?

Dim cell As Range, rng As Range
    Dim LRRow As Long, LRCol As Long

        With ThisWorkbook.Worksheets("Data")

         LRRow = .Cells(4, .Columns.Count).End(xlToLeft).Column
         LRCol = .Cells(.Rows.Count, LRRow).End(xlUp).row

        Set rng = .Range(Cells(7, 4), Cells(LRRow, LRCol))


       ' Selection.AutoFilter
       ' Range("A1").Select


        For Each cell In rng

            If cell.Interior.Color = RGB(255, 204, 204) And cell.Value = "" Then
             cell.Value = 0
            End If

        Next cell

        End With

1 个答案:

答案 0 :(得分:2)

尝试更改:

 LRRow = .Cells(4, .Columns.Count).End(xlToLeft).Column
 LRCol = .Cells(.Rows.Count, LRRow).End(xlUp).row

TO

LRRow = .Cells(.Rows.Count, LRRow).End(xlUp).row
LRCol = .Cells(4, .Columns.Count).End(xlToLeft).Column