在Excel上访问特定数据而不使用ADO打开它

时间:2016-03-21 11:43:38

标签: excel vba excel-vba ado

我正在尝试从另一个excel工作簿中提取一些数据。我正在通过逐个打开和关闭excel工作簿来执行下面给出的vba代码。但是这个过程花费了太多时间,所以我试图学习一种在不打开excel工作簿的情况下获取相同数据的方法。我可以使用ADO连接吗?

Sub GetOldNumber()

Dim adres As String
Dim m As String
Dim found As Range
Dim count As Integer
Dim ws As Worksheet
Dim sht As Worksheet
Set sht = Worksheets("Sayfa1")


For i = 1 To 10
    With sht

    adres = "C:\users\ali\Desktop" & "\" & .Cells(i, 10) & "\" _
    & .Cells(i, 11) & ".xlsx"

    Workbooks.Open (adres)
    m = .Cells(i, 4).Value

    End With



count = 0

For Each ws In ActiveWorkbook.Worksheets

    Set found = ws.Cells.Find(What:=m, LookIn:=xlValues, lookat:=xlWhole)
        If Not found Is Nothing Then
            sht.Cells(i, 12) = found.Cells.Offset(-1, 0)
            count = count + 1
        End If
    Next ws
If count = 0 Then MsgBox "No Matches Found"

ActiveWorkbook.Close
Next



End Sub

0 个答案:

没有答案