Quantcast
Channel: Active questions tagged excel - Stack Overflow
Viewing all articles
Browse latest Browse all 88868

VBA Loop Code works for one cell, but I need to apply it to all cells

$
0
0

Here is my code. In this code, I am checking if cell E2 value exists in another workbook in range C and L2 value exists in another workbook in range G, and if the corresponding column F contains the value "LAX". If the case, then return true else false in my active workbook in cells H2:I2. It works for cell E2, but I want to check for cell E3, E4, etc and return the true or false value in H3:I3, H4:I4, etc. I tried dimming J as a long and running that but it did not work so I commented it out with '. Any suggestions? Thank you.

Sub Worksheet_Change()
Dim KeyCells As Range
Dim Sheet1 As Worksheet
Dim Sheet2 As Worksheet
Dim CellChanged As Integer
Dim Path, File As String
Dim LastRow As Long
Dim LastRow1 As Long
Dim i As Long


Set Sheet1 = ThisWorkbook.Worksheets("Data") 'Edit Sheet File1

Set KeyCells = Range("E:E")

   Set Sheet2 = Workbooks("Data.xlsx").Worksheets("MyData") 'Edit Sheet of File2

   LastRow = Sheet2.Cells(Rows.Count, "A").End(xlUp).Row
   'Lastrow2 = Sheet1.Cells(Rows.Count, "A").End(xlUp).Row


    For i = 1 To LastRow
    'For j = 1 To LastRow2

        If Sheet1.Range("E2").Value = Sheet2.Range("C"& i) And Sheet1.Range("L2").Value = Sheet2.Range("G"& i) And Sheet2.Range("F"& i).Value = "LAX" Then
            Sheet1.Range("H2:I2").Value = "True"

        End If
    'Next j
    Next i

End
End Sub

Viewing all articles
Browse latest Browse all 88868


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>