|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
- Private Sub CommandButton1_Click()
- Dim sht, sht1 As Worksheet, d As Object, i, n, m, x, y As Integer, arr, brr, var As Variant
- Set sht = ThisWorkbook.Worksheets(1) '设定sht为表一
- Set sht1 = ThisWorkbook.Worksheets(2) '设定sht1为表二
- Set d = CreateObject("scripting.dictionary") '设定d为字典
- y = Day(Now()) '设定y为当日日数
- arr = sht.Range("a2:a" & sht.Cells(Rows.Count, 1).End(xlUp).Row) '将表一名称列的内容写入数组arr
- brr = sht1.Range("a2:a" & sht1.Cells(Rows.Count, 1).End(xlUp).Row) '将表二名称列内容写入数组brr
- For i = 1 To UBound(brr) '遍历数组brr
- d(brr(i, 1)) = "" '将数组brr的内容写入字典
- Next
- For n = 1 To UBound(arr) '遍历数组arr
- If Not d.exists(arr(n, 1)) Then '假如字典中不存在数组arr中的某个项
- sht1.Cells(sht1.Cells(Rows.Count, 1).End(xlUp).Row + 1, 1).Value = arr(n, 1) '在表二现有名称的下一行写入符合条件的值
- End If
- Next
- For m = 2 To sht1.Cells(Rows.Count, 1).End(xlUp).Row '遍历表二名称项
- For x = 2 To sht.Cells(Rows.Count, 1).End(xlUp).Row '遍历表一名称项
- If sht1.Cells(m, 1).Value = sht.Cells(x, 1).Value Then '假如表二名称等于表一的名称
- var = sht.Cells(x, 2).Value + sht1.Cells(m, y + 1).Value '将表一与表二对应列的结果相加得到和
- sht1.Cells(m, y + 1).Value = var
- End If
- Next
- Next
- '录入完后清除表一数据
- sht.Range("a2:c65536").Clear
- End Sub
复制代码 |
|