简体   繁体   English

Excel VBA 宏可在列中出现值时在工作表之间复制数据,然后循环到下一个值

[英]Excel VBA Macro to copy data between sheets when value appears in column then loops to next value

I have some experience of writing macros but this area is new to me.我有一些编写宏的经验,但这个领域对我来说是新的。 I have been working on a new project and have most parts of it working but I can't seem to get this part to work.我一直在研究一个新项目,并且它的大部分内容都在工作,但我似乎无法让这部分工作。 I have looked at lookup and match range but none seem to work properly.我已经查看了查找和匹配范围,但似乎都无法正常工作。

  • Sheet "Master" = contains all the data in a large sheet with a number of columns including an ID column.工作表“主”= 包含一个大工作表中的所有数据,其中包含一个 ID 列。
  • Sheet "Projectx" = contains ID column I am searching for.工作表“Projectx”= 包含我正在搜索的 ID 列。

What I am trying to do is this.我正在尝试做的是这个。

  1. Search ID from Column A "Projectx" in "Master" sheet Column A and if it exists then copy the entire row beside the value in "Projectx".从“主”表 A 列中的 A 列“Projectx”中搜索 ID,如果存在,则复制“Projectx”中值旁边的整行。
  2. if copied or no match exist then move on to the next value until the column is blank.如果已复制或不存在匹配项,则移至下一个值,直到该列为空。
  3. this will then be expanded in the future to allow different files to be opened and searched from different suppliers.这将在未来进行扩展,以允许从不同的供应商打开和搜索不同的文件。

Many thanks & I hope this makes sense非常感谢 & 我希望这是有道理的

This was my attempt:这是我的尝试:

Sub Match()

On Error Resume Next  
Dim ID_upn As String  
Dim ID_Row As Long  
Dim ID_Clm As Long  

Table1 = Sheets("Projectx").Range("A2:A200") ' ID Column from Project sheet table  
Table2 = Sheets("Master").Range("A2:P200") ' Master Table  
ID_Row = Sheets("Projectx").Range("A2").row ' C2 is the first cell to start checking  
ID_Clm = Sheets("Master").Range("A2").Column
For Each cl In Table1  
  Sheets("Projectx").Cells(ID_Row, ID_Clm) = Application.WorksheetFunction.if(cl, Table2, 3, False)  
  ID_Row = ID_Row + 1  
Next cl  
MsgBox "Done"  
End SubSub Match()  

Here is how I would do it:这是我将如何做到的:

Sub getFromMaster()
Dim row as Range, wb as Workbook, masterSheet as Worksheet, idSheet as Worksheet
Dim idVal as String, masterRow as Range, masterVal as String
    Set wb = ActiveWorkbook
    Set masterSheet = wb.sheets("Master")
    Set idSheet = wb.sheets("Projectx")
    For each row in idSheet.usedrange.rows
        idVal = row.cells(1).value
        For each masterRow in masterSheet.usedrange.rows
            masterVal = masterRow.cells(1).value
            If masterVal = idVal then
                masterRow.copy row
                Exit For
            End if
        next 'masterRow
    next 'row

End Sub

暂无
暂无

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

相关问题 Excel VBA-如果单元格值满足条件,则在工作表之间复制单元格 - Excel VBA - Copy cells between sheets if cell value satisfies criteria Excel VBA宏,复制行中的第一个值 - Excel VBA Macro, copy the first value in row 根据日期和单元格值在工作表之间复制Excel数据 - Copy Excel data between sheets based on date & cell value Excel VBA宏可基于其他工作表中的值将粘贴值复制到其他工作表 - Excel VBA Macro to copy paste values to other sheets based on value in the other sheet Excel VBA宏以在A列的值为1时添加空白行 - Excel VBA Macro to add a blank row when the value in column A is 1 无法为Excel VBA编写宏以将特定的单元格值复制到下一个空白行 - Unable to write a Macro for Excel VBA to copy a specific cell value to the next blank rows VBA Excel:宏,该宏在一行中搜索特定单元格中的某个变量值,然后将值复制并粘贴到此列中 - VBA Excel : Macro that searches a row for a certain variable value from specific cell and then copy&pastes value to this column Excel VBA:将数据复制到具有值的最后一列 - Excel VBA: Copy the Data up to the last column that has value VBA-Excel-如果值=下一行值,则复制另一个值 - vba - excel - if value = next row value then copy another value Microsoft Excel 2013-VBA宏-插入值并选择同一列(下一行)中的下一个单元格 - Microsoft Excel 2013 - VBA Macro - Insert value and select next cell in same column (row underneath)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM