简体   繁体   中英

How to loop two columns and put result into one column?

Trying to loop two columns and put result into one column.

1) looping is incorrect (no hits = wrong)
2) printing puts result into two different columns ("O" +7 from H and "R" +7 from K).

Private Sub FindValueKH_JN()

'New column O (no 15)
'Find if value starting in column H (no8) is between 207100-208100
'AND if value starting in column K (no11) is between 12700-12729, 
' then T2J in column O, else T2N in O

Range("O1").Select
Selection.EntireColumn.Insert , CopyOrigin:=xlFormatFromLeftOrAbove
ActiveCell.FormulaR1C1 = "T2 er Ja eller Nei"

Dim loopRange As Range

'From H to new column O is +7 columns
lastrow1 = ActiveSheet.Cells(Rows.Count, "H").End(xlUp).Row

'From K to new column O is +4 columns
lastrow2 = ActiveSheet.Cells(Rows.Count, "K").End(xlUp).Row

'loop columns H and K
Set loopRange = Union(Range("H2:H" & lastrow1), Range("K2:K" & lastrow2))

For Each cell In loopRange
    If Left(cell.Value, 6) >= 207100 And Left(cell.Value, 6) <= 208100 And _
      Left(cell.Value, 5) >= 12700 And Left(cell.Value, 5) <= 12729 Then

        cell.Offset(0, 7).Value = "T2J"

    Else: cell.Offset(0, 7).Value = "T2N"
    End If
Next cell

End Sub

Your references are incorrect, and this is why you are not getting any hits. You want to check two separate columns for specific values, but instead are just looking in one single cell for both conditions:

For Each cell In loopRange will loop through every cell in your defined loopRange range, which contains both columns.

You'd have to change your code so it loops through just a single column instead, like the following

Dim loopRange As Range
lastrow = ActiveSheet.Cells(Rows.Count, "H").End(xlUp).Row    'From H to new column O is +7 columns
Set loopRange = Range("H2:H" & lastrow1)                      'loop columns H

For Each cell In loopRange
    If Left(cell.Value, 6) >= 207100 And Left(cell.Value, 6) <= 208100 And Left(cell.Offset(, 3).Value, 5) >= 12700 And Left(cell.Offset(, 3).Value, 5) <= 12729 Then
        cell.Offset(0, 7).Value = "T2J"
    Else: cell.Offset(0, 7).Value = "T2N"
    End If
Next cell

In your If -statement, you are checking the content of a single cell and your If -statement can never be true. With your Union -statement, you will get a Range with all cells of Col H and all cells of Col K , and in the loop you are checking all cells that are either in H or in K .

So your If hits, for example, Cell H2 and you are checking if the content is > 207100 and in the same moment < 12729.

What you probably want is to loop over all cells if column H , check it's value together with the value of the cell in column K of the same row.

I assume your cells contain a string starting with a number but holds also some characters. I would advice that you write the values into intermediate variables, makes it much easier to debug. You are using the left -function which will give you the first 6 (resp. 5) characters. The result is still a string (even if it contains only digits), and you compare it to a number, and that's not a good idea because now VBA has to do some implicit conversions, and that may lead to unexpected results. You should use the Val -function to convert a string into a numeric value.

As already mentioned in the comments, never work implicit on the so called Active Worksheet . Specify explicitly the worksheet you want to work with.

One question: Why do you use the strange syntax for the Else -statement. The : means that you put a second statement into a line. It is much more readable to omit the : and put the next statement(s) into separate lines.

Dim loopRange As Range, cell As Range, lastrow  As Long
With ThisWorkbook.Sheets(1)
    lastrow = .Cells(Rows.Count, "H").End(xlUp).row
    Set loopRange = .Range("H2:H" & lastrow)
End With

For Each cell In loopRange
    Dim valH As Long, valK As Long
    valH = Val(Left(cell.Value, 6))
    valK = Val(Left(cell.Offset(0, 3).Value, 6))

    If valH >= 207100 And valH <= 208100 And valK >= 12700 And valK <= 12729 Then
        cell.Offset(0, 7).Value = "T2J"
    Else
        cell.Offset(0, 7).Value = "T2N"
    End If
Next cell

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM