1、在工作表中,点击菜单栏【开发工具】,在其工具栏内,点击【visual basic】!
2、会进入VBA编辑界面!点击菜单栏【插入】,在其下拉菜单中,选择【模块】!
3、会弹出模块编辑窗口!
4、在模块编辑窗口输入以下代码:Sub 提取数据()Dim wdapp As Obje罕铞泱殳ctSet wdapp = CreateObject("word.application")wdapp.Documents.Open Filename:=ThisWorkbook.Path & "\DEMO\9-4.demo.docx"wdapp.Visible = TrueWith wdapp.Documents(1).Tables(1).Range For i = 1 To .Cells.Count u = Left(.Cells(i).Range, Len(.Cells(i).Range) - 1) If IsNumeric(u) Then n = n + 1 Cells(n + 1, 2) = u Cells(n + 1, 1) = Left(.Cells(i - 1).Range, Len(.Cells(i - 1).Range) - 1) End If NextEnd With Cells(1, 1) = "姓名" Cells(1, 2) = "分数"wdapp.QuitEnd Sub
5、按F8键,一步步运行代码!同时可以检查代码编写的是否正确!
6、运行完毕后,我们会看到数据被提取到excel表格中!