|
aman1516 发表于 2015-3-28 23:06
谢谢老师,
第二个问题对同格式的WORD修改代码可获取数据了;
第一个问题这种情况,请教下如何识别处理 ...
多行单元格容错代码:
1. 编号行,第四列以后出现多行单元格,可以正确读出所需要值;
2 .编号行,第二/三列出现多行单元格,则该编号行只能读出多行单元格的第一行值;
3. 上面第二个问题,由于项目名称以及表格格式差异较大,必须另写代码.
4. 关于遍历各子文件夹.论坛较多论述,自己去研究.(早晚得过这一关)- Sub test()
- Dim arr(1 To 3000, 1 To 9), ar, fso, fp, n%, subp, f, i%, s$, k$, k1$, br, cr, m%
- ar = [{1,3;2,3;3,3;1,5;2,5;3,5}]
- Set fso = CreateObject("scripting.filesystemobject")
- Set fp = fso.getfolder(ThisWorkbook.Path)
- Set wd = CreateObject("word.application")
- wd.Visible = True
- n = 1
- For Each subp In fp.subfolders
- For Each f In subp.Files
- If f Like "*.doc*" Then
- With wd.documents.Open(f & "")
- For i = 1 To UBound(ar)
- arr(n, i) = Application.Clean(.tables(1).Cell(ar(i, 1), ar(i, 2)))
- Next
- On Error Resume Next
- For Each tabe In .tables
- m = 5
- s = Application.Clean(tabe.Cell(m, 1))
- Do While IsNumeric(s)
- If Err = 0 Then
- arr(n, 7) = s
- k = tabe.Cell(m, 2): k = Left(k, Len(k) - 2)
- k1 = tabe.Cell(m, 3): k1 = Left(k1, Len(k1) - 2)
- br = Split(k, vbCr): cr = Split(k1, vbCr)
- If UBound(br) = UBound(cr) Then
- For i = 0 To UBound(br)
- arr(n, 8) = br(i): arr(n, 9) = cr(i)
- n = n + 1
- Next
- Else
- arr(n, 8) = k: arr(n, 9) = k1: n = n + 1
- End If
- Else
- Err.Clear
- End If
- m = m + 1
- If tabe.Rows.Count = m Then Exit Do
- s = Application.Clean(tabe.Cell(m, 1))
- Loop
- Next
- On Error GoTo 0
- .Close False
- End With
- End If
- Next
- Next
- wd.Quit: Set fso = Nothing
- Sheets("主文件").Range("a4").Resize(n, 9) = arr
- End Sub
复制代码 |
|