功能:我正在开展一个小型项目,我需要在列中搜索单词" Unknown"如果它包含该单词,则将整行复制到新表格上。
问题:我得到"对象不支持此属性方法。"错误。我相信它是在复制声明(不是目的地)内的某个地方。它很简单但我似乎无法解决这个问题。
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
答案 0 :(得分:2)
试试Range.EntireRow.Value
财产
b.EntireRow.Value = SrchRng.EntireRow.Value
在
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
答案 1 :(得分:2)
考虑以下情况:
Sheet 1
有一个数据块,&#34;未知&#34; G列中的条目,Sheet 2
为空。
将数据块定义为Range
个对象,并应用.Autofilter
来标识&#34;未知&#34;条目我们可以简单地将过滤后的结果复制到Sheeet 2
。以下评论很严重的脚本就是这样做的:
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
以下是Sheet 2
上的输出: