简体   繁体   English

VBA复制条件和粘贴条件

[英]VBA Copy criteria and Paste Criteria

I think my biggest problem here is that I don't know exactly how to ask what I am looking for. 我认为我最大的问题是我不知道该怎么问我要寻找的东西。

I am trying to look up information in a column in(workbook A) using criteria. 我正在尝试使用条件在(工作簿A)中的列中查找信息。 For instance, I need to look up a site name which we will call "Site X". 例如,我需要查找一个站点名称,我们将其称为“站点X”。 If column 2 meets this criterion I want to select a range of cells and copy. 如果第2列符合此条件,我想选择一个单元格区域并复制。

Once I have selected the range of cells I want to activate (workbook b) which will already be open in my macro process, select the correct sheet, find "Site x" in a specific column, compare the date in a column in (workbook A) and (workbook b), then place the copied data in the correct location in the existing data source. 一旦我选择了要激活的单元格范围(工作簿b),该范围将在宏过程中打开,请选择正确的工作表,在特定列中找到“站点x”,比较(工作簿中)一列中的日期A)和(工作簿b),然后将复制的数据放在现有数据源中的正确位置。

I have found plenty of code that can do the first part of my problem, however, the paste criterion has me stumped. 我发现很多代码可以解决问题的第一部分,但是粘贴标准使我感到困惑。 My apologies If this is already answered in this forum. 我很抱歉,如果这个论坛已经回答了。 Please direct me to a link if able. 如果可以的话,请将我定向到链接。

Here is some of the code I am attempting to use. 这是我尝试使用的一些代码。

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

Well this is not exactly an answer, but I thought I'd give you some pointers, I've tried commenting the code with some things that may help you,... (I may get corrected on these, but I would welcome some more pointers), you should definitely look into With Statements to clarify which workbook and worksheets you are working with: 好吧,这并不是一个确切的答案,但我想我会给您一些指针,我尝试了一些可能对您有帮助的事情来注释代码,...(我可能对此有所纠正,但我欢迎您提出一些建议。更多指针),则您绝对应该使用With语句来澄清您正在使用的工作簿和工作表:

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

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

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