Excel VBA:从search另一个表中插入表格中的单元格

自从我上个世纪以来,已经有好几年了,但是现在我似乎再次需要它了。

为了简单起见,我在A列中有第7个数字,而且我需要在第B列中input另一个数字,这取决于另一个表中另一个表中的数字7与哪个数字相关。

因此,在Sheet2中,另一个表的数据范围从1到10列A,并根据列B中的数字。然后需要它来searchSheet2列A中的数字7,并给我列B中的数字,并将其放置在B列在第一张表中。

我已经在For循环内尝试了一个For循环,基于我在另一个地方find的另一个代码,但是很久以前我需要花费几个小时的时间来重读,并尝试接近某个解决scheme。 也许这对于高级编码者来说是一件容易的事情? 反正,在此先感谢您的帮助!

你不能没有VBA的帮助,那么你可以使用这个

Option Explicit Sub main() Dim cell As Range, f As Range Dim rng1 As Range, rng2 As Range Set rng1 = Worksheets("Sht1").Columns(1).SpecialCells(xlCellTypeConstants) '<--Change "Sht1" to your actual sheet1 name Set rng2 = Worksheets("Sht2").Columns(1).SpecialCells(xlCellTypeConstants) '<--Change "Sht2" to your actual sheet2 name For Each cell In rng1 Set f = rng2.Find(what:=cell.Value2, LookIn:=xlValues, lookat:=xlWhole, MatchCase:=xlNo) If Not f Is Nothing Then cell.Offset(, 1) = f.Offset(, 1) Next cell End Sub 

以下是在两个表格上进行search的两种方法。

 Sub LoopValues() Application.ScreenUpdating = False Application.Calculation = xlCalculationManual Dim wsSource As Worksheet, wsSearch As Worksheet Dim sourceLastRow As Long, searchLastRow As Long Dim i As Long, j As Long Set wsSource = Worksheets("Sheet3") Set wsSearch = Worksheets("Sheet4") With wsSource sourceLastRow = .Range("A" & Rows.Count).End(xlUp).Row searchLastRow = wsSearch.Range("A" & Rows.Count).End(xlUp).Row For i = 2 To sourceLastRow For j = 2 To sourceLastRow If .Cells(i, 1).Value = wsSearch.Cells(j, 1).Value Then .Cells(i, 2).Value = wsSearch.Cells(j, 2).Value Next Next End With Application.ScreenUpdating = True Application.Calculation = xlCalculationAutomatic End Sub Sub FindValuesLoop() Application.ScreenUpdating = False Application.Calculation = xlCalculationManual Dim wsSource As Worksheet, wsSearch As Worksheet Dim sourceLastRow As Long Dim i As Long Dim SearchRange As Range, rFound As Range Set wsSource = Worksheets("Sheet3") Set wsSearch = Worksheets("Sheet4") With wsSource sourceLastRow = .Range("A" & Rows.Count).End(xlUp).Row Set SearchRange = wsSearch.Range(wsSearch.Range("A1"), wsSearch.Range("A" & Rows.Count).End(xlUp)) For i = 2 To sourceLastRow Set rFound = SearchRange.Find(What:=.Cells(i, 1).Value, LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False) If Not rFound Is Nothing Then .Cells(i, 2).Value = rFound.Offset(0, 1).Value Next End With Application.ScreenUpdating = True Application.Calculation = xlCalculationAutomatic End Sub