paste special values in vba

后端 未结 4 1592
太阳男子
太阳男子 2021-01-20 22:54

I am working on a small project which requires me to copy and paste certain columns if I detect \"true\" in the row. I am trying to paste these selected columns onto a diffe

4条回答
  •  鱼传尺愫
    2021-01-20 23:12

    I believe the code you provided is much faster than earlier. However to help other understand easier, why not put some comment?

    I have done that for you.

    Sub ExtractData()
    
    Dim selectedRange As Range ' Range to check
    Dim Cell As Range
    Dim iTotalRows As Integer ' Selected total number of rows
    Dim i As Integer ' marker to identify which row to paste in new sheet
    
    Dim shtNew As Worksheet
    Dim shtData As Worksheet
    
    Set shtData = Sheets("data")
    Set shtNew = Sheets.Add()
    shtNew.Name = "Analyzed data"
    
    iTotalRows = shtData.UsedRange.Rows.count
    Set selectedRange = shtData.Range("F2:F" & iTotalRows)
    
    i = 1
    
    ' Check the selected column value one by one
    For Each Cell In selectedRange.Cells
    
         If Cell.Value = "True" Then
            Cell.Copy shtNew.Range("A" & i)
    
            ' Copy the brand to column B in "Analyzed data" sheet
            shtNew.Range("B" & i).Value = _
                           shtData.Cells(Cell.Row, 2).Value
            i = i + 1
        End If
    
    Next ' Check next cell in selected range
    
    End Sub
    

提交回复
热议问题