Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0301· OpenAIAi2 WildChat-4.8M (AI2)

I am searching cells in column E; lastRow = Cells(Rows.Count, "E").End(xlUp).Row Set…

  1. 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)

  2. 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)

  3. User

    If i <> 5 And Cells(myCell.Row, i).Value = “” how does this line of code look at column G

  4. Assistant

    The i <> 5 part 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 condition i <> 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.

  5. 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)

  6. Assistant

    Yes, using If Cells(myCell.Row, i + 2).Value = "" instead of If 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 myCell
    

    Note that I have added the modified If statement inside the For loop, so that it checks for empty cells in Column G for every row as it goes through the loop.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.