简体   繁体   English

Excel VBA:如果列中的值匹配,则将值从工作表1插入到工作表2

[英]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. 我是VBA的新手,今天才刚开始,当时遇到的是包含约3万行的电子表格。

I have two worksheets: 我有两个工作表:

  1. named "tohere", contains ordinal numbers in column C. 名为“ tohere”的列在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. 名为“ fromhere”的内容包含C列中的数字和B列中的值。它基本上是相同的序数,但是缺少一些序号-这就是为什么我首先开始在他的位置编写宏。

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; 我希望Excel检查“ tohere”单元格C1中的单元格中是否存在“ tohere”单元格中的数字,如果存在,请将“ fromhere”列B中的相应行中的值复制到“ tohere”中,单元格B1; then do it again for C2 etc. If there's no such number in sheet "fromhere", just do nothing about this row. 然后针对C2等再次执行此操作。如果“ fromhere”工作表中没有此类数字,则无需对此行执行任何操作。

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. 对于第一个相等的单元格(我的情况是4个),它会执行我想要的操作,然后停下来,不进一步查找。

I tried using Cells(i, "C") instead, same thing. 我尝试使用Cells(i, "C")代替,也是一样。 Using i = i + 1 after Then doesn't help. 在“ Then之后使用i = i + 1并没有帮助。

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): 这是我的示例“ fromhere”列表的样子(您会注意到C列中缺少一些数字):

从这里

This is the sample of what I get with the "tohere" list: 这是我在“ tohere”列表中得到的示例:

到这里

It gets to the point where there's no "5" in "fromhere" and stops at this point. 到达“ fromhere”中没有“ 5”的位置,并在此位置停止。

PS: i = 1 To 100 is just to test it. PS: i = 1 To 100仅用于测试。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过EXCEL VBA为每个值创建新列,将与ID匹配的一列中的值复制到新表中 - Copying values from one column that matches a ID to a new sheet by creating new columns for each values through EXCEL VBA 如果工作表sheet2中的值与工作表sheet1中的标头的值匹配的Vba代码,从工作表sheet1检索整列 - Vba code to retrieve an entire column from sheet1 if the value in a cell of sheet2 matches the value of a header in sheet1 Excel VBA - 根据 3 个条件(复杂的 IF AND 相关 VBA 与通配符)使用另一张工作表中的值填充一张工作表上的列 - Excel VBA - Populate a column on one sheet with values from another sheet based on 3 criteria (complicated IF AND related VBA with wildcards) Excel VBA-从工作表复制(扫描名称),然后将其插入另一工作表 - Excel VBA - Copy from a sheet (scan for name) then insert it in another sheet VBA Excel。 如何搜索值,获取第一行/第一列的值并将其插入其他工作表(如果尚不存在) - VBA Excel. How to search for a value, get the first row/first column values and insert them in a different sheet if they don't yet exist VBA Excel-将数据粘贴到另一列中的数据与列匹配 - VBA excel - paste data into another sheet with data in column matches 如何使用Excel VBA将一张纸中的值分配给隐藏的纸中? (并跳过范围内的列?) - How to assign values from one sheet into hidden sheet using Excel VBA? (and skip a column within the range?) 在Excel VBA中,根据列值在表格之间复制行 - Copying Rows from sheet to sheet in Excel VBA based upon column values Excel VBA按钮。 将行从Sheet1复制到Sheet2 /条件:列值 - Excel VBA button. Copy rows from Sheet1 to Sheet2 / Condition: column value Excel“如果&gt;复制&gt;插入1行&gt;粘贴&gt;仅值(从一张纸到另一张纸)” - Excel “if > copy > insert 1 line > paste > only values (from sheet to sheet)”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM