简体   繁体   English

代码不会将特定的单元格复制并粘贴到新工作表VBA中

[英]Code Won't Copy and Paste Specific Cells Into New Sheet VBA

I am using this code to check each row in worksheet "Report2" for the phrase "Chicago" and copy and paste any rows with "Chicago" in them into a new sheet. 我正在使用此代码检查工作表“ Report2”中的每一行是否包含短语“ Chicago”,并将其中包含“ Chicago”的任何行复制并粘贴到新表中。 However, it is not working. 但是,它不起作用。 Any help on why would be appreciated. 任何关于为什么的帮助将不胜感激。

Code: 码:

Sub BranchCount()

Dim s As Worksheet
Dim LastRow As Long

Set s = Worksheets("Report 1")
LastRow = s.UsedRange.SpecialCells(xlCellTypeLastCell).Row

Worksheets("Report 1").Select
Range("A1:J" & LastRow).Select
Selection.Copy

Sheets.Add.Name = "Report2"
Selection.PasteSpecial xlPasteValues
Range("A1").EntireRow.Delete
Range("B1").EntireRow.Delete
Range("C1").EntireRow.Delete

Dim Z As Range
Dim Y As String

Y = W
W = "Chicago"

Sheets("Report2").Range("A1").Select

For Each Z In Range("J1:J" & LastRow)
    If Y = Z.Value Then
        Z.EntireRow.Copy
            Sheets("Clean").Select
                Range("A700").End(xlUp).Offset(1, 0).Select
                Selection.PasteSpecial xlPasteValues
            Sheets("Report2").Select
    End If
Next

End Sub

Let me know if you can help. 让我知道您能否提供帮助。 Thanks! 谢谢!

no need for any helper ("Report2") sheet 无需任何帮助(“ Report2”)表

you could filter the relevant part of data cells and copy selected cells directly to "Clean" sheet as follows 您可以过滤数据单元格的相关部分,然后将所选单元格直接复制到“ Clean”工作表中,如下所示

Option Explicit

Sub BranchCount()

Dim s1 As Worksheet, sC As Worksheet
Dim LastRow As Long

Set s1 = Worksheets("Report 1")
Set sC = Sheets("Clean")

With s1
    LastRow = .Cells(.Rows.Count, "J").End(xlUp).Row
    With .Range("A1:J" & LastRow)
        .AutoFilter field:=10, Criteria1:="Chicago"
        With .Offset(1).Resize(.Rows.Count - 1)
            If Application.WorksheetFunction.Subtotal(103, .Columns("J")) > 1 Then .SpecialCells(xlCellTypeVisible).Copy Destination:=sC.Range("A700").End(xlUp).Offset(1, 0)
        End With
        .AutoFilter
    End With
End With

End Sub 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 VBA代码可根据条件复制特定列中的单元格并将其粘贴(值)到另一张工作表中特定列中的单元格 - VBA code to copy cells in specific column and paste (value) it to cells in specific column on another Sheet based on conditions 如果没有单元格可以使用特殊粘贴复制并粘贴到新工作表中,则会出现代码错误 - Code errors if there are no cells to copy and paste into a new sheet using paste special vba代码以查找特定值并在该列的后续单元格中复制值并粘贴到新工作表中 - vba code to find a specific value and copy value in subsequent cell in the column and paste in a new sheet Excel VBA代码将行复制并粘贴到新工作表失败 - Excel VBA code to copy and paste rows to new sheet failing 复制并粘贴到使用相同VBA代码创建的新工作表中 - Copy and paste into a new sheet that is created in the same VBA code 如果条件满足,则用于复制和粘贴特定单元格的 VBA 代码 - VBA Code to Copy and Paste Specific Cells if Condition is Met Excel VBA:将表格中的单元格复制到新单元格的代码 - Excel VBA: code to copy cells from sheet to new one 使用 VBA 将列复制并粘贴到新工作表 - Using VBA to Copy and Paste Columns to New sheet 如何使用 VBA 复制 excel 中的一列单元格,直到出现空白并将其粘贴到新工作表中? - How can I copy a column of cells in excel, with VBA, until there is a blank and paste it into a new sheet? Excel VBA代码,用于在一个工作簿中选择包含特定文本字符串的单元格,然后将这些单元格复制并粘贴到新工作簿中 - Excel VBA code to select cells that contain a certain text string in one workbook, and then copy and paste these cells into a new workbook
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM