获取Range.Find工作

时间:2017-08-16 00:13:09

标签: excel vba excel-vba

我正在处理一个项目工作,我需要帮助更改此代码以使用我当前的工作表。在我的注册"工作表我有一个区域,我试图输入"运动员"名称,它将从"主列表中提取信息"工作表。

更新 以下是此编码的原始工作簿。我试图让这个编码工作在我的新工作簿上,因为我使用的是两个工作表而不是一个。

Original Workbook

以下是旧代码,但我无法将其配置为使用此新工作簿,因为我正在使用两个工作表而不是一个工作表。

Sub Update()
Dim rngSearch As Range, rngFound As Range
Set rngSearch = Range("B18:B150")
Set rngFound = rngSearch.Find(What:=Range("B12").Value, LookIn:=xlValues, 
LookAt:=xlPart)

If rngFound Is Nothing Then
MsgBox "Please recheck name"
Range("B12").Value = " "
Exit Sub

End If

'Range("B12:O12").Copy
'Range("B" & rngFound.Row).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, 
SkipBlanks _
    :=False, Transpose:=False
'Range("B12:O12").ClearContents
For a = 5 To 14
If Cells(12, a) = "X" Or Cells(12, a) = "X" Then
Cells(rngFound.Row, a).Value = "X"
Else
Cells(rngFound.Row, a).Value = " "
End If
Next
Range("B12:O12").ClearContents

Range("C12").Formula = "=IF($B12="" "","" "",VLOOKUP($B12,$B18:$O150,2,FALSE))"
Range("D12").Formula = "=IF($B12="" "","" "",VLOOKUP($B12,$B18:$O150,3,FALSE))"
Range("E12").Formula = "=IF($B12="" "","" "",VLOOKUP($B12,$B18:$O150,4,FALSE))"
Range("F12").Formula = "=IF($B12="" "","" "",VLOOKUP($B12,$B18:$O150,5,FALSE))"
Range("G12").Formula = "=IF($B12="" "","" "",VLOOKUP($B12,$B18:$O150,6,FALSE))"
Range("H12").Formula = "=IF($B12="" "","" "",VLOOKUP($B12,$B18:$O150,7,FALSE))"
Range("I12").Formula = "=IF($B12="" "","" "",VLOOKUP($B12,$B18:$O150,8,FALSE))"
Range("J12").Formula = "=IF($B12="" "","" "",VLOOKUP($B12,$B18:$O150,9,FALSE))"
Range("K12").Formula = "=IF($B12="" "","" "",VLOOKUP($B12,$B18:$O150,10,FALSE))"
Range("L12").Formula = "=IF($B12="" "","" "",VLOOKUP($B12,$B18:$O150,11,FALSE))"
Range("M12").Formula = "=IF($B12="" "","" "",VLOOKUP($B12,$B18:$O150,12,FALSE))"
Range("N12").Formula = "=IF($B12="" "","" "",VLOOKUP($B12,$B18:$O150,13,FALSE))"
Range("O12").Formula = "=IF($B12="" "","" "",VLOOKUP($B12,$B18:$O150,14,FALSE))"

End Sub

*更新* 我能够找到一个需要更新的宏。这隐藏在另一个模块中。

Sub values()
'
'values Macro
'

'
    Range("B12:O12").Select
    Selection.Copy
    Range("B18").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _
    SkipBlanks:=False, Transpose:=False

End Sub

以下是我正在制作的新工作表的几张图片。

Update Athlete: A29:O29

"Master List"

1 个答案:

答案 0 :(得分:1)

在获取数据文件(带按钮的文件)中,尝试删除任何合并列(A& B)。对“主列表”执行相同操作 如您所见,您在单元格A29中输入名称,因此您需要更改B12 - > A29

Sub Update()
Dim rngSearch As Range, rngFound As Range
'Search range: Change sheets and range 
Set rngSearch = Sheets("Master list").Range("A3:O150")
' You type in cell A29 then search by cell A29, not B12
Set rngFound = rngSearch.Find(What:=Range("A29").Value, LookIn:=xlValues, LookAt:=xlPart)

If rngFound Is Nothing Then
    MsgBox "Please recheck name"
    Range("A29").Value = " "
    Exit Sub
End If
'I think this is meaningless
'Range("B12:O12").Copy
'Range("B" & rngFound.Row).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
'Range("B12:O12").ClearContents
'For a = 5 To 14
'If Cells(12, a) = "X" Or Cells(12, a) = "X" Then
'Cells(rngFound.Row, a).Value = "X"
'Else
'Cells(rngFound.Row, a).Value = " "
'End If
'Next
'Range("B12:O12").ClearContents

'You should pay attention to this as this is where your data will show: Row 29 from column B to O ( I assume that you remove the merge cell A&B)
' Source :master list
Range("B29").Formula = "=IF($A29="","",VLOOKUP($A29,'Master list'!$A3:$O150,2,FALSE))"
Range("C29").Formula = "=IF($A29="","",VLOOKUP($A29,'Master list'!$A3:$O150,3,FALSE))"
Range("D29").Formula = "=IF($A29="","",VLOOKUP($A29,'Master list'!$A3:$O150,4,FALSE))"
Range("E29").Formula = "=IF($A29="","",VLOOKUP($A29,'Master list'!$A3:$O150,5,FALSE))"
Range("F29").Formula = "=IF($A29="","",VLOOKUP($A29,'Master list'!$A3:$O150,6,FALSE))"
Range("G29").Formula = "=IF($A29="","",VLOOKUP($A29,'Master list'!$A3:$O150,7,FALSE))"
Range("H29").Formula = "=IF($A29="","",VLOOKUP($A29,'Master list'!$A3:$O150,8,FALSE))"
Range("I29").Formula = "=IF($A29="","",VLOOKUP($A29,'Master list'!$A3:$O150,9,FALSE))"
Range("J29").Formula = "=IF($A29="","",VLOOKUP($A29,'Master list'!$A3:$O150,10,FALSE))"
Range("K29").Formula = "=IF($A29="","",VLOOKUP($A29,'Master list'!$A3:$O150,11,FALSE))"
Range("L29").Formula = "=IF($A29="","",VLOOKUP($A29,'Master list'!$A3:$O150,12,FALSE))"
Range("M29").Formula = "=IF($A29="","",VLOOKUP($A29,'Master list'!$A3:$O150,13,FALSE))"
Range("N29").Formula = "=IF($A29="","",VLOOKUP($A29,'Master list'!$A3:$O150,14,FALSE))"

End Sub

更新以获取值sub

    Sub values()
    '
    'values Macro
    '

    ' Edit your target sheet here:
With Sheets("target sheet name")
' Avoid select, it will slow down your code
        .Range("B12:O12").Copy
        .Range("B18").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _
        SkipBlanks:=False, Transpose:=False
    end with
    End Sub

p / s:可能只需要这个:

=IFERROR(VLOOKUP(A29,'Master list'!$A3:$O150,2,FALSE),"")