简体   繁体   English

Excel VBA - 遍历单元格列,然后遍历行的单元格

[英]Excel VBA - Iterate through a Column of Cells and then Cells of a Row

If the value was found in the Column, I want it to start iterating through the Cells of that Row from which the value was first found. 如果在列中找到该值,我希望它开始迭代首先找到该值的那行的单元格。 I've used nested FOR Loops to attempt it, but still couldn't figure it out. 我已经使用嵌套的FOR Loops来尝试它,但仍然无法解决它。 Anyone with an solution? 有解决方案的人吗?

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

As you can see in the comments, your problem is the syntax of Cells . 正如您在评论中看到的,您的问题是Cells的语法。 Try the following: 请尝试以下方法:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM