查找前30个字符串匹配&粘贴在位置A,然后在位置B粘贴剩余的字符串匹配

时间:2015-01-15 03:41:49

标签: excel vba copy-paste string-matching worksheet-function

此代码将在我的一个工作表的第E行中搜索单词' white'并将该行上的名称粘贴到我的其他工作表中。我想要它做的是粘贴从x中的第一个匹配计算的前30个匹配,并在第二个定义的x处粘贴剩余的匹配。而不是从第2行到第30行搜索,这是它现在正在做的事情。如果这没有意义,标题就足够了。谢谢!

Sub As_Of_Analysis_Sorting()
Dim lr As Long, lr2 As Long, r As Long
Set Sh1 = ThisWorkbook.Worksheets("All Trades")
Set Sh2 = ThisWorkbook.Worksheets("As-Of Trades")
Sh1.Select

Sh2.Cells(1, 1).Value = "Account"
Sh2.Cells(1, 2).Value = "Amount"
lr = Sh1.Cells(Rows.Count, "A").End(xlUp).row
x = 2
For r = 2 To 30
    If Range("E" & r).Value = "WHITE" Then
        Sh2.Cells(x, 1).Value = Sh1.Cells(r, 2).Value
        Sh2.Cells(x, 2).Value = Sh1.Cells(r, 3).Value
        x = x + 1
    End If
Next r
x = 35
For r = 31 To lr
    If Range("E" & r).Value = "WHITE" Then
        Sh2.Cells(x, 1).Value = Sh1.Cells(r, 2).Value
        Sh2.Cells(x, 2).Value = Sh1.Cells(r, 3).Value
        x = x + 1
    End If
Next r
Sh2.Select
End Sub

1 个答案:

答案 0 :(得分:0)

Sub As_Of_Analysis_Sorting()
Dim lr As Long, lr2 As Long, r As Long, x As Long
Dim i as Long
Set Sh1 = ThisWorkbook.Worksheets("All Trades")
Set Sh2 = ThisWorkbook.Worksheets("As-Of Trades")


Sh2.Cells(1, 1).Value = "Account"
Sh2.Cells(1, 2).Value = "Amount"
lr = Sh1.Cells(Rows.Count, "A").End(xlUp).Row

x = 2
i = 0
For r = 2 To lr
    If Sh1.Range("E" & r).Value = "WHITE" Then
        Sh2.Cells(x, 1).Value = Sh1.Cells(r, 2).Value
        Sh2.Cells(x, 2).Value = Sh1.Cells(r, 3).Value
        x = x + 1
        i = i + 1
        If i = 30 Then x = 35
    End If
Next r

Sh2.Select
End Sub