【发布时间】:2016-10-18 09:50:46
【问题描述】:
我正在寻找 Excel 的 VBA 代码,它将遍历一个工作表中的单列数字(条形码),在另一个工作表(同一工作簿)上查找每个数字(条形码)的完全匹配,然后复制输入搜索词编号(条形码)旁边的列中原始工作表的整行。
我找到了此代码,但它没有遍历工作表(搜索词)中的数字(条形码)列。搜索范围应该是包含所有数据的整个工作表。
Sub Copy()
Dim objWorksheet As Worksheet
Dim rngBurnDown As Range
Dim rngCell As Range
Dim strPasteToSheet As String
'Used for the new worksheet we are pasting into
Dim objNewSheet As Worksheet
Dim rngNextAvailbleRow As Range
'Define the worksheet with our data
Set objWorksheet = ActiveWorkbook.Sheets("Burn Down")
'Dynamically define the range to the last cell.
'This doesn't include and error handling e.g. null cells
'If we are not starting in A1, then change as appropriate
Set rngBurnDown = objWorksheet.Range("A3:A" & objWorksheet.Cells(Rows.Count, "A").End(xlUp).Row)
'Now loop through all the cells in the range
For Each rngCell In rngBurnDown.Cells
objWorksheet.Select
If rngCell.Value <> "" Then
'select the entire row
rngCell.EntireRow.Select
'copy the selection
Selection.Copy
'Now identify and select the new sheet to paste into
Set objNewSheet = ActiveWorkbook.Sheets("Burn Down " & rngCell.Value)
objNewSheet.Select
'Looking at your initial question, I believe you are trying to find the next available row
Set rngNextAvailbleRow = objNewSheet.Range("A1:A" & objNewSheet.Cells(Rows.Count, "A").End(xlUp).Row)
'MsgBox "Success"
objNewSheet.Range("A" & rngNextAvailbleRow.Rows.Count + 1).Select
ActiveSheet.Paste
End If
Next rngCell
objWorksheet.Select
objWorksheet.Cells(1, 1).Select
End Sub
【问题讨论】:
-
需要再次编辑帖子 - 将所有代码包装在代码标签中
-
found this code but it doesn't loop所以研究代码并相应地修改它XD -
为什么不简单地使用 vlookup 公式?看起来你把事情复杂化了。
-
vlookup 只返回一个值...我需要复制整行,除非当然有办法用 vlookup 复制整行?
-
@KevinMcDermott 你想要一行中的所有值在一列中吗?有固定数量的列吗?