簡體   English   中英

VBA復制條件和粘貼條件

[英]VBA Copy criteria and Paste Criteria

我認為我最大的問題是我不知道該怎么問我要尋找的東西。

我正在嘗試使用條件在(工作簿A)中的列中查找信息。 例如,我需要查找一個站點名稱,我們將其稱為“站點X”。 如果第2列符合此條件,我想選擇一個單元格區域並復制。

一旦我選擇了要激活的單元格范圍(工作簿b),該范圍將在宏過程中打開,請選擇正確的工作表,在特定列中找到“站點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

好吧,這並不是一個確切的答案,但我想我會給您一些指針,我嘗試了一些可能對您有幫助的事情來注釋代碼,...(我可能對此有所糾正,但我歡迎您提出一些建議。更多指針),則您絕對應該使用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