Excel 如何根据列中的条件将行从一个工作表复制到另一个工作表--仅粘贴值和格式(而不是公式)?
此代码按预期工作,以复制B列中给定值“xxx”的单元格。 问题是它复制了整行内容,包括公式。我只想复制单元格值和格式,不想复制公式Excel 如何根据列中的条件将行从一个工作表复制到另一个工作表--仅粘贴值和格式(而不是公式)?,excel,vba,Excel,Vba,此代码按预期工作,以复制B列中给定值“xxx”的单元格。 问题是它复制了整行内容,包括公式。我只想复制单元格值和格式,不想复制公式 Sub CommandButton1_Click() Dim LastRow As Long Dim i As Long, j As Long 'Find the last used row in a Column: column A in this example (source sheet = sheet2) With Workshee
Sub CommandButton1_Click()
Dim LastRow As Long
Dim i As Long, j As Long
'Find the last used row in a Column: column A in this example (source sheet = sheet2)
With Worksheets("Sheet2")
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
End With
'Message box to confirm how many rows were scanned to ensure all rows were scanned
MsgBox ("Number of rows scanned: " & LastRow)
'First row number where you need to paste values in Sheet3 (destination sheet = sheet3)'
With Worksheets("Sheet3")
j = .Cells(.Rows.Count, "A").End(xlUp).Row + 1
End With
For i = 1 To LastRow
With Worksheets("Sheet2")
If .Cells(i, 2).Value = "xxx" Then
.Rows(i).Copy Destination:=Worksheets("Sheet3").Range("A" & j)
j = j + 1
End If
End With
Next i
End Sub
我试着把最后一部分改成
.Rows(i).Copy
.Range("A" & j).PasteSpecial xlPasteValuesAndNumberFormats
但是,这会尝试将行粘贴到同一工作表中(可能是因为它位于“With”下)。我无法更改行粘贴的目标。理想情况下,我希望将复制的行粘贴到Sheet3中。而不是复制粘贴使用value=value,以便:
.Rows(j).value = .rows(i).value
要移动到其他图纸,可以添加图纸参照和最后一行:
sheets(3).rows(sheets(3).cells(sheets(3).rows.count,1).end(xlup).offset(1,0).row).value = .rows(i).value
Edit1:
用你的j
sheets(3).rows(j).value = .rows(i).value
Public Function FilterByTable(fromWs作为工作表,destWs作为工作表,tableFilter作为字符串)作为布尔值
从As范围变暗复制
暗淡的光线和长的一样
“假设是假的
FilterByTable=False
和弗洛姆
.AutoFilterMode=False
'这将给出此范围内最后一行的值
lRow=.Range(“A”&.Rows.Count).End(xlUp).Row
带.Range(“A1:A”和lRow)
'查找符合此筛选器的任何行,即val=tableFilter
.AutoFilter字段:=1,准则1:=“=”&表格过滤器
设置copyFrom=.SpecialCells(xlCellTypeVisible).EntireRow
以
.AutoFilterMode=False
以
用destWs
'一些错误检查,因为如果您尝试在空数据集上执行此操作,此操作将失败
如果Application.WorksheetFunction.CountA(.Cells)为0,则
lRow=.Cells.Find(内容:=“*”_
之后:=.范围(“A1”)_
看:=xlPart_
LookIn:=xl公式_
搜索顺序:=xlByRows_
搜索方向:=xlPrevious_
MatchCase:=False)。行
其他的
lRow=1
如果结束
copyFrom.Copy.Rows(lRow)
以
FilterByTable=True
端函数
谢谢您的回复!我担心我对VBA太无知,无法理解如何将其实现到我的代码中以使其正常工作,但是……您能否澄清我应该将对目标工作表的引用添加到哪些部分?@Tester_Y这将进入您的循环,替换复制/粘贴行
Public Function FilterByTable(fromWs As Worksheet, destWs As Worksheet, tableFilter As String) As Boolean
Dim copyFrom As Range
Dim lRow As Long
'Assume false
FilterByTable = False
With fromWs
.AutoFilterMode = False
'This gives the value for the last row in this range
lRow = .Range("A" & .Rows.Count).End(xlUp).Row
With .Range("A1:A" & lRow)
'Looking for any row that meets this filter i.e. val=tableFilter
.AutoFilter Field:=1, Criteria1:="=" & tableFilter
Set copyFrom = .SpecialCells(xlCellTypeVisible).EntireRow
End With
.AutoFilterMode = False
End With
With destWs
'Some error checking since this will fail if you try to perform the operation on an empty data set
If Application.WorksheetFunction.CountA(.Cells) <> 0 Then
lRow = .Cells.Find(What:="*", _
After:=.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
Else
lRow = 1
End If
copyFrom.Copy .Rows(lRow)
End With
FilterByTable = True
End Function