【问题标题】:Excel VBA - Iterate through a Column of Cells and then Cells of a RowExcel VBA - 遍历一列单元格,然后遍历一行单元格
【发布时间】:2017-03-17 10:29:05
【问题描述】:

如果在列中找到值,我希望它开始遍历第一次找到值的那一行的单元格。我已经使用嵌套的 FOR 循环来尝试它,但仍然无法弄清楚。有人有解决方案吗?

LastRow = Range("p" & Rows.Count).End(xlUp).Row
LastCol = Cells(LastRow & Columns.Count).End(xlToRight).Column
    For s = 1 To LastRow
        If Range("a" & s).Value = "TO BE PAID Total" Then
            For i = 1 To LastCol

                    If Cells(s & i).Value <> "" Then
                        Cells(s & i).Select
                        Selection.Style = "Accent2"
                    End If


            Next i
        End If
    Next s

【问题讨论】:

  • 首先检查 Cells 的语法 - 你的语法不正确。
  • 是的,你的 if 语句没有意义。你是说如果单元格 (1 & 1).value ...
  • 提示:这是Cells(row , column) 而不是Cells(row &amp; column)

标签: vba excel for-loop


【解决方案1】:

正如您在 cmets 中看到的,您的问题是 Cells 的语法。请尝试以下操作:

Dim LastRow As Long
Dim LastCol As Long
Dim s As Integer
Dim i As Integer

LastRow = Worksheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
LastCol = Worksheets("Sheet1").Cells(1, Columns.Count).End(xlToLeft).Column
    For s = 1 To LastRow
        If Worksheets("Sheet1").Cells(s, 1).Value = "TO BE PAID Total" Then
            For i = 1 To LastCol
                    If Worksheets("Sheet1").Cells(s, i) <> "" Then
                        Worksheets("Sheet1").Cells(s, i).Style = "Accent2"
                    End If
            Next i
        End If
    Next s

【讨论】:

  • 是的.. 明白了!非常感谢。 @CMArg
猜你喜欢
  • 2017-05-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多