I am searching cells in column E; lastRow = Cells(Rows.Count, "E").End(xlUp).Row Set…
- User
I am searching cells in column E; lastRow = Cells(Rows.Count, "E").End(xlUp).Row Set myRange = Range("E1:E" & lastRow)
For Each myCell In myRange If myCell.Value <> "" Then newRow = newRow + 1 ' Move to the next row
However, I want my criteria search ( If Cells(myCell.Row, i).Value = "" Then) to look at the value in column G
How then will I change the criteria of the code below:
For i = 1 To 7 ' Assuming data range is from A to G If Cells(myCell.Row, i).Value = "" Then newWs.Cells(newRow, i).Value = Trim(Cells(myCell.Row, i).Value)
- Assistant
For i = 1 To 7 ' Assuming data range is from A to G If i <> 5 And Cells(myCell.Row, i).Value = "" Then 'check if the cell in column G is empty newWs.Cells(newRow, i).Value = Trim(Cells(myCell.Row, i).Value)
- User
If i <> 5 And Cells(myCell.Row, i).Value = “” how does this line of code look at column G
- Assistant
The
i <> 5part of the code checks if the current column being checked is not column 5 (Column E).The
Cells(myCell.Row, i).Value = ""part of the code looks at the value of the cell in the current row (myCell.Row) and the current column (i). So, by adding the conditioni <> 5, the code will only execute if the current column being checked is not column 5 (Column E).This means that the code is indirectly looking at the value in column G because it is checking if the current column being checked is not column 5 before checking if the cell in that column and row is empty or not.
- User
I am searching cells in column E; lastRow = Cells(Rows.Count, "E").End(xlUp).Row Set myRange = Range("E1:E" & lastRow)
For Each myCell In myRange If myCell.Value <> "" Then newRow = newRow + 1 ' Move to the next row
However, I want my criteria search ( If Cells(myCell.Row, i).Value = "" Then) to look at the value in column G
maybe something like this might work ( If Cells(myCell.Row, i + 2).Value = "" Then)
- Assistant
Yes, using
If Cells(myCell.Row, i + 2).Value = ""instead ofIf Cells(myCell.Row, i).Value = ""in your code will check for empty cells in Column G (i.e., two columns to the right of Column E).Here's how your modified code should look like:
lastRow = Cells(Rows.Count, "E").End(xlUp).Row Set myRange = Range("E1:E" & lastRow) For Each myCell In myRange If myCell.Value <> "" Then newRow = newRow + 1 ' Move to the next row For i = 1 To 7 ' Assuming data range is from A to G If Cells(myCell.Row, i + 2).Value = "" Then ' Check for empty cells in Column G newWs.Cells(newRow, i).Value = Trim(Cells(myCell.Row, i).Value) End If Next i End If Next myCellNote that I have added the modified
Ifstatement inside theForloop, so that it checks for empty cells in Column G for every row as it goes through the loop.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.