[英]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.
在这个工作表(让它是rawdata)中有列info3,它对应于天线名称和频率列以及其他列。 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.
我需要一个代码来检查info3和频率(workbook rawdata)中的值/文本是否与天线名称和频率(工作簿触角)中的值/文本相同,然后复制参数(在灰色列中)它对应于其他工作簿中相应列(标记为红色)的天线名称和频率。 The columns are not sequentially and the column frequency in workbook rawdata are variable.
列不是顺序的,工作簿rawdata中的列频率是可变的。 It will be good to check the frequency only the first symbol -if is 9 (900), or 1 (1800), or 2 (2100).
最好只检查第一个符号的频率 - 如果是9(900),或1(1800),或2(2100)。 I have no idea how to do it.... I appreciate the help with this.
我不知道该怎么做....我很感激这方面的帮助。
https://imgur.com/Vq6V95E https://imgur.com/psT9Z1h 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.