简体   繁体   English

在VBA中使用数组复制和粘贴

[英]Copy and Paste using array in VBA

I am trying to create a relatively simple copy and paste procedure. 我正在尝试创建一个相对简单的复制和粘贴过程。 I have certain cell addresses stored in an array (Results1()) and I am trying to extrapolate the row which it is in to copy an entire row of data into another Excel sheet. 我将某些单元格地址存储在数组中(Results1()),并且我正在尝试推断其所在的行,以将整个数据行复制到另一个Excel工作表中。 I have the following so far and currently I am getting an object required error when I try to define what my 'NextRow' is. 到目前为止,我有以下内容,并且当我尝试定义“ NextRow”是什么时,当前出现对象必需错误。 Any advice or feedback would be greatly appreciated! 任何建议或反馈将不胜感激!

For i1 = LBound(Results1) To UBound(Results1)
    Set NextRow = Worksheets("searchresult").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
                Range(Results(i1)).EntireRow.Copy NextRow
Next i1

Editted code 修改后的代码

    For i1 = LBound(Results1) To UBound(Results1)
        Worksheets("properties").Select
        Set p1results = Range(Results1(i1))
        p1results.EntireRow.Copy
            With Worksheets("SearchResult").Select
                NextRow = Range("D65536").End(xlUp).Row + 1
                Cells(NextRow, 1).Select
                ActiveSheet.Paste
            End With
    Next i1

Update: In actuality my data ranges only range from columns D:P so when I count rows I count from column D (4) because columns AC are empty ranges. 更新:实际上我的数据范围仅在D:P列范围内,因此当我对行进行计数时,我从D列(4)开始计数,因为AC列为空范围。 Would this affect the way it is pasting my data? 这会影响其粘贴数据的方式吗?

       On Error Resume Next
   For i1 = LBound(Results1) To UBound(Results1)
        Set NextRow = shSearchResult.Cells(shSearchResult.Rows.Count, 4).End(xlUp).Offset(1, 0)
        shProperties.Range(Results1(i1)).EntireRow.Copy NextRow
        If Err.Number <> 0 Then
            Err.Clear
            MsgBox "Bad address" & Results1(i1)
        End If
    Next i1

A few potential causes here: 这里有一些潜在原因:

Your for loop references Results1 but your copy references Results (typo in question? or route cause?) 您的for循环引用了Results1,但是您的副本引用了Results (有问题的打字错误?还是路由原因?)

In your Set NextRow = line references .Cells(Rows.Count, unqualified, Rows refers to the active sheet not "searchresult" sheet. Probably not what you intended, but also will meke no difference in this case. 在您的Set NextRow =行引用中.Cells(Rows.Count,不合格,行是指活动工作表,而不是"searchresult"表。可能不是您想要的,但在这种情况下也不会有任何区别。

Likewise Range(Results(i1)) refers to the active sheet, may be ok - depends on the wider context of your application 同样, Range(Results(i1))指向活动工作表,可能还可以-取决于应用程序的更广泛的上下文

I suspect that your imediate problem is that NextRow has not been DIM 'd. 我怀疑您的直接问题是NextRow尚未DIM Try adding 尝试添加

Dim NextRow as Range to your Sub Dim NextRow as Range您的Dim NextRow as Range

In general it is good practice to use Option Explicit (first line in your module). 通常,良好的做法是使用Option Explicit (模块的第一行)。 This forces explicit declaration of all variable. 这将强制所有变量的显式声明。

EDIT 编辑

based on your edit a "Method 'Range' of object '_Global' failed" error may be caused by an invalid adress string 根据您的编辑, "Method 'Range' of object '_Global' failed"错误可能是由无效的地址字符串引起的

Your first code was actually better than your second. 您的第一个代码实际上比第二个更好。 Here's a refactor version 这是重构版本

Sub zxx()
    Dim Results1(0 To 1) As Variant
    Dim i1 As Long
    Dim NextRow As Range
    Dim shProperties As Worksheet
    Dim shSearchResults As Worksheet


    '  other code ...

    Set shSearchResults = ActiveWorkbook.Worksheets("searchresult")
    Set shProperties = ActiveWorkbook.Worksheets("properties")

    On Error Resume Next
    For i1 = LBound(Results1) To UBound(Results1)
        Set NextRow = shSearchResults.Cells(shSearchResults.Rows.Count, 1).End(xlUp).Offset(1, 0)
        shProperties.Range(Results1(i1)).EntireRow.Copy NextRow
    Next i1


End Sub

To detect bad addresses try adding On Error Resume Next before the For and 要检测不良地址,请尝试在For和For之前添加On Error Resume Next

        If Err.Number <> 0 Then
            Err.Clear
            MsgBox "Bad Address " & Results1(i1)
        End If

after the copy (inside the for loop) 复制之后(在for循环内)

EDIT2 编辑2

Copying an EntireRow into a single cell only works if the single cell is in column A because because the EntireRow range overhangs the right hand side of the sheet. 仅当单个单元格位于列A中时,才将EntireRow复制到单个单元格中,因为这是因为EntireRow范围悬于工作表的右侧。

Use this to offset back to column A 使用此偏移回A列

Set NextRow = _
    shSearchResults.Cells(shSearchResults.Rows.Count, 4).End(xlUp).Offset(1, -3)

You never use the With Statement, so just do this instead: 您永远不会使用With语句,因此只需执行以下操作:

For i1 = LBound(Results1) To UBound(Results1)
    Worksheets("properties").Select
    Set p1results = Range(Results1(i1))
    p1results.EntireRow.Copy
    Worksheets("SearchResult").Select
    NextRow = Range("D65536").End(xlUp).Row + 1
    Cells(NextRow, 1).Select
    ActiveSheet.Paste           
Next i1

or better: 或更好:

For i1 = LBound(Results1) To UBound(Results1)
    Worksheets("properties").Range(Results1(i1)).EntireRow.Copy
    Worksheets("SearchResult").Cells(Range("D65536").End(xlUp).Row + 1, 1).Paste
Next i1

Depending on what you're doing, you might want to use PasteSpecial instead. 根据您正在执行的操作,您可能需要改用PasteSpecial。

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

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