Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 使用索引匹配提取多列数据_Excel_Vba - Fatal编程技术网

Excel 使用索引匹配提取多列数据

Excel 使用索引匹配提取多列数据,excel,vba,Excel,Vba,下面的脚本允许用户通过一系列输入框执行索引匹配功能。此版本仅允许用户每次使用一列数据。我想知道是否有可能更改此代码以允许用户提取多列数据。我感谢社区能提供的任何反馈 Sub MatchMaster() 'this script helps simplify the use of Excel's Index Match function 'place this script in your personal macro workbook and assign it to a button 'us

下面的脚本允许用户通过一系列输入框执行索引匹配功能。此版本仅允许用户每次使用一列数据。我想知道是否有可能更改此代码以允许用户提取多列数据。我感谢社区能提供的任何反馈

Sub MatchMaster()

'this script helps simplify the use of Excel's Index Match function
'place this script in your personal macro workbook and assign it to a button
'use it to pull data between two worksheets that share unique identifiers

'dim ranges
Dim ValuesToPull As Range
Dim TargetIDs As Range
Dim SourceIDs As Range
Dim MyRange As Range

'dim worksheets
Dim Source1 As Worksheet
Dim Target1 As Worksheet
Dim Source2 As Worksheet
Dim Target2 As Worksheet

'input box dims
Dim Prompt1 As String
Dim Prompt2 As String
Dim Prompt3 As String
Dim Prompt4 As String
Dim Title1 As String
Dim Title2 As String
Dim Title3 As String
Dim Title4 As String

'set prompts
Prompt1 = "Select values to pull (1 column only)"
Prompt2 = "Select unique IDs on target sheet (1 column only)"
Prompt3 = "Select unique IDs on source sheet (1 column only)"
Prompt4 = "Where should we put these values? (1 column only)"

'set titles
Title1 = "Source Sheet"
Title2 = "Target Sheet"
Title3 = "Source Sheet"
Title4 = "Target Sheet"

'error handling
On Error GoTo OuttaHere

'input boxes
Set SourceIDs = Application.InputBox(Prompt3, Title3, Type:=8)
Set Source1 = SourceIDs.Worksheet
SourceIDcolumn = SourceIDs.Column
LastSourceID = Source1.Cells(Rows.Count, SourceIDcolumn).End(xlUp).Row
Source1.Activate

Set ValuesToPull = Application.InputBox(Prompt1, Title1, Type:=8)
Set Source2 = ValuesToPull.Worksheet
ValuesColumn = ValuesToPull.Column
LastValue = Source2.Cells(Rows.Count, ValuesColumn).End(xlUp).Row
Source2.Activate

Set TargetIDs = Application.InputBox(Prompt2, Title2, Type:=8)
Set Target1 = TargetIDs.Worksheet
TargetIDcolumn = TargetIDs.Column
LastTargetID = Target1.Cells(Rows.Count, TargetIDcolumn).End(xlUp).Row '<~~ also use this for MyRange
Target1.Activate

Set MyRange = Application.InputBox(Prompt4, Title4, Type:=8)
Set Target2 = MyRange.Worksheet
MyColumn = MyRange.Column
Target2.Activate

'convert input to Range Cells format
With Source1
    Set SourceIDs = .Range(.Cells(1, SourceIDcolumn), .Cells(LastSourceID, SourceIDcolumn))
End With

With Source2
    Set ValuesToPull = .Range(.Cells(1, ValuesColumn), .Cells(LastValue, ValuesColumn))
End With

With Target1
     Set TargetIDs = .Range(.Cells(1, TargetIDcolumn), .Cells(LastTargetID, TargetIDcolumn))
End With

With Target2
     Set MyRange = .Range(.Cells(1, MyColumn), .Cells(LastTargetID, MyColumn))
End With

'apply formula
MyRange = Application.Index(ValuesToPull, Application.Match(TargetIDs, SourceIDs, 0))

OuttaHere:
ActiveWorkbook.ActiveSheet.Columns.AutoFit

End Sub
Sub MatchMaster()
'此脚本有助于简化Excel索引匹配函数的使用
'将此脚本放在个人宏工作簿中,并将其分配给按钮
'使用它在共享唯一标识符的两个工作表之间拉取数据
“昏暗的范围
Dim ValuesToPull As范围
变暗目标为射程
将SourceID设置为范围
将MyRange变暗为Range
“暗淡的工作表
将Source1设置为工作表
将Target1变暗为工作表
将Source2设置为工作表
Dim Target2 As工作表
'输入框变暗
作为字符串的Dim Prompt1
作为字符串的Dim Prompt2
作为字符串的Dim Prompt3
作为字符串的Dim Prompt4
将标题1设置为字符串
小标题2作为字符串
暗标题3作为字符串
暗标题4作为字符串
"设置提示,
Prompt1=“选择要拉取的值(仅1列)”
Prompt2=“在目标工作表上选择唯一ID(仅1列)”
Prompt3=“在源工作表上选择唯一ID(仅1列)”
Prompt4=“我们应该将这些值放在哪里?(仅1列)
"定题",
Title1=“源工作表”
Title2=“目标工作表”
Title3=“源工作表”
Title4=“目标工作表”
'错误处理
上错了就去外面
'输入框
设置SourceId=Application.InputBox(Prompt3,标题3,类型:=8)
Set Source1=SourceIDs.Worksheet
SourceIDcolumn=SourceIDs.Column
LastSourceID=Source1.Cells(Rows.Count,SourceIDcolumn).End(xlUp).Row
来源1.激活
Set ValuesToPull=Application.InputBox(Prompt1,标题1,类型:=8)
Set Source2=ValuesToPull.Worksheet
ValuesColumn=ValuesToPull.Column
LastValue=Source2.Cells(Rows.Count,ValuesColumn).End(xlUp).Row
来源2.激活
设置targetId=Application.InputBox(Prompt2,标题2,类型:=8)
设置Target1=targetId.Worksheet
TargetIDcolumn=TargetIDs.Column

LastTargetID=Target1.Cells(Rows.Count,targetdColumn).End(xlUp).Row'不好笑。。。但是,教他们如何使用索引匹配不是更容易吗?实际上,我也想自己弄清楚这一点。我发现手动输入多个索引匹配公式相当繁琐,仅从匹配中获取正确的行。然后使用类似这样的
.Range(“A:D”).Rows(i).Values,其中i是匹配返回的数字。它将在该行返回四个值的数组。@ScottCraner谢谢Scott,这听起来很有趣。有没有可能你有一个正在使用的工作示例?我刚刚给了你一个,这是另一个ActiveSheet.Range(“A5:N100”).Rows(3).Address`它将返回地址
$a$7:$N$7
,只需单独使用匹配,并允许用户选择多个列,将其设置为范围并匹配为行。