簡體   English   中英

Excel VBA - 遍歷單元格列,然后遍歷行的單元格

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

如果在列中找到該值,我希望它開始迭代首先找到該值的那行的單元格。 我已經使用嵌套的FOR Loops來嘗試它,但仍然無法解決它。 有解決方案的人嗎?

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的語法。 請嘗試以下方法:

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