2017-03-17 64 views
0

的細胞,然後一列單元格如果列被發現的價值,我希望它開始通過從該值首次發現行的單元格進行迭代。我已經使用嵌套的for循環來嘗試它,但還是沒能弄明白。任何人都有解決方案?Excel的VBA - 遍歷一個行

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 
+0

首先檢查單元格的語法 - 您的錯誤。 – SJR

+0

耶你的if語句是沒有意義的。你說,如果細胞(1&1).value的... – user1

+0

提示:這是'細胞(行,列)''不細胞(行和列)' – CLR

回答

0

正如您在評論中看到的,您的問題是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 
+0

耶士..明白了!非常感謝。 @CMArg – Hameez