简体   繁体   English

Excel 2 合一

[英]Excel 2 For in 1

I bumped into a problem as i tried to add 2 values to be searched and its seems something is wrong with my code.我在尝试添加 2 个要搜索的值时遇到了一个问题,我的代码似乎有问题。

The part is that I added 2 For each , but i think it's not how it suppose to go.部分是我添加了 2 For each ,但我认为这不是它应该的样子。

I have 2 words : PMC ( column F) and PRM ( Column C) .我有 2 个词:PMC(F 栏)和 PRM(C 栏)。 If they match in sheet Main Data, then copy that row and paste to Second Data.如果它们在工作表 Main Data 中匹配,则复制该行并粘贴到 Second Data。


Sub Copyrow()

Dim c As Range

Dim D As Range

Dim j As Integer

Dim Source As Worksheet

Dim Target As Worksheet

Dim lRow As Long

Set Source = ActiveWorkbook.Worksheets("Main DATA")

Set Target = ActiveWorkbook.Worksheets("Second Data")

Lastrow = ActiveSheet.Cells(Rows.Count, "F").End(xlUp).Row + 1

j = Cells(Rows.Count, 2).End(xlUp).Row

For Each c In Source.Range("F1:F20000")

For Each D In Source.Range("C1:C20000") ' Second ( FOR ) for addition search word.

If c = "PMC" & D = "PRM" Then

Source.Range("A" & c.D.Row, "O" & c.D.Row).copy

Target.Range("A" & j, "O" & j).PasteSpecial Paste:=xlPasteValuesAndNumberFormats, _

Operation:=xlNone, SkipBlanks:=False, Transpose:=False

j = j + 1

End If

Next D

With Range("H1:H5000")

.NumberFormat = "General"

.Value = .Value

End With

End Sub

This is untested, the approach is to do a For over the rows and check the cell value of column C and F in the row i .这是未经测试的,方法是对行执行For并检查行i中列 C 和 F 的单元格值。

Sub Copyrow()
   Dim j As Long
   Dim Source As Worksheet
   Dim Target As Worksheet
   Dim lRow As Long

   Set Source = ActiveWorkbook.Worksheets("Main DATA")
   Set Target = ActiveWorkbook.Worksheets("Second Data")

   lRow = Source.Cells(Source.Rows.Count, "F").End(xlUp).Row + 1

   j = Target.Cells(Target.Rows.Count, 2).End(xlUp).Row

   Dim i As Long
   For i = 1 to lRow
       If Source.Cells(i, 6).Value = "PMC" And Source.Cells(i,3).Value = "PRM" Then
           Source.Range(Replace("A#:O#","#",i)).Copy
           Target.Range(Replace("A#:O#","#", j).PasteSpecial Paste:=xlPasteValuesAndNumberFormats, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False
           j = j + 1
       End If
   Next i

   With Target.Range("H1:H5000")
       .NumberFormat = "General"
       .Value = .Value
   End With
End Sub

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

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