繁体   English   中英

尝试创建搜索/复制/粘贴VBA代码

[英]Trying to create a search/copy/paste VBA code

我是VBA的新手,我正在尝试自动执行电子表格中的报告功能,而这需要避免手工操作。 我已经创建了以下代码,但是我继续收到错误消息。 我将解释我要实现的目标,并希望我们能够找到解决该问题的方法。

我有两张纸,我想查看工作表Sheet1的L列,对于所有值为“ NO”的单元格,我想将值复制到同一行的列A中,并将其粘贴到工作表Sheet2的最后一行在A栏中。

听起来很简单,但是我无法理解代码。

以下代码有什么问题?

    Sub SearchMacro()

    Dim wb As Workbook
    Dim ws As Worksheet
    Set wb = ActiveWorkbook
    Set ws = Sheets("Sheet1")
    wb.Activate
    ws.Select

RowCount = Cells(Cells.Rows.Count, "A").End(xlUp).Row
For i = 1 To RowCount
    Range("L" & i).Select
    If ActiveCell = "NO" Then
        ActiveCell.Range("A").Copy
        Sheets("Sheet2").Select
        RowCount = Cells(Cells.Rows.Count, "A").End(xlUp).Row
        Range("A" & RowCount + 1).Select
        ActiveSheet.Paste
        Sheets("Sheet1").Select
    End If
Next

End Sub

我认为您可以使用自动过滤器而不是循环。

RowCount = Cells(Cells.Rows.Count, "A").End(xlUp).Row
Cells.AutoFilter ' set an filter on the sheet
With Sheets("Sheet1").Range("A1:L" & RowCount) ' filter on NO column L
    .AutoFilter Field:=12, Criteria1:="NO"
End With
Sheets("Sheet1").Range("A2:L" & Range("A2").End(xlDown)).Copy 'Copy the filtered data
Sheets("Sheet2").Select
RowCount = Cells(Cells.Rows.Count, "A").End(xlUp).Row
Range("A" & RowCount + 1).Select
ActiveSheet.Paste

我很想将此问题标记为重复,因为每天都有大量此类复制粘贴数据问题,但是哦。

  1. 不要使用Select/ActiveCell/Activesheet/Activeworkbook/.. period! 这是一个很糟糕的vba-excel做法,可以随时避免。 另外,仅因为您通过RowCount循环并不意味着该单元处于活动状态。 这也可能是您不断收到错误的原因:MSDN定义下的Application.ActiveCell如下:

    返回一个Range对象,该对象表示活动窗口 (顶部窗口)或指定窗口中的活动单元格 如果窗口未显示工作表,则此属性将失败 只读。

    (有关如何避免使用它们的更多信息,请参考 stackoverflow问题)

  2. 您的代码中总共有一些小错误。 我没有要使用的数据,也没有有关哪个工作表的信息,因此我只假定Sheet1包含要复制的数据,Sheet2包含要粘贴的数据

     Private Sub copy_paste() Dim ws_source As Worksheet: Set ws_source = Sheets("Sheet1") Dim ws_target As Worksheet: Set ws_target = Sheets("Sheet2") Dim last_row As Long last_row = ws_source.Cells(ws_source.Rows.Count, "L").End(xlUp).Row Dim next_paste As Long For i = 1 To last_row If ws_source.Cells(i, "L") = "NO" Then ws_source.Rows(i).EntireRow.Copy next_paste = ws_target.Cells(ws_target.Rows.Count, "A").End(xlUp).Row + 1 ws_target.Rows(next_paste).PasteSpecial xlPasteValues End If Next i End Sub 

附带数据: 在此处输入图片说明

预期结果: 在此处输入图片说明

您可以使用FIND 这将找到“ 否”,但找不到“ 否”或“ nO” (更改为MatchCase=False以查找所有情况)。

Public Sub SearchAndCopy()

    Dim wb As Workbook
    Dim ws As Worksheet
    Dim ws1 As Worksheet
    Dim last_row As Long
    Dim rFound As Range
    Dim sFirstAdd As String

    Set wb = ThisWorkbook 'ActiveWorkbook
                          'Workbooks("SomeWorkbook.xlsx")
                          'Workbooks.Open("SomePath/SomeWorkbook.xlsx")

    Set ws = wb.Worksheets("Sheet1")
    Set ws1 = wb.Worksheets("Sheet2")

    With ws.Columns("L")
        Set rFound = .Find(What:="NO", _
                           LookIn:=xlValues, _
                           LookAt:=xlWhole, _
                           SearchDirection:=xlNext, _
                           MatchCase:=True)

        If Not rFound Is Nothing Then
            sFirstAdd = rFound.Address
            Do
                'Find next empty row on destination sheet.
                    'Only really need to give worksheet reference when
                    'counting rows if you have 2003 & 2007+ files open - "ws.Rows.Count"
                last_row = ws1.Cells(ws.Rows.Count, "A").End(xlUp).Row + 1

                'Copy the figure from source to target sheet.
                'You could also use Copy/Paste if you want the formatting as well.
                ws1.Cells(last_row, 1) = ws.Cells(rFound.Row, 1)

                'Look for the next matching value in column L.
                Set rFound = .FindNext(rFound)
            Loop While rFound.Address <> sFirstAdd
        End If
    End With

End Sub  

我在下面添加了对您的代码的解释-您的代码的主要错误是ActiveCell.Range("A").Copy 没有范围A ,但是有A1A2等。

Sub SearchMacro()

    'You didn't declare these two which
    'indicates you haven't got Option Explicit
    'at the top of your module.
    Dim RowCount As Long
    Dim i As Long

    Dim wb As Workbook
    Dim ws As Worksheet

    'I'll only comment that you set
    'wb to be the ActiveWorkbook and you then
    'activate the active workbook which is already active.....
    Set wb = ActiveWorkbook
    Set ws = Sheets("Sheet1")
    wb.Activate
    ws.Select

    'Looks at the active sheet as you just activated it.
    'Generally better to say "the cells in this named worksheet, even if it isn't active, or
    'in the active book... just reference the damn thing."
    'Something like "ws.cells(ws.cells.rows.count,"A").End(xlUp).Row"
    'Note it references the correct worksheet each time.
    RowCount = Cells(Cells.Rows.Count, "A").End(xlUp).Row
    For i = 1 To RowCount
        Range("L" & i).Select
        If ActiveCell = "NO" Then

            'Your code falls over here - you can't have range A.
            'You can have range A1, which is the first cell in your referenced range.
            'So ActiveCell.Range("A1") will return the ActiveCell - "L1" probably.
            ActiveCell.Range("A1").Copy

            'This will copy from column A using your method:
            'ws.Cells(ActiveCell.Row, 1).Copy

            'If you get the above line correct this will all work.
            Sheets("Sheet2").Select
            RowCount = Cells(Cells.Rows.Count, "A").End(xlUp).Row
            Range("A" & RowCount + 1).Select
            ActiveSheet.Paste

            'You've already called it "ws" so just "ws.Select" will work.
            Sheets("Sheet1").Select
        End If
    Next

End Sub

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM