码迷,mamicode.com
首页 > 编程语言 > 详细

VBA 练习-从两个库中调用数据到活动表中

时间:2016-08-11 22:23:04      阅读:139      评论:0      收藏:0      [点我收藏+]

标签:

 

练习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

 

VBA 练习-从两个库中调用数据到活动表中

标签:

原文地址:http://www.cnblogs.com/lunawzh/p/5762535.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!