VBA macro for copying conditional data to specific cells

前端 未结 5 2029
太阳男子
太阳男子 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:36

    I was working through the duplicate question for this and provided this answer:

    Try:

    Sub CopyValues()
    
    'Declare counter variables
    Dim i As Integer, j as Integer, lastrow As Long
    'Declare variables
    Dim Sourcedataws As Worksheet, WStotransfer As Worksheet
    'Declare sheet variables
    Set Sourcedataws = ThisWorkbook.Sheets("Source Data")
    Set WStotransferws = ThisWorkbook.Sheets("WStotransfer")
    
    lastrow = Sourcedataws.Cells(Sourcedataws.Rows.Count, "A").End(xlUp).Row
    
    WStotransferws.Range("C18:I18").ClearContents
    
    For i = 2 To lastrow
    If WStotransferws.Range("I18").Value="" Then
        If Sourcedataws.Range("AA" & i).Value = "Condition" Then
            Sourcedataws.Range("A"&i).Copy 
            j=WStotransferws.Cells(18, WStotransferws.Columns.Count).End(xlToLeft).Column
            WStotransferws.Cells(18,j+1).PasteSpecial xlPasteValues
            End If
        Else
        End If
    Next i
    
    End Sub
    

    Other post found: VBA Need a Nested Loop to shift columns

    There's a long conversation with the poster about details that were not in the post.

提交回复
热议问题