Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 有条件地将特定列复制到其他工作表_Excel_Vba_Copy Paste_Worksheet - Fatal编程技术网

Excel 有条件地将特定列复制到其他工作表

Excel 有条件地将特定列复制到其他工作表,excel,vba,copy-paste,worksheet,Excel,Vba,Copy Paste,Worksheet,我下面的示例将在E列中找到“是”时将特定行从工作表1复制到工作表2。 我需要它只复制行的特定列,即B&C Fund Account Amount Gain/Loss As/Of? (Y/N) 1 11111 $15,000.00 -$1.51 YES 1 22222 $32,158.52 $78.14 YES 2 123123 $1.00 $0.00 NO 代

我下面的示例将在E列中找到“是”时将特定行从工作表1复制到工作表2。 我需要它只复制行的特定列,即B&C

Fund Account Amount         Gain/Loss   As/Of? (Y/N)
1    11111    $15,000.00       -$1.51        YES
1    22222    $32,158.52       $78.14        YES
2    123123   $1.00         $0.00        NO
代码:


您需要做的是使用计数器对执行
,该计数器将从上到下读取工作表中包含内容的所有单元格
F
是新工作表中要放置物料的行

尝试类似的方法:

sub alfa()
    UF = Cells(Rows.Count, 1).End(xlUp).Row
    for i = 1 to uf
        if sheetname.cells(i,Columnofyes).value = "YES" then 
            sheetwheretocopy.cells(f,columnwheretocopy).value = sheetname.cells(i,columnofdata).value
            f=f+1
        end if
    next i
end sub
试试这个:

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 lr
        If Range("E" & r).Value = "YES" 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

新要求:

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 = "YES" 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 = "YES" 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

这个很好用,谢谢。我还有一个问题。假设我希望返回“是”的前30行从A1向下粘贴到第2页,其余的从A35向下粘贴,这是如何实现的?再次感谢您的帮助,我搜索的时间太长了。您可以更改
x
的值以指示要从中开始复制的行。谢谢。然而,我需要它来粘贴前30个结果,从x开始,然后有一个5行的间隙,然后通过剩余的。这是怎么做到的谢谢你Jose传奇
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 = "YES" 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 = "YES" 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