VBA macro for copying conditional data to specific cells

前端 未结 5 2004
太阳男子
太阳男子 2021-01-26 11:57

I\'m new to programming in VBA and I\'m looking To take data from different worksheets that matches a condition. Then copy and paste from one specific cell to another specific

5条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-26 12:45

    Try this. I'm assuming you want to paste into row 18 and then 19 etc, and not 18 repeatedly!

    Sub CopyValues()
    
    'Declare variables
    'Declare sheet variables
    Dim Sourcews As Worksheet
    Dim Pastews As Worksheet
    
    'Declare counter variables
    Dim i As Long
    Dim n As Long
    Dim lastrow As Long
    
    Set Sourcews = ThisWorkbook.Sheets("Source")
    Set Pastews = ThisWorkbook.Sheets("Paste")
    
    lastrow = Sourcews.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    n = 18
    
    For i = 3 To lastrow
        If Sourcews.Cells(i, "AA").Value = "Needed Value" Then
            Sourcews.Cells(i, "AA").Copy Pastews.Cells(n, "C").Resize(, 6)
            n = n + 1
        End If
    Next
    
    End Sub
    

提交回复
热议问题