简体   繁体   English

VBA根据条件从另一个工作簿复制粘贴

[英]VBA to copy paste from another workbook based on criteria

I have a total 3 workbooks named wb1, wb2 and wb3. 我总共有3个工作簿,分别名为wb1,wb2和wb3。 I've written a vba program to copy and paste the data to the macro enabled workbook. 我编写了一个vba程序,将数据复制并粘贴到启用了宏的工作簿中。 The problem I have is to refine the copy&paste in such a way that if the data in column B2:B25 in wb1 is between -0.1 to 0.1, then the next successive column C2:C25 and the original B2:B25 values will not be copied. 我的问题是要以这种方式优化复制和粘贴,如果wb1中B2:B25列中的数据介于-0.1到0.1之间,则下一个连续的C2:C25列和原始B2:B25值将不会被复制。 The copy&pasting will continue until all the columns up to AG has been checked 复制和粘贴将继续进行,直到选中了AG之前的所有列

Simliarly, the program will also copy&paste from wb2 & wb3 based on the above criteria. 同样,该程序还将基于上述条件从wb2&wb3复制并粘贴。 The only difference is the paste location which will be A5:AG8 and A40:AG43, respectively. 唯一的区别是粘贴位置分别为A5:AG8和A40:AG43。

Example In wb1: 在wb1中的示例:

A      B   C        D      F
0.09   1   0.0026   0      -0.17
800    2   0.00457  -0.05  -0.15
1600   3.1 0.00345  0.01   -0.1

The program will copy paste all data from column A to C but omits D & F to the excel macro in A13:AG36 which will look like the result below. 该程序会将所有数据从A列复制粘贴到C,但是将D&F忽略到A13:AG36中的excel宏,这将类似于以下结果。

A      B   C        
0.09   1   0.0026   
800    2   0.00457  
1600   3.1 0.00345 

This will continue for next successive columns until AG; 这将在接下来的连续列中继续进行,直到AG; if G2:G25 have all values between -0.1 to 0.1 then both G and H are omitted. 如果G2:G25的所有值都在-0.1到0.1之间,则G和H均被省略。

The program below simply copy and pastes the data to the excel macro workbook without any filter. 下面的程序仅将数据复制并粘贴到excel宏工作簿中,而无需任何过滤器。 How do I make it in a way that it implements the changes above? 如何以实现上述更改的方式进行制作?

Sub TransferTRA015()


Dim strPath2 As String
Dim strPath3 As String
Dim strPath4 As String
Dim wbkWorkbook1 As Workbook
Dim wbkWorkbook2 As Workbook
Dim wbkWorkbook3 As Workbook
Dim wbkWorkbook4 As Workbook

Application.ScreenUpdating = False


strPath2 = "C:\Users\transducer1.CCS\Desktop\LabVIEW Data\TRA015\TRA015_TEST_Room.xlsx"
strPath3 = "C:\Users\transducer1.CCS\Desktop\LabVIEW Data\TRA015\TRA015_TEST_Cold.xlsx"
strPath4 = "C:\Users\transducer1.CCS\Desktop\LabVIEW Data\TRA015\TRA015_TEST_Hot.xlsx"


Set wbkWorkbook1 = ThisWorkbook '### changed this
Set wbkWorkbook2 = Workbooks.Open(strPath2)
Set wbkWorkbook3 = Workbooks.Open(strPath3)
Set wbkWorkbook4 = Workbooks.Open(strPath4)


'### change the sheet and range to what you need
wbkWorkbook1.Worksheets("RAW DATA").Range("A13:AG36").Value = _
    wbkWorkbook2.Worksheets("sheet1").Range("A2:AG25").Value


wbkWorkbook1.Worksheets("RAW DATA").Range("A5:AG8").Value = _
    wbkWorkbook4.Worksheets("sheet1").Range("A2:AG5").Value

wbkWorkbook1.Worksheets("RAW DATA").Range("A40:AG43").Value = _
    wbkWorkbook3.Worksheets("sheet1").Range("A2:AG5").Value


wbkWorkbook2.Close (True)
wbkWorkbook3.Close (True)
wbkWorkbook4.Close (True)

Application.ScreenUpdating = False

End Sub

Here are two ways to use WorksheetFunctions to test if a range meets your conditions. 这是使用WorksheetFunctions来测试范围是否满足您的条件的两种方法。

VBA WorksheetFunction.Min and WorksheetFunction.Max VBA WorksheetFunction.Min和WorksheetFunction.Max

With wbkWorkbook1.Worksheets("RAW DATA")

    If WorksheetFunction.Min(.Range("B2:B25")) < -0.1 Or WorksheetFunction.Min(.Range("B2:B25")) < -0.1 Then

    End If

End With

Worksheet.Evaluate to return the value of a formula that tests ranges of that worksheet. Worksheet.Evaluate求值以返回测试该工作表范围的公式的值。

If wbkWorkbook1.Worksheets("RAW DATA").Evaluate("OR(MIN(B2:B25)>=-0.1,MAX(B2:B25)<=0.1)") Then

End If

暂无
暂无

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

相关问题 VBA根据某些用户输入的标准将数据从一个工作簿复制并粘贴到另一个工作簿? - VBA copy and paste data based on certain user inputed criteria from one workbook to another? VBA从一个工作簿复制并粘贴到另一个工作簿 - VBA to copy from one workbook and paste to another VBA - 根据汇总 Excel 表格上的条件,将工作簿中的不同模板工作表复制到另一个工作簿的多张工作表中 - VBA - copy different template sheets from a workbook, into multiple sheets of another workbook based on criteria on a summary excel sheet Excel VBA:从另一个工作簿复制行并粘贴到主工作簿 - Excel VBA: Copy Row from another workbook and paste into master workbook 我想根据 vba 中的某些条件将一个工作簿中的一些行粘贴到另一个工作簿 - I want to paste some rows from one workbook to another based on some criteria in vba 使用VBA根据条件将数据复制到另一个Excel工作簿 - Copy Data to Another Excel WorkBook Based on Criteria Using VBA 如何使用 VBA 将基于某些条件的粘贴数据从一个工作簿复制到另一个工作簿(特定单元格)? - How to copy paste data based on some criterias from one workbook to another(specific cells) using VBA? Excel VBA 可根据文本将整行从 1 个工作表复制并粘贴到同一工作簿中的另一个工作表 - Excel VBA to copy and paste an entire row from 1 worksheet to another in the same workbook based on a text 如果满足条件,复制并粘贴到另一个工作簿 - Copy and paste to another workbook if criteria is met 复制,粘贴基于多个条件的选择到VBA中的另一个工作表 - copy, paste selection based on multiple criteria to another worksheet in VBA
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM