【问题标题】:Find first 30 string matches & paste at location A, then paste remaining string matches at location B查找前 30 个字符串匹配项并粘贴到位置 A,然后将剩余的字符串匹配项粘贴到位置 B
【发布时间】:2015-01-15 03:41:48
【问题描述】:

此代码将在我的一个工作表的 E 行中搜索单词“white”并将该行上的名称粘贴到我的另一个工作表中。我想要它做的是粘贴从 x 中的 first match 计数的前 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

【问题讨论】:

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


    【解决方案1】:
    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
    

    【讨论】:

    • 简直完美。谢谢蒂姆
    猜你喜欢
    • 1970-01-01
    • 2015-08-05
    • 1970-01-01
    • 1970-01-01
    • 2021-04-04
    • 1970-01-01
    • 2015-05-20
    • 2020-01-07
    • 1970-01-01
    相关资源
    最近更新 更多