【发布时间】:2019-10-30 11:12:59
【问题描述】:
我在 Excel vba 上玩得很开心(很糟糕)。
我在工作簿中有两张工作表 - sheet1 和 sheet2。
表 1
AccountNo Account Name
110101 Imprest
110102 abs
110104 abs - Call
110105 abc-MANAGED
110109 bda - Dollar
110201 jhk - Dollar
表 2
AccountNo Account Name
110101 Imprest
110102 abs
110103 bas
110104 abs - Call
110105 abc-MANAGED
110109 bda - Dollar
110201 jhk - Dollar
我想比较 AccountNo 列的行以找出已添加到 sheet2 但不在 sheet1 中的新 accountNo。 如果找到AccountNO,我想将它插入到sheet1中,它必须插入到sheet1中的最佳位置,因为帐号是按顺序排列的。 示例是 110103 accountNO,我想在 sheet1 中的 110102 account no 之后插入整行。
Dim rngCell As Range
Dim matchRow
For Each rngCell In Worksheets("Sheet2").Range("A2:A200")
If WorksheetFunction.CountIf(Worksheets("Sheet1").Range("A2:A200"), rngCell) = 0 Then
' Range("K" & Rows.Count).End(xlUp).Offset(1) = rngCell
matchRow = rngCell.Row
Rows(matchRow & ":" & matchRow).Select
rngCell.EntireRow.Copy
Range("K" & Rows.Count).PasteSpecial xlPasteValues
End If
Next
代码似乎没有帮助。只能识别新的accountNO。
【问题讨论】:
-
最后一行是做什么的?为什么不直接在表格 1 的底部插入新行,然后在末尾按 a/c no 排序?
-
如果工作表 2 总是比工作表 1 多或相同,只需复制前者?
-
听起来你让这变得比必要的更困难 - 如果 sheet2 的行数比 sheet1 的多,请复制工作表 2 中的 who 列并替换工作表 1 中的列 1。
-
sheet1 链接到其他一些工作表,需要这些值的位置。(我不知道如何通过打字来表达)。 sheet1 通常有一些旧的帐号,它们在 sheet2 中不再需要,但在其他一些工作表中需要。
标签: excel vba excel-2010