简体   繁体   English

使用复制创建价目表,使用 VBA 粘贴

[英]Creating Pricelist using Copy ,paste with VBA

I have a template Pricelist, were there are about 2600 rows, from this based on one column you pick what products you want.我有一个模板价目表,大约有 2600 行,根据这一列,您可以选择您想要的产品。 I want to copy these rows into a new sheet.我想将这些行复制到新工作表中。 Use the following code, but does some things it shouldn't.使用以下代码,但会做一些不应该做的事情。 Any suggestions?有什么建议么?

Private Sub CmdAdd_Click()
    Dim rng As Range
    Dim cel As Range

    Set rng = Range("B8:B39")
    For Each cel In rng
        If cel.Value = "X" Then
            cel.EntireRow.Select
            Application.CutCopyMode = False
            Selection.Copy Destination:=Sheets("NewPricelist").Range("A" & Rows.Count).End(xlDown).Offset(1, 0)
    
        End If
    Next cel
End Sub

I believe the issue you are facing is due to xlDown.我相信您面临的问题是由于 xlDown。
You go from rows.count (end of worksheet) and down...你从rows.count(工作表结尾)开始向下......

If we make that xlUp then the code copies the rows to the other sheet as expected.如果我们制作 xlUp,那么代码会按预期将行复制到另一个工作表。 (if that is the expected) (如果这是预期的)

Private Sub CmdAdd_Click()

    Dim rng As Range
    Dim cel As Range
       
       
        
    
    Set rng = Range("B8:B39")
    For Each cel In rng
        If cel.Value = "X" Then
            'cel.EntireRow.Select
            'Application.CutCopyMode = False
            cel.EntireRow.Copy Destination:=Sheets("NewPricelist").Range("A" & Rows.Count).End(xlUp).Offset(1, 0)    
        End If
    Next cel
End Sub

I also commented out the select to make the code faster我还注释掉了 select 以使代码更快

I have tested your code, but by the looks of it you are pasting the row all the way down in the bottom of the worksheet.我已经测试了您的代码,但从它的外观来看,您正在将行一直粘贴到工作表的底部。 On row 1048576, and then overwriting the values.在第 1048576 行,然后覆盖值。

I take it you want to paste them in an order on the top of your worksheet?我认为您想将它们按顺序粘贴到工作表的顶部?

Private Sub CmdAdd_Click()
Dim rng As Range
Dim cel As Range
Dim icount As Integer
Set rng = Range("B8:B39")

icount = 0

For Each cel In rng
    If cel.Value = "X" Then
    icount = icount + 1
        cel.EntireRow.Select
        Application.CutCopyMode = False
        Selection.Copy
        Selection.Copy Destination:=Sheets("NewPricelist").Range("A" & 0 + icount)
    End If
Next cel


End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM