简体   繁体   中英

How to copy cells from one workbook to another based on multiple criteria

I will try to explain my problem as clear as possible. There is a workbook (lets named it antennalist) with one sheet. This sheet contains a list of antennas - column antenna names, column frequency and columns with parameters of the antennas. The other workbook contains also one worksheet. In this worksheet (let it be rawdata) have columns info3 which corresponds to antenna names and columns with frequency and other columns too. I need a code which will check if the value/text in info3 and frequency (workbook rawdata) is the same as value/text in antenna name and frequency (workbook antennalist), then to copy the parameters (which are in the gray columns) which corresponds to the antenna name and frequency to the corresponding columns (marked in red) in the other workbook. The columns are not sequentially and the column frequency in workbook rawdata are variable. It will be good to check the frequency only the first symbol -if is 9 (900), or 1 (1800), or 2 (2100). I have no idea how to do it.... I appreciate the help with this.

https://imgur.com/Vq6V95E https://imgur.com/psT9Z1h

It's NOT the code BUT some guidelines:

Option Explicit

Sub test()

    Dim wb1 As Workbook, wb2 As Workbook
    Dim LastRow1, LastRow2 As Long
    Dim rng1 As Range, rng2 As Range, Position As Range, cell As Range

    'Set the two workbooks. Keep in mind that code will works only if you have both workbooks open
    Set wb1 = Workbooks("antennalist.xls")
    Set wb2 = Workbooks("rawdata.xls")

    'Let us assume that data appears in Sheet1 & Column A in both workbooks. Find the last row of column A in both Sheets to create our ranges
    With wb1.Worksheets("Sheet1")
        LastRow1 = .Cells(.Rows.Count, "A").End(xlUp).Row
        Set rng1 = .Range("A" & LastRow1)
    End With
    With wb2.Worksheets("Sheet1")
        LastRow2 = .Cells(.Rows.Count, "A").End(xlUp).Row
        Set rng2 = .Range("A" & LastRow2)
    End With

    'Loop rawdata workbook,Sheet 1 & column A and check:
    ' 1.If the values of rawdata workbook,Sheet 1 & column A appears also in antennalist workbook,Sheet 1 & column A
    ' 2.If the value next to them match also (this is for testing purposes so you must change)
    For Each cell In rng2
        If Application.WorksheetFunction.CountIf(rng1, cell.Value) > 0 And cell.Offset(0, 1).Value = .Range("B" & rng1.Row).Value Then
        'If condition met copy from rawdata & paste to antennalist
            wb2.Worksheets("Sheet1").Range("A1:A2").Copy wb1.Worksheets("Sheet1").Range("A1:A2")
        End If
    Next cell

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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