简体   繁体   中英

Excel VBA: Insert values from sheet 1 to sheet 2 if value in column matches

I'm a total newbie in VBA, just started this morning when confronted with a spreadsheet with ~30K rows.

I have two worksheets:

  1. named "tohere", contains ordinal numbers in column C.
  2. named "fromhere", contains numbers in column C and values in column B. It's basically the same ordinal numbers, but some are missing - that's why I started to write a macro in he first place.

I want Excel to check if the number in "tohere", Cell C1 exists in any cell in "fromhere", column C, and if it does, copy the value from the corresponding row in "fromhere", column B into "tohere", Cell B1; then do it again for C2 etc. If there's no such number in sheet "fromhere", just do nothing about this row.

I tried this code:

Dim i As Long
Dim tohere As Worksheet
Dim fromhere As Worksheet

Set tohere = ThisWorkbook.Worksheets("tohere")
Set fromhere = ThisWorkbook.Worksheets("fromhere")

For i = 1 To 100
    If fromhere.Range("C" & i).Value <> tohere.Range("C" & i).Value Then
    Else: fromhere.Cells(i, "B").Copy tohere.Cells(i, "B")
    End If
Next i

It does what I want for the first cells that are equal (4 in my case) and then just stops without looking further.

I tried using Cells(i, "C") instead, same thing. Using i = i + 1 after Then doesn't help.

I feel that the problem is in my cells addressing, but I don't understand how to fix it.

This is how my sample "fromhere" list looks like (you can notice some numbers are missing from the C column):

从这里

This is the sample of what I get with the "tohere" list:

到这里

It gets to the point where there's no "5" in "fromhere" and stops at this point.

PS: i = 1 To 100 is just to test it.

This should do your job. Run this and let me know.

Sub test()
    Dim tohere            As Worksheet
    Dim fromhere          As Worksheet
    Dim rngTohere         As Range
    Dim rngfromHere       As Range
    Dim rngCelTohere      As Range
    Dim rngCelfromhere    As Range

    'Set Workbook
    Set tohere = ThisWorkbook.Worksheets("tohere")
    Set fromhere = ThisWorkbook.Worksheets("fromhere")

    'Set Column
    Set rngTohere = tohere.Columns("C")
    Set rngfromHere = fromhere.Columns("C")

    'Loop through each cell in Column C
    For Each rngCelTohere In rngTohere.Cells
        If Trim(rngCelTohere) <> "" Then
            For Each rngCelfromhere In rngfromHere.Cells
                If UCase(Trim(rngCelTohere)) = UCase(Trim(rngCelfromhere)) Then
                    rngCelTohere.Offset(, -1) = rngCelfromhere.Offset(, -1)
                    Exit For
                End If
            Next rngCelfromhere
        End If
    Next rngCelTohere

    Set tohere = Nothing
    Set fromhere = Nothing
    Set rngTohere = Nothing
    Set rngfromHere = Nothing
    Set rngCelTohere = Nothing
    Set rngCelfromhere = Nothing
End Sub

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