VBA复制条件和粘贴标准

时间:2017-12-18 20:23:01

标签: excel vba excel-vba

我认为我最大的问题是我不知道如何询问我在寻找什么。

我正在尝试使用条件在(工作簿A)的列中查找信息。例如,我需要查找一个名为“Site X”的站点名称。如果第2列符合此标准,我想选择一系列单元格并复制。

一旦我选择了要激活的单元格范围(工作簿b),这些单元格已经在我的宏程序中打开,请选择正确的工作表,在特定列中找到“Site x”,比较列中的日期在(工作簿A)和(工作簿b)中,然后将复制的数据放在现有数据源中的正确位置。

我发现有很多代码可以解决我问题的第一部分,但是,粘贴标准让我很难过。我的道歉如果在这个论坛中已经回答了这个问题。如果能够,请指导我。

以下是我尝试使用的一些代码。

Sub Part3Transfer1()

Dim LastRow As Integer, i As Integer, erow As Integer

LastRow = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row

For i = 2 To LastRow

If Cells(i, 2) = "Site x" Then
Range(Cells(i, 5), Cells(i, 17)).Select
Selection.Copy

Workbooks("InstanceOnePartTwo.xlsb").Activate
Worksheets("sheet1").Activate

If Cells(i, 2) = "Site X" Then
Range(Cells(i, 68), Cells(i, 81)).Select
Selection.Paste

Next i
End If
End Sub

1 个答案:

答案 0 :(得分:0)

嗯,这不是一个答案,但我想我会给你一些指示,我已经尝试用可能对你有帮助的一些事情评论代码,......(我可能会得到纠正)这些,但我会欢迎更多的指示),你一定要查看语句,以澄清你正在使用的工作簿和工作表:

Sub Part3Transfer1()
Dim LastRow As Long
Dim i As Long
'these are declared as Long because there are more cells in Excel than there are Integer values.
LastRow = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
'*** LastRow = Sheet1.Cells(Sheet1.Rows.Count, "A").End(xlUp).Row ***'
'To find the last row, its usually better to declare which Sheet by either
'their code name (ie. Sheet1 or Sheet2) or by declaring them by name (such as, Sheets("Sheet1"))

For i = 2 To LastRow
    If Cells(i, 2) = "Site x" Then ' better to declare the Sheet and workbook when refering to Cells as you are moving things from workbook to workbook
                                    ' like Sheet1.Cells or Sheets("Sheet1").Cells
        Range(Cells(i, 5), Cells(i, 17)).Copy 'its considerably faster not to use Select
        'Sheet1.Range(Cells(i, 5), Cells(i, 17)).Copy
    End If

    Workbooks("InstanceOnePartTwo.xlsb").Activate
    Worksheets("Sheet1").Activate
'by activating the workbook and sheet above, it assumes that the file are already open,
'so if they is isn't open I'm sure you'll see an error, but you could change the active for an alternative
'to actually open the workbook for you?

    If Cells(i, 2) = "Site X" Then 'You should declare the workbook and Sheet when refering to Cells so it doesn't unintentionally pick the cells from the ActiveSheet
    'something like before Worksheets("Sheet1").Cells(i, 68), Worksheets("Sheet1").Cells(i,80))

        Range(Cells(i, 68), Cells(i, 81)).PasteSpecial xlPasteValues
        'use PasteSpecial with xlPasteValues to paste after your copy

        'the range of your copy statement selects a smaller range than the range you are pasting into
        'so you won't get an error, but I'm pretty sure they should be the same size (ie. from column 5 to 17 to copy
        'columns 68 to 81,...?

        'Consider using With statements, instead of this, but this will 
        'show you how to refer to a cell in a specific workbook:
        'Workbooks("MySpreadsheet.xlsx").Worksheets("Sheet1").Range("A1").Value = "Hello"

    End If
Next i
End Sub
相关问题