标签:
练习VBA
Sub 入库() Dim basedb As String, cpdb As String, wb As Workbook, ws As Worksheet, curWs As Worksheet basedb = ThisWorkbook.Path & "\分析数据.xlsx" cpdb = ThisWorkbook.Path & "\成品抽检信息.xlsx" ‘分析数据库中提取 If Len(Dir(basedb)) = 0 Then MsgBox "找不到文件:" & vbCrLf & basedb, vbExclamation, "错误" Else Set wb = GetObject(basedb) Set curWs = ActiveSheet ‘当前活动表的行数 curLastRow = curWs.Range("d6556").End(xlUp).Row For i = 4 To curLastRow ‘产品批号 v = Range("d" & i) For Each ws In wb.Worksheets ‘分析库中的行数 wslastrow = ws.Range("a65536").End(xlUp).Row For j = 4 To wslastrow If ws.Range("a" & j) = v Then curWs.Range("O" & i & ":U" & i) = ws.Range("C" & j & ":I" & j).Value curWs.Range("X" & i) = ws.Range("N" & j) curWs.Range("G" & i) = Range("X" & i) & Range("Y" & i) Exit For End If Next Next Next wb.Close End If ‘成品抽检信息中提取 If Len(Dir(cpdb)) = 0 Then MsgBox "找不到文件:" & vbCrLf & cpdb, vbExclamation, "错误" Else Set wb = GetObject(cpdb) Set curWs = ActiveSheet curLastRow = curWs.Range("d6556").End(xlUp).Row For i = 4 To curLastRow v = Range("d" & i) For Each ws In wb.Worksheets wslastrow = ws.Range("a65536").End(xlUp).Row For j = 3 To wslastrow If ws.Range("a" & j) = v Then curWs.Range("V" & i) = ws.Range("Q" & j) Exit For End If Next Next Next wb.Close End If ‘关闭工具库 Windows("工具库.xlsm").Activate ActiveWindow.Close False End Sub
标签:
原文地址:http://www.cnblogs.com/lunawzh/p/5762535.html