VBA macro for copying conditional data to specific cells

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

    Aside from some syntax errors that others have discussed, you haven't specified what is bieng copied before you try to use the .paste method. I would just avoid the copy and paste methods (they are inefficient) and set the cells equal to the value of the range in the if statement like so:

    Sub CopyValues()
    
     'Declare variables
     'Declare sheet variables
     Dim Sourcews As Worksheet
     Dim Pastews As Worksheet
    
     'Declare counter variables
     Dim i As Integer
     Dim n As Integer
     Dim lastrow As Long
    
     Set Sourcews = ThisWorkbook.Sheets("sheet1")
     Set Pastews = ThisWorkbook.Sheets("sheet2")
    
      lastrow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    
     For i = 3 To lastrow
    
    If Sourcews.Range("AA" & i).Value = "Needed Value" Then
    
        Pastews.Range("C18") = Sourcews.Range("AA" & i).Value
        Pastews.Range("D18") = Sourcews.Range("AA" & i).Value
        Pastews.Range("E18") = Sourcews.Range("AA" & i).Value
        Pastews.Range("F18") = Sourcews.Range("AA" & i).Value
        Pastews.Range("G18") = Sourcews.Range("AA" & i).Value
    
    
    
    End If
    
    Next
    
    End Sub
    

    Or you could set the value as a variable for cleaner looking code, like this:

    Sub CopyValues()
    
     'Declare variables
     'Declare sheet variables
     Dim Sourcews As Worksheet
     Dim Pastews As Worksheet
    
     'Declare counter variables
     Dim i As Integer
     Dim n As Integer
     Dim lastrow As Long
     Dim x As String
    
     Set Sourcews = ThisWorkbook.Sheets("sheet1")
     Set Pastews = ThisWorkbook.Sheets("sheet2")
    
      lastrow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    
     For i = 3 To lastrow
    
    If Sourcews.Range("AA" & i).Value = "Needed Value" Then
    
        x = Sourcews.Range("AA" & i).Value
        Pastews.Range("C18") = x
        Pastews.Range("D18") = x
        Pastews.Range("E18") = x
        Pastews.Range("F18") = x
        Pastews.Range("G18") = x
    
    
    
    End If
    
    Next
    
    End Sub
    

    Or, to make the code even more concise, you can combine the range that is receiving the copied value as Pastews.Range("C18:G18") = x like this:

    Sub CopyValues()
    
     'Declare variables
     'Declare sheet variables
     Dim Sourcews As Worksheet
     Dim Pastews As Worksheet
    
     'Declare counter variables
     Dim i As Integer
     Dim n As Integer
     Dim lastrow As Long
     Dim x As String
    
     Set Sourcews = ThisWorkbook.Sheets("sheet1")
     Set Pastews = ThisWorkbook.Sheets("sheet2")
    
      lastrow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    
     For i = 3 To lastrow
    
    If Sourcews.Range("AA" & i).Value = "Needed Value" Then
    
        x = Sourcews.Range("AA" & i).Value
        Pastews.Range("C18:G18") = x
    
    End If
    
    Next
    
    End Sub
    

    I know I posted a lot, but I wanted to show you a progression of how your could can be more concise and efficient. I hope it helps.

提交回复
热议问题