Excel VBA查找/查找文本

Excel VBA查找/查找文本,excel,vba,Excel,Vba,我正在努力完成一些事情,而我缺乏编码知识,这让我和我的项目都慢了下来 我有三个表,源表、输入表和输出表,如图所示: 我想要我的代码做的是循环输入表中的名称,每当代码在源表中找到相等的名称时,它就会将NUM、NAME和PRICE复制到输出表中,就像名称a一样: 它应该跳过不在源表中的一个,即e和f,结果应该如下所示: 这就是我到目前为止想到的 Sub TEST_2_2() Dim LastInputRow As Integer 'Count the number of rows in my

我正在努力完成一些事情,而我缺乏编码知识,这让我和我的项目都慢了下来

我有三个表,源表、输入表和输出表,如图所示:

我想要我的代码做的是循环输入表中的名称,每当代码在源表中找到相等的名称时,它就会将NUM、NAME和PRICE复制到输出表中,就像名称a一样:

它应该跳过不在源表中的一个,即e和f,结果应该如下所示:

这就是我到目前为止想到的

Sub TEST_2_2()

Dim LastInputRow As Integer 'Count the number of rows in my input table
LastInputRow = Range("F" & Rows.Count).End(xlUp).Row

Dim LastSourceRow As Integer ' count the the number of rows in my source table
LastSourceRow = Range("B" & Rows.Count).End(xlUp).Row

Dim i As Integer 'declare a variable for monitoring on which row in my input table Iam
Dim MatString As String 'String we are searching for
Dim MatRange As Range 'The range we are currently in

For i = 2 To LastInputRow
    MatString = Range("F" & i).Value
    Set MatRange = Range("B1:B" & LastSourceRow).Find(What:=MatString)
    If Not MatRange Is Nothing Then
        'Can´t figure this out
    Else
    End If
Next i

End Sub
使用的数据只是为了说明我的问题,实际上我正在处理更大的表,但要点是一样的


欢迎任何关于我应该如何解决它的帮助,因为我说过我不是很有经验,所以任何提示都会派上用场,谢谢

您的输出似乎是由输入表中的名称过滤的源表

*注意:当您编写
时,我假设它是一个Excel表。如果没有,可以将ListObject范围引用更改为常规引用)

以下是VBA代码,可实现以下功能:

Option Explicit
Sub outputTable()
    Dim LOS As ListObjects, WS As Worksheet
    Dim loSource As ListObject, loInput As ListObject, loOutput As ListObject
    Dim rFilteredData As Range, vFilter As Variant
    Dim v, w
    
Set WS = Worksheets("Sheet9") 'or whatever

Set LOS = WS.ListObjects
Set loSource = LOS("tblSource")
Set loInput = LOS("tblInput")

'get filtering values
'empirically it seems that entries in the filtering values array that do not 
'  exist in the table to be filtered are ignored.
'if this is not the case, some additional code may be needed
v = loInput.ListColumns("NAME").DataBodyRange

'if might have greater than 65,535 entries then
'  transpose using a loop
v = Application.WorksheetFunction.Transpose(v)

loSource.Range.AutoFilter Field:=2, Criteria1:= _
        v, Operator:=xlFilterValues
        
Set rFilteredData = loSource.Range.SpecialCells(xlCellTypeVisible)

WS.Range("M:O").Clear
rFilteredData.Copy WS.Range("M1")

loSource.AutoFilter.ShowAllData

End Sub

通过在两个表上执行
innerJoin
,您可以对
powerquery
(在Excel 2010+中提供)执行相同的操作:

M代码

let
    Source = Excel.CurrentWorkbook(){[Name="tblSource"]}[Content],
    Source2 = Excel.CurrentWorkbook(){[Name="tblInput"]}[Content],
    output = Table.NestedJoin(Source,"NAME", Source2,"NAME","Output",JoinKind.Inner),
    #"Removed Columns" = Table.RemoveColumns(output,{"Output"}),
    #"Changed Type" = Table.TransformColumnTypes(#"Removed Columns",{{"NUM", Int64.Type}, {"NAME", type text}, {"PRICE", Currency.Type}})
in
    #"Changed Type"

也许是这样的

Sub test()
ar = Application.Transpose(Range("F2", Range("F2").End(xlDown)))
Range("A1").AutoFilter Field:=2, Criteria1:=ar, _
    Operator:=xlFilterValues
Range("A1").CurrentRegion.SpecialCells(xlCellTypeVisible).Copy _
Destination:=Range("i1")
Cells.AutoFilter
End Sub

更容易在源表上循环并对输入表运行匹配,然后在有匹配的情况下复制。像符咒一样工作,从未想过过滤某些内容,直接跳到for循环,谢谢您的帮助!