Vba 允许用户选择范围以搜索其他工作簿中的列表并返回单元格值的代码

Vba 允许用户选择范围以搜索其他工作簿中的列表并返回单元格值的代码,vba,excel,Vba,Excel,信息 工作簿A:有一个包含项目列表的主工作表,但值按月份列排列 工作簿B:我有两张工作表,上面有不同的项目列表,我想用来搜索工作簿A并返回我需要的当前或特定月份 注意:工作簿B列是偏移量,因此我们可能需要对此进行说明 到目前为止,我掌握的代码是: Sub Button() Dim OpenFileName As String Dim MyWB As Workbook, wb As Workbook Dim aRange As Range 'Excel titled, "MODs", cont

信息

工作簿A:有一个包含项目列表的主工作表,但值按月份列排列

工作簿B:我有两张工作表,上面有不同的项目列表,我想用来搜索工作簿A并返回我需要的当前或特定月份

注意:工作簿B列是偏移量,因此我们可能需要对此进行说明

到目前为止,我掌握的代码是:

Sub Button()

Dim OpenFileName As String
Dim MyWB As Workbook, wb As Workbook
Dim aRange As Range

'Excel titled, "MODs", contains this module
Set MyWB = ThisWorkbook

'Ignore possible messages on a excel that has links

Application.AskToUpdateLinks = False
Application.DisplayAlerts = False

'Select and Open workbook
OpenFileName = Application.GetOpenFilename '("clients saved spreadsheet,*.xlsb")
If OpenFileName = "False" Then Exit Sub

Set wb = Workbooks.Open(OpenFileName)
Application.DisplayAlerts = True
Application.AskToUpdateLinks = True


If MsgBox("Please select list range to search.", vbExclamation, "Search List") = vbOK Then

On Error Resume Next
Set aRange = Application.InputBox(prompt:="Enter range", Type:=8)
If aRange Is Nothing Then
  MsgBox "Operation Cancelled"
Else
  aRange.Select
End If

End If

End Sub
我可能会让这件事变得更难,所以我愿意接受建议。我似乎找不到合适的find函数来使用我所选的范围列表,并使用特定的主工作表(类似于vlookup)将新打开的工作簿作为目标

第2版:有一个设定的范围,但我仍然没有得到值返回

Sub Button()
Dim OpenFileName As String
Dim MyWB As Workbook, wb As Workbook
Dim MyWs As Worksheet, ws As Worksheet
Dim aRange As Range


'This line of code turns off the screen updates which make the macro run much faster.
'Application.ScreenUpdating = False

'Excel titled, "MODs", contains this module
Set MyWB = ThisWorkbook
Set MyWs = MyWB.Sheets("Sheet")

'Ignore possible messages on a excel that has links
Application.AskToUpdateLinks = False
Application.DisplayAlerts = False

'Select and Open workbook
OpenFileName = Application.GetOpenFilename '("clients saved spreadsheet,*.xlsb")
If OpenFileName = "False" Then Exit Sub

Set wb = Workbooks.Open(OpenFileName)

On Error Resume Next
Set ws = Application.InputBox("Select a cell on the key sheet.", Type:=8).Parent
On Error GoTo 0

If ws Is Nothing Then
    MsgBox "cancelled"
Else
    MsgBox "You selected sheet " & ws.Name
End If

Application.DisplayAlerts = True
Application.AskToUpdateLinks = True

With MyWs
    For Each aCell In .Range("A1:A10" & LastRow)
        If Len(Trim(.Range("A19" & aCell.Row).Value)) <> 0 Then
            .Cells(aCell.Row, 15) = Application.WorksheetFunction.VLookup( _
                                    aCell.Value, ws.Range("A1:C18"), 2, 0)
        End If
    Next aCell
End With

'wb.Close (False)

'If MsgBox("Please select list range to search.", vbExclamation, "Search List") = vbOK Then
    'On Error Resume Next
    'Set aRange = Application.InputBox(prompt:="Enter range", Type:=8)
    'If aRange Is Nothing Then
      'MsgBox "Operation Cancelled"
    'Else
      'aRange.Select
    'End If
'End If

'Return to default setting of screen updating.
'Application.ScreenUpdating = True
End Sub
子按钮()
将OpenFileName设置为字符串
将MyWB设置为工作簿,将wb设置为工作簿
将MyWs设置为工作表,ws设置为工作表
暗淡的天际
'这行代码关闭屏幕更新,使宏运行得更快。
'Application.ScreenUpdate=False
标题为“MODs”的Excel包含此模块
设置MyWB=ThisWorkbook
设置MyWs=MyWB.Sheets(“Sheet”)
'忽略包含链接的excel上可能出现的消息
Application.AskToUpdateLinks=False
Application.DisplayAlerts=False
'选择并打开工作簿
OpenFileName=Application.GetOpenFilename'(“客户端保存的电子表格,*.xlsb”)
如果OpenFileName=“False”,则退出Sub
设置wb=Workbooks.Open(OpenFileName)
出错时继续下一步
设置ws=Application.InputBox(“在键表上选择一个单元格”,类型:=8)。父项
错误转到0
如果ws什么都不是,那么
MsgBox“已取消”
其他的
MsgBox“您选择的工作表”&ws.Name
如果结束
Application.DisplayAlerts=True
Application.AskToUpdateLinks=True
与MyWs
对于范围内的每个aCell(“A1:A10”和LastRow)
如果Len(Trim(.Range(“A19”&aCell.Row).Value))为0,则
.Cells(aCell.Row,15)=Application.WorksheetFunction.VLookup(_
aCell.Value,ws.Range(“A1:C18”),2,0)
如果结束
下一个亚塞尔
以
'wb.Close(False)
'如果MsgBox(“请选择要搜索的列表范围”,vb感叹号,“搜索列表”)=vbOK,则
'出现错误时,请继续下一步
'Set aRange=Application.InputBox(提示:=“输入范围”,类型:=8)
“如果阿兰奇什么都不是的话
“MsgBox”操作已取消
”“否则呢
'aRange.Select
"完"
"完"
'返回屏幕更新的默认设置。
'Application.ScreenUpdating=True
端接头
我认为我遇到的问题是以下代码:

With MyWs
    For Each aCell In .Range("A1:A10" & LastRow)
        If Len(Trim(.Range("A19" & aCell.Row).Value)) <> 0 Then
            .Cells(aCell.Row, 15) = Application.WorksheetFunction.VLookup( _
                                    aCell.Value, ws.Range("A1:C18"), 2, 0)
使用MyWs的

对于范围内的每个aCell(“A1:A10”和LastRow)
如果Len(Trim(.Range(“A19”&aCell.Row).Value))为0,则
.Cells(aCell.Row,15)=Application.WorksheetFunction.VLookup(_
aCell.Value,ws.Range(“A1:C18”),2,0)

开始声明
aCell为范围
lastRow为长度

您似乎忽略了
lastRow
的定义,它可能类似于

lastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
然后仔细查看
.Range(“A1:A10”&LastRow)
。假设
lastRow
为100,那么这将设置从A1到A10100的范围:这就是您想要的吗?或者你会用

.Range(“A1:A”和lastRow)

同样地,
.Range(“A19”&aCell.Row)
会导致一个单元格地址,如“A1989”(were aCell.Row=89):这就是您想要的吗

除了上面提到的,我无法理解你在哪里搜索的实际情况。你可能想提供更多的信息