VBA Excel:如果声明不起作用

时间:2013-10-27 01:23:43

标签: excel vba excel-vba

我正在尝试使用其他工作表中的信息将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

现在我只是想让它发挥作用,以便将所有识别为男性的ID和分数复制到另一张纸上,为此我有两种不同的解决方案,但都不起作用。

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'语句则不会复制。

我的另一个解决方案是:

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的内容。任何帮助或建议都将受到赞赏。

1 个答案:

答案 0 :(得分:2)

我的意思是基于您上面提供的信息。
此代码将所有男性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

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

此代码仅合并“玩家”和“结果”表中的信息 这意味着,它不会检查“Temp”表中是否已存在ID 此外,它没有总结 我把剩下的留给你了。