ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 效率神器,一键搞定繁琐工作
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
让更多数据处理,一键完成 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
12
返回列表 发新帖
楼主: 小NN

[求助] 不打开工作簿提取数据

[复制链接]

TA的精华主题

TA的得分主题

发表于 2024-3-14 22:15 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
贵在参与。。

不打开文件,提取单元格内容.zip

45.94 KB, 下载次数: 14

TA的精华主题

TA的得分主题

 楼主| 发表于 2024-3-15 08:58 | 显示全部楼层
wangyuhui7 发表于 2024-3-14 17:06
Sub ExtractCellContents()
    Dim fso As Object
    Dim folderPath As String

大神,你看我改了下:
Sub ExtractCellContents()
    Dim fso As Object
    Dim folderPath As String
    Dim folder As Object
    Dim file As Object
    Dim wb As Workbook
    Dim ws As Worksheet
    Dim targetSheet As Worksheet
    Dim targetRow, x As Long
    Dim cellContents As String
    Set targetSheet = ThisWorkbook.Worksheets("sheet1")
    targetRow = 2
    folderPath = "G:\文件夹\" '根据需要修改路径
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set folder = fso.GetFolder(folderPath)
    For Each file In folder.Files
        If Right(file.Name, 4) = "xlsx" Or Right(file.Name, 3) = "xls" Then
            Set wb = Workbooks.Open(file.Path, ReadOnly:=True)
            For Each ws In wb.Worksheets
                x = 1
                For Each cell In ws.Range("Z16,AZ49,ZH51")
                    cellContents = cell.Value
                    targetSheet.Cells(targetRow, x).Value = cellContents
                    x = x + 1
                Next cell
                targetSheet.Cells(targetRow, 4).Value = wb.Name
                targetRow = targetRow + 1
            Next ws
            wb.Close SaveChanges:=False
        End If
    Next file
    Set folder = Nothing
    Set file = Nothing
    Set wb = Nothing
    Set ws = Nothing
    Set fso = Nothing
End Sub
改了这句: For Each cell In ws.Range("Z16,AZ49,ZH51")
这句提示下标越界:Set targetSheet = ThisWorkbook.Worksheets("sheet1")

TA的精华主题

TA的得分主题

发表于 2024-3-15 12:03 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
学习留下脚印。

TA的精华主题

TA的得分主题

发表于 2024-3-15 13:27 | 显示全部楼层
  1.   Dim strPath As String
  2. '  With Application.FileDialog(msoFileDialogFolderPicker)
  3. '    .InitialFileName = ThisWorkbook.Path
  4. '    If .Show Then strPath = .SelectedItems(1) Else: Exit Sub
  5. '  End With
  6. '  If Right(strPath, 1) <> "\" Then strPath = strPath & "\"
  7.   strPath = ThisWorkbook.Path & "\文件夹\"
  8.   
  9.   Dim Conn As Object, Cata As Object, tb As Object
  10.   Dim strConn As String, strFile As String, t As String, i As Long
  11.   
  12.   Sheet1.Activate
  13.   ActiveSheet.UsedRange.Offset(1).ClearContents
  14.   
  15.   Application.ScreenUpdating = False
  16.   
  17.   Set Conn = CreateObject("ADODB.Connection")
  18.   Set Cata = CreateObject("ADOX.Catalog")
  19.   
  20.   If Application.Version < 12 Then
  21.     strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties='Excel 8.0;HDR=NO';Data Source="
  22.   Else
  23.     strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties='Excel 12.0;HDR=NO';Data Source="
  24.   End If
  25.   'Conn.Open strConn & ThisWorkbook.FullName
  26.   
  27.   On Error Resume Next   '数据不规范,无奈之举,用上容错……
  28.   strFile = Dir(strPath & "*.xls*")
  29.   While Len(strFile)
  30.     If strPath & strFile <> ThisWorkbook.FullName Then
  31.       Conn.Open strConn & strPath & strFile
  32.       Cata.ActiveConnection = Conn
  33.       For Each tb In Cata.Tables
  34.         If tb.Type = "TABLE" Then
  35.           t = Replace(tb.Name, "'", vbNullString)
  36.           If Right(t, 1) = "$" Then
  37.             i = i + 1
  38.             With Range("A1")
  39.               .Offset(i, 0).CopyFromRecordset Conn.Execute("SELECT * FROM [" & t & "D10:D10]")
  40.               .Offset(i, 1).CopyFromRecordset Conn.Execute("SELECT * FROM [大大]")
  41.               .Offset(i, 2).CopyFromRecordset Conn.Execute("SELECT * FROM [小小]")
  42.               .Offset(i, 3) = Split(strFile, ".xls")(0)
  43.               .Offset(i, 4) = Left(t, Len(t) - 1)
  44.             End With
  45.           End If
  46.           If Err.Number Then Err.Clear
  47.         End If
  48.       Next
  49.       If Conn.State = 1 Then Conn.Close
  50.     End If
  51.     strFile = Dir
  52.   Wend
  53.   
  54.   Set Cata = Nothing
  55.   Set Conn = Nothing
  56.   
  57.   Application.ScreenUpdating = True
  58.   Beep
  59. End Sub
复制代码

评分

1

查看全部评分

您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-9-29 16:29 , Processed in 0.026444 second(s), 7 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表