VBA Excel:如果语句不起作用

我试图将excel文档中的表单上的一些信息分类到不同的组中,使用另一个表单中的信息,这两个表单都有一个列来标识它们。 具体来说,一个男人和女人正在玩游戏,我想把第二张纸上的游戏结果分成三列, 总体来说,男性和女性,但玩家信息保存在第一张表中,结果表中唯一的识别特征是它们在第一张表中的唯一ID号。 所以基本上我有两张纸,看起来像这样:

第1页:

A | B | C | D Name | Gender| | ID Alex | M | | 171 Alexis | F | | 172 Kelly | F | | 177 Chris | M | | 179 

第2页:

  A | B | C | D ID | | | Score 171 | | | 58.2 172 | | | 67.1 177 | | | 73.4 179 | | | 68.95 

现在我只是试图让它工作,以便所有的身份证和分数被确定为男性复制到另一张表,为此我有两个不同的解决scheme,但都没有工作。

 Dim sh1 As Worksheet Dim sh2 As Worksheet Dim sh3 As Worksheet Dim lc1, lc2, x, y, i, vLook, z Set sh1 = Sheets("players") Set sh2 = Sheets("Results") Set sh3 = Sheets("temp") rcount1 = sh1.Cells(Rows.Count, "A").End(xlUp).Row rcount2 = sh2.Cells(Rows.Count, "A").End(xlUp).Row x = 2 y = 2 z = 2 Dim t As Integer Dim k As Integer k = 1 t = 1 For t = 1 To rcount2 If sh2.Range("A1").Offset(t).Value = sh1.Range("D1").Offset(t).Value Then If sh1.Range("B1").Offset(t).Value = "M" Then sh3.Range("A1").Offset(k).Value = sh2.Range("A1").Offset(t).Value sh3.Range("B1").Offset(k).Value = sh2.Range("D1").Offset(t).Value k = k + 1 End If End If Next t 

如果我删除了“if”语句,范围将被复制,但是“if”语句不会执行任何操作。

我的另一个解决scheme是:

 For i = 2 To rcount2 vLook = Application.WorksheetFunction.VLookup(sh1.Cells(i, 4), Range(sh2.Cells(1, 1), sh2.Cells(rcount2, 4)), 4, "false") If sh1.Cells(i, 2) = "M" Then sh3.Cells(x, 1) = sh1.Cells(i, 4) sh3.Cells(x, 2) = vLook x = x + 1 ElseIf sh1.Cells(i, 2) = "F" Then sh3.Cells(y, 3) = sh1.Cells(i, 4) sh3.Cells(y, 4) = vLook y = y + 1 Else sh3.Cells(z, 5) = sh1.Cells(i, 4) sh3.Cells(z, 6) = vLook z = z + 1 End If Next i 

但是,它所做的就是把所有东西都看作只适合于“其他”。所以基本上就我所知,在表1的B列没有看到M或F的任何帮助或build议。 。

我只是基于你上面提供的信息。
此代码将所有男性ID和分数分别复制到Colums A and B Temp表单中。

 Dim rcount1, rcount2, t as long Dim sh1, sh2, sh3 as Worksheet Dim wb as Workbook Dim score Set wb = Thisworkbook 'i included wb for flexibility, your call if you want to adopt Set sh1 = wb.Sheets("Players") Set sh2 = wb.Sheets("Results") Set sh3 = wb.Sheets("Temp") rcount1 = sh1.Cells(Rows.Count, "A").End(xlUp).Row For t = 2 to rcount1 If sh1.Range("B" & t).Value Like "*M*" Then 'rcount2 should be inside your loop, otherwise you'll only overwrite values rcount2 = sh3.Cells(Rows.Count, "A").End(xlUp).Row sh1.Range("D" & t).copy sh3.Range("A" & rcount2 + 1) 'I used Vlookup function instead of nesting another loop With Application.WorksheetFunction score = .Vlookup(sh1.Range("D" & t).Value, sh2.Columns("A:D"), 4, 0) sh3.Range("B" & rcount2 + 1).value = score End with End if Next t End Sub 

所以这个代码就像你上面的作品的组合。
希望这个得到你的开始。

此代码仅合并“玩家”和“结果”表中的信息。
意思是,它不检查一个ID是否已经存在于“Temp”表中。
另外,它不总结。
我把其余的留给你。