简体   繁体   English

VBA复制工作表中的行,如果符合条件,则将其粘贴到其他工作表中

[英]VBA to copy row from worksheet and paste it into a different worksheet if criteria is met

I am currently trying to copy data from a worksheet into another If the dates in the columns F or G or H from the sheet "QJ Portfolio" are between the dates in the Cells B1 and D1 of the sheet "Archive". 我当前正在尝试将数据从一个工作表复制到另一个工作表中。如果“ QJ投资组合”工作表的F或G或H列中的日期介于工作表“存档”的单元格B1和D1中的日期之间。 To do so, I am using this code seen here 1 slightly modified. 为此,我正在使用此代码,这里看到1稍作修改。 The problem is that it's just copying every row and I can't understand why. 问题在于它只是复制每一行,而我不明白为什么。

Sub Archive()
   Dim LastRow As Long
   Dim i As Long, j As Long
   Dim DFrom As Date
   Dim DTo As Date

   DFrom = Worksheets("Archive").Cells(1, 2).Value
   DTo = Worksheets("Archive").Cells(1, 4).Value

   'Find the last used row in a Column: column A in this example
   With Worksheets("QJ Portfolio")
      LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
   End With

   MsgBox (LastRow)
   'first row number where you need to paste values in Sheet1'
   With Worksheets("Archive")
      j = .Cells(.Rows.Count, "A").End(xlUp).Row + 1
   End With

   For i = 1 To LastRow
       With Worksheets("QJ Portfolio")
           If .Cells(i, 6).Value >= DFrom & .Cells(i, 6).Value <= DTo Or         
.Cells(i, 7).Value >= DFrom & .Cells(i, 7).Value <= DTo Or .Cells(i, 8).Value >= DFrom & .Cells(i, 8).Value <= DTo Then
               .Rows(i).Copy Destination:=Worksheets("Archive").Range("A" & j)
               j = j + 1

           End If
       End With
   Next i
End Sub

It looks like you've confused your If then statement. 您似乎已经混淆了if语句。 Try the following. 请尝试以下方法。

    Sub Archive()
Dim LastRow As Long
Dim i As Long, j As Long
Dim DFrom As Date
Dim DTo As Date

DFrom = Worksheets("Archive").Cells(1, 2).Value
DTo = Worksheets("Archive").Cells(1, 4).Value

'Find the last used row in a Column: column A in this example
With Worksheets("QJ Portfolio")
 LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
End With

MsgBox (LastRow)
'first row number where you need to paste values in Sheet1'
With Worksheets("Archive")
  j = .Cells(.Rows.Count, "A").End(xlUp).Row + 1
End With

For i = 1 To LastRow
   With Worksheets("QJ Portfolio")
       If (.Cells(i, 6).Value >= DFrom And .Cells(i, 6).Value <= DTo) And (.Cells(i, 7).Value >= DFrom And .Cells(i, 7).Value <= DTo) And (.Cells(i, 8).Value >= DFrom And .Cells(i, 8).Value <= DTo) Then
           .Rows(i).Copy Destination:=Worksheets("Archive").Range("A" & j)
           j = j + 1

       End If
   End With
Next i
End Sub

暂无
暂无

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

相关问题 Excel 2007 VBA代码,如果满足单个条件,则将行从工作表1复制到工作表2 - Excel 2007 VBA Code to Copy Row from Worksheet 1 to Worksheet 2 if single criteria met Excel vba将来自不同工作表的单元格复制并粘贴到新工作表 - Excel vba to copy and paste a cells from different worksheets to new worksheet 如果条件满足,我如何使用 VBA 代码复制和粘贴特定单元格到另一个工作表的不同区域 - How can i use VBA Code to Copy and Paste Specific Cells if Condition is Met to different areas of another worksheet 最后一行粘贴到不同的工作表VBA - Last Row Paste to different Worksheet VBA Excel-VBA将数据从一个工作表复制到另一工作表并粘贴到新行 - Excel-VBA Copy data from one worksheet to another worksheet and paste in new row VBA代码可从一个工作表复制数据并将其粘贴到另一工作表的最后一行下方 - VBA code to copy data from one worksheet & paste below last row of another worksheet 复制,粘贴基于多个条件的选择到VBA中的另一个工作表 - copy, paste selection based on multiple criteria to another worksheet in VBA VBA,使用条件将很多行复制并粘贴到另一个工作表中 - VBA, copy and paste lots of rows in another worksheet with a criteria 在一个工作表中复制一行并粘贴到另一个工作表中 - copy a row in one worksheet and paste in another worksheet VBA复制并粘贴到新工作表 - VBA Copy and Paste to new Worksheet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM