我需要一点Excel的宏帮助。
我需要创建一个自动查找用户并复制其他工作表中的值的宏:
我有一张包含用户及其杀戮和死亡的值的工作表,我创建了3张(3个不同的用户组),我需要宏复制值自动查找用户和复制值。
更好地描述图片:
----(复制此值)----->
答案 0 :(得分:3)
您不需要使用宏,使用工作表函数VLOOKUP
就足够了。
例如,如果第1行中的标题和A列中的用户,您在单元格B2中放入的内容(第一个用户的排名数)将为=VLOOKUP($A2;Values!$A$2:$C$9;2;FALSE)
和C2将是=VLOOKUP($A2;Values!$A$2:$C$9;3;FALSE)
。
该函数的参数(您也可以在链接文档中找到)是:
vlookup
返回结果的值数组只会查看第一列,但由于您要返回其他列的结果,因此我们在公式中包含A:C列false
)或者大概是否匹配(true
)。答案 1 :(得分:2)
如果我了解您的目标,您应该可以使用VLOOKUPs
执行此操作(无需VBA)
答案 2 :(得分:1)
以下源代码可以解决您的问题。
Option Explicit
Dim MyResultWorkbook As Workbook
Dim ValuesWorksheet As Worksheet
Dim SniperWorksheet As Worksheet
Dim ARsWorksheet As Worksheet
Sub CopyResult()
Set MyResultWorkbook = ActiveWorkbook
Set ValuesWorksheet = MyResultWorkbook.Sheets("Values")
Set SniperWorksheet = MyResultWorkbook.Sheets("Sniper")
Set ARsWorksheet = MyResultWorkbook.Sheets("Ars")
Dim SniperLastRow As Long
Dim ARLastRow As Long
Dim RowPointer As Long
Dim ValuePointer As Long
ValuePointer = 2
'Update the Sniper worksheets
SniperLastRow = SniperWorksheet.Cells(SniperWorksheet.Rows.Count, "A").End(xlUp).Row
For RowPointer = 2 To SniperLastRow
Do While (SniperWorksheet.Range("A" & RowPointer).Value <> ValuesWorksheet.Range("A" & ValuePointer).Value)
ValuePointer = ValuePointer + 1
Loop
SniperWorksheet.Range("A" & RowPointer).Offset(0, 1).Value = ValuesWorksheet.Range("A" & ValuePointer).Offset(0, 1).Value 'copy kill
SniperWorksheet.Range("A" & RowPointer).Offset(0, 2).Value = ValuesWorksheet.Range("A" & ValuePointer).Offset(0, 2).Value 'copy death
ValuePointer = 2
Next
'Update the Ars worksheets
ARLastRow = ARsWorksheet.Cells(ARsWorksheet.Rows.Count, "A").End(xlUp).Row
For RowPointer = 2 To ARLastRow
Do While (ARsWorksheet.Range("A" & RowPointer).Value <> ValuesWorksheet.Range("A" & ValuePointer).Value)
ValuePointer = ValuePointer + 1
Loop
ARsWorksheet.Range("A" & RowPointer).Offset(0, 1).Value = ValuesWorksheet.Range("A" & ValuePointer).Offset(0, 1).Value 'copy kill
ARsWorksheet.Range("A" & RowPointer).Offset(0, 2).Value = ValuesWorksheet.Range("A" & ValuePointer).Offset(0, 2).Value 'copy death
ValuePointer = 2
Next
End Sub