将If Statment公式分配给variables并select单元格

我正在尝试将一个if / index / match公式分配给一个variables。 该公式输出我需要在For循环中使用的playerId来查找表格中的值并粘贴到右侧单元格中的玩家信息。

这是我的代码:

Public playerId as Range Sub SubmitEdit() Dim x As Long Dim playRange As Range Dim pcell As Range Dim counter As Long counter = 0 x = ThisWorkbook.Sheets("Welcome").Range("A48").Value Set playRange = ThisWorkbook.Sheets("PlayerRecord").Range("A:A") lastRow = Sheets("PlayerRecord").Range("A" & Rows.Count).End(xlUp).Row 'runtime error!! playerId.formula = "=IF($A46="""","""",INDEX(PlayerRecord!A:A,MATCH(INDEX(Registration!$A:$A,MATCH($A46,Registration!$C:$C,0)),PlayerRecord!$A:$A,0)))" For Each pcell In playRange.Cells counter = counter + 1 If counter = lastRow Then Exit For If pcell.Value = playerId Then 'Select row ActiveCell.Offset(0, 1).Select 'Go to PlayerRecord Worksheet and paste information Worksheets("Welcome").Range("B48:G48").Copy _ Destination:=ActiveCell.Offset(0, 0) 'ActiveCell.Offset(0, 1).PasteSpecial Paste:=xlPasteValues End If 'Call HideSheets 'returns to main sheet 'Sheets("Welcome").Select 'Application.CutCopyMode = False Next pcell Exit Sub End Sub 

当我运行脚本运行时错误91:对象variables或未设置块variablespopup。

更新:更改palyerID范围,现在得到错误:运行时错误91。 对象variables或未设置块

解决了所有的错误后,最终的工作代码应该是:

 Public playerId As Integer Sub SubmitEdit() Dim x As Long Dim playRange As Range Dim pcell As Range Dim counter As Long x = ThisWorkbook.Sheets("Welcome").Range("A48").Value Set playRange = ThisWorkbook.Sheets("PlayerRecord").Range("A:A") lastRow = Sheets("PlayerRecord").Range("A" & Rows.Count).End(xlUp).Row playerId = Application.Evaluate("=IF($A46="""","""",INDEX(PlayerRecord!A:A,MATCH(INDEX(Registration!$A:$A,MATCH($A46,Registration!$C:$C,0)),PlayerRecord!$A:$A,0)))") counter = 0 For Each pcell In playRange counter = counter + 1 If counter = lastRow Then Exit For If pcell.Value = playerId Then 'Select row ActiveCell.Offset(0, 1).Select 'Go to PlayerRecord Worksheet and paste information Worksheets("Welcome").Range("B48:G48").Copy _ Destination:=ActiveCell 'ActiveCell.Offset(0, 1).PasteSpecial Paste:=xlPasteValues End If 'Call HideSheets 'returns to main sheet 'Sheets("Welcome").Select 'Application.CutCopyMode = False Next pcell End Sub 

略有改进的版本可能如下:

 Sub SubmitEditVerions2() Dim x As Long Dim pcell As Range Dim counter As Long Dim playRange As Range Dim playerId As Integer Dim lastRow As Long With ThisWorkbook If VarType(.Sheets("Welcome").Range("A48").Value) = vbLong Then x = .Sheets("Welcome").Range("A48").Value Else MsgBox "Error. Cell A48 on sheet 'Welcome' is not a number." & Chr(10) & "Aborting..." Exit Sub End If Set playRange = .Sheets("PlayerRecord").Range("A:A") lastRow = .Sheets("PlayerRecord").Range("A" & .Sheets("PlayerRecord").Rows.Count).End(xlUp).Row If VarType(Application.Evaluate("=IF($A46="""","""",INDEX(PlayerRecord!A:A,MATCH(INDEX(Registration!$A:$A,MATCH($A46,Registration!$C:$C,0)),PlayerRecord!$A:$A,0)))")) = vbInteger Then playerId = Application.Evaluate("=IF($A46="""","""",INDEX(PlayerRecord!A:A,MATCH(INDEX(Registration!$A:$A,MATCH($A46,Registration!$C:$C,0)),PlayerRecord!$A:$A,0)))") Else MsgBox "Error. Not a valid PlayerID." & Chr(10) & "Aborting..." Exit Sub End If counter = 0 For Each pcell In playRange counter = counter + 1 If counter = lastRow Then Exit For If pcell.Value = playerId Then 'Select row ActiveCell.Offset(0, 1).Select 'Go to PlayerRecord Worksheet and paste information .Worksheets("Welcome").Range("B48:G48").Copy _ Destination:=ActiveCell 'ActiveCell.Offset(0, 1).PasteSpecial Paste:=xlPasteValues End If 'Call HideSheets 'returns to main sheet 'Sheets("Welcome").Select 'Application.CutCopyMode = False Next pcell End With End Sub