[英]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.