Copy and paste the entire line
Feature: I am working on a small project where I need to search every cell in a column for the word "Unknown" if it contains that word, and then copy that entire row to a new sheet.
Problem: I get "Object does not support this property method". mistake. I believe it is somewhere inside the copy statement (not the destination). It's as simple as that, but I can't seem to solve this problem.
Sub CheckRows()
Dim b As Range
Dim SrchRng As Range
Set b = ActiveWorkbook.Sheets("Sheet 2").Range("A1")
Set SrchRng = ActiveWorkbook.Sheets("Sheet 1").Range("G1")
Do While SrchRng.Value <> ""
If SrchRng.Value = "Unknown" Then
Worksheets("Sheet 1").SrchRng.EntireRow.Copy _
Destination:=Worksheets("Sheet 2").b
Set b = b.Offset(1, 0)
Set SrchRng = SrchRng.Offset(1, 0)
Else: Set SrchRng = SrchRng.Offset(1, 0)
End If
Loop
End Sub
source to share
Try Range.EntireRow.Value
Property
b.EntireRow.Value = SrchRng.EntireRow.Value
IN
Do While SrchRng.Value <> ""
If SrchRng.Value = "Unknown" Then
b.EntireRow.Value = SrchRng.EntireRow.Value
Set b = b.Offset(1, 0)
Set SrchRng = SrchRng.Offset(1, 0)
Else: Set SrchRng = SrchRng.Offset(1, 0)
End If
Loop
source to share
Consider the following situation:
Sheet 1
has a data block with "unknown" entries in column G and is Sheet 2
empty.
By defining the data block as an object Range
and applying .Autofilter
that identifies the "Unknown" records, we can simply copy the results to Sheeet 2
. The following heavily commented script does exactly that:
Option Explicit
Sub CheckRowsWithAutofilter()
Dim DataBlock As Range, Dest As Range
Dim LastRow As Long, LastCol As Long
Dim SheetOne As Worksheet, SheetTwo As Worksheet
'set references up-front
Set SheetOne = ThisWorkbook.Worksheets("Sheet 1")
Set SheetTwo = ThisWorkbook.Worksheets("Sheet 2")
Set Dest = SheetTwo.Cells(1, 1) '<~ this is where we'll put the filtered data
'identify the "data block" range, which is where
'the rectangle of information that we'll apply
'.autofilter to
With SheetOne
LastRow = .Range("G" & .Rows.Count).End(xlUp).Row
LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
Set DataBlock = .Range(.Cells(1, 1), .Cells(LastRow, LastCol))
End With
'apply the autofilter to column G (i.e. column 7)
With DataBlock
.AutoFilter Field:=7, Criteria1:="=*Unknown*"
'copy the still-visible cells to sheet 2
.SpecialCells(xlCellTypeVisible).Copy Destination:=Dest
End With
'turn off the autofilter
With SheetOne
.AutoFilterMode = False
If .FilterMode = True Then .ShowAllData
End With
End Sub
Here is the result on Sheet 2
:
source to share