不打开Excel文件直接读取数据



Sub readData()

'数据读取
'----------------------------------------------
    Dim dataExcel, Workbook, sheet
    Dim totalRow  As Integer
    Set dataExcel = CreateObject("Excel.Application")
    Set Workbook = dataExcel.Workbooks.Open("E:\要读取数据的源文件.xlsx")
    Set sheet = Workbook.Worksheets(1)     '读取第一个sheet页的数据
    totalRow = sheet.UsedRange.Find(what:="*", searchorder:=xlByRows, searchdirection:=xlPrevious).Row
    For i = 2 To totalRow
         
         Sheets("sheet1").Cells(i, 3) = sheet.Cells(i, 4)
        
    Next i
    Workbook.Close
    MsgBox "读取成功!", vbSystemModal '读取完后弹框提醒
'----------------------------------------------
End Sub

你可能感兴趣的:(VB/VBA)