且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

VBA从Word表复制到Excel

更新时间:2023-12-04 13:53:48

经过一番审查,我发现我应该在粘贴中使用pastespecial,正确的代码如下所示

After some review, i;ve found that i shoud use pastespecial in my paste the corrected code is bellow

Sub copyfromwordtoexcel()
Dim exApp As Excel.Application
Dim exDoc As Excel.Workbook
Set exApp = CreateObject("Excel.Application")
Set exDoc = exApp.Workbooks.Add
For xx = 1 To ActiveDocument.Tables.Count
On Error Resume Next
If ActiveDocument.Tables(xx).Columns.Count = 2 Then

ActiveDocument.Tables(xx).Cell(2, 2).Range.Copy
exApp.Visible = True
Cells(xx, 1).Select
ActiveSheet.PasteSpecial (xlPasteAll)

Application.Visible = True
exApp.Visible = False
ActiveDocument.Tables(xx).Cell(3, 2).Range.Copy
exApp.Visible = True
Cells(xx, 2).Select
ActiveSheet.PasteSpecial (xlPasteAll)
i = ActiveDocument.Tables(xx).Rows.Count
ActiveDocument.Tables(xx).Cell(i - 2, 2).Range.Copy
exApp.Visible = True
Cells(xx, 3).Select
ActiveSheet.PasteSpecial (xlPasteAll)
Application.Visible = True
ActiveDocument.Tables(xx).Cell(i - 1, 2).Range.Copy
exApp.Visible = True
Cells(xx, 4).Select
ActiveSheet.PasteSpecial (xlPasteAll)
Application.Visible = True
ActiveDocument.Tables(xx).Cell(i, 2).Range.Copy
exApp.Visible = True
Cells(xx, 5).Select
ActiveSheet.PasteSpecial (xlPasteAll)
Application.Visible = True
exApp.Visible = True
End If

Next

结束子