简体   繁体   English

在工作簿中查找匹配的字符串,从不同的工作表中复制整行并替换原始行 - Excel VBA

[英]Find matching string in workbook, copy entire row from different worksheet and replace original row - Excel VBA

I'm looking to loop through the entire workbook and find matching strings from "Sheet1, ColumnA" in "Sheet3, ColumnA" and copy the entire row from "Sheet3, ColumnB" back to matching location in "Sheet1".我希望遍历整个工作簿并从“Sheet3,ColumnA”中的“Sheet1,ColumnA”中找到匹配的字符串,并将整行从“Sheet3,ColumnB”复制回“Sheet1”中的匹配位置。 "Sheet1" would be used as an organized view and "Sheet3" would be used as an input sheet. “Sheet1”将用作有组织的视图,“Sheet3”将用作输入表。 The idea is to copy exported values into "Sheet3" to have "Sheet1" automatically update without the need of manual copy/paste.这个想法是将导出的值复制到“Sheet3”,让“Sheet1”自动更新,无需手动复制/粘贴。

Sheet1 Sheet1 Example Sheet1 Sheet1 示例

Sheet3 Sheet3 Example Sheet3 Sheet3 示例

Updated Sheet1 Updated Sheet1 Example更新 Sheet1更新 Sheet1 示例

I'm new to VBA but was able to create a check for matching values and making the matches "Bold".我是 VBA 的新手,但能够创建匹配值检查并将匹配设置为“粗体”。 Maybe I just need some modifications?也许我只需要一些修改?

Sub HighlightMatches()
    Application.ScreenUpdating = False
    
    'Declare variables
    Dim var As Variant, iSheet As Integer, iRow As Long, iRowL As Long, bln As Boolean
       
       'Set up the count as the number of filled rows in the first column of Sheet1.
       iRowL = Cells(Rows.Count, 1).End(xlUp).Row
       
       'Cycle through all the cells in that column:
       For iRow = 1 To iRowL
          'For every cell that is not empty, search through the first column in each worksheet in the
          'workbook for a value that matches that cell value.

          If Not IsEmpty(Cells(iRow, 1)) Then
             For iSheet = ActiveSheet.Index + 1 To Worksheets.Count
                bln = False
                var = Application.Match(Cells(iRow, 1).Value, Worksheets(iSheet).Columns(1), 0)
                
                'If you find a matching value, indicate success by setting bln to true and exit the loop;
                'otherwise, continue searching until you reach the end of the workbook.
                If Not IsError(var) Then
                   bln = True
                   Exit For
                End If
             Next iSheet
          End If
          
          'If you do not find a matching value, do not bold the value in the original list;
          'if you do find a value, bold it.
          If bln = False Then
             Cells(iRow, 1).Font.Bold = False
             Else
             Cells(iRow, 1).Font.Bold = True
          End If
       Next iRow
    Application.ScreenUpdating = True
End Sub

Have modified your code to do what I understand it is you want to do (see below).已修改您的代码以执行我理解的您想要执行的操作(见下文)。
Have also made other changes and provided reasoning in the comments.还进行了其他更改并在评论中提供了推理。
Hope this answers your question, as well and providing other useful information.希望这也能回答您的问题,并提供其他有用的信息。

Sub HighlightMatches()
    
''' Declare variables:
''' o It's good practice to specify (in you own parlance) the data type in the variable name
''' o  Some use a one character, I prefer two characters.
''' o  Your Dim here uses one character, but incorrectly uses 'i' (used for integer) for longs
    ' Dim var As Variant, iSheet As Integer, iRow As Long, iRowL As Long, bln As Boolean
    
''' Also, you can specify data types using a symbol 'as short-hand' for the common data types
''' o  Variants don't need 'as Variant' and you can use: % for integer, # for double, & for long, $ for string.
''' I also tend to abbreviate (what I consider self-evident) terms (e.g. Row to R, Last Row to LR)

''' All up, I'd suggest the following:
    Dim inSh%, lgR&, lgLR&, blFlag As Boolean, vnVal, vnMatchedR
       
''' Speed things up with screen and calculation off
    With Application: .Calculation = xlCalculationManual: .ScreenUpdating = False: End With
       
''' Get last data row (last row in the active sheet, with any data in column 1)
    lgLR = Cells(Rows.Count, 1).End(xlUp).Row
    
''' Cycle through the cells in that column:
''' o  I changed this to ignore the headings. Revert to 1 if you want to copy headings as well:
    For lgR = 2 To lgLR: vnVal = Cells(lgR, 1)
       
       'For every cell that is not empty, search through the first column in each worksheet in the
       'workbook for a value that matches that cell value.

    ''' Empty cell values (nulls) = a zero-length string
        If vnVal <> "" Then
            For inSh = ActiveSheet.Index + 1 To Worksheets.Count
            With Worksheets(inSh)
                vnMatchedR = Application.Match(vnVal, .Columns(1), 0)
                
            ''' If matched: Copy the row from the activesheet to the matched row
                If Not IsError(vnMatchedR) Then
                    
                ''' Here I am transfering the entire row's values only (not row heights, formats, etc.)
                    .Rows(vnMatchedR).EntireRow.Value = ActiveSheet.Cells(lgR, 1).EntireRow.Value

                ''' If your text is taken literally (i.e. copy the row),
                ''' then you want this instead of the above:
                    'ActiveSheet.Cells(lgR, 1).EntireRow.Copy
                    '.Rows(vnMatchedR).EntireRow.PasteSpecial xlPasteAll
                End If
            End With
          
          Next inSh
       End If
    
    Next lgR
    
''' Reinstate screen and calculation
    With Application: .Calculation = xlCalculationAutomatic: .ScreenUpdating = False: End With
End Sub

暂无
暂无

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

相关问题 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 Excel VBA-如何使用不同的子例程将行从工作簿的行复制到另一个工作簿的工作表 - Excel VBA - How to copy row by row from a sheet of workbook to another Workbook's sheet using different subroutine 如果在 A 列中找不到匹配项,请使用 VBA 将整行从一个 excel 工作表复制到另一个 - Use VBA to copy entire row from one excel worksheet to another if match is not found in Column A Excel VBA:从另一个工作簿复制行并粘贴到主工作簿 - Excel VBA: Copy Row from another workbook and paste into master workbook VBA复制工作表中的行,如果符合条件,则将其粘贴到其他工作表中 - VBA to copy row from worksheet and paste it into a different worksheet if criteria is met Excel VBA 将工作表名称添加到从整行创建的数组中 - Excel VBA add worksheet name to array created from entire row 查找Excel工作簿的工作表名称和行号 - Find Worksheet Name and Row Number for an Excel Workbook 复制整行的值并将其过去到另一个工作表中 - Copy value of entire row and past it into a different worksheet 优秀 在不同的工作表中查找行 - excel | find row in different worksheet 在特定行的整个工作簿中查找和替换 - Find & Replace across entire workbook in specific row
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM