如何使用vba将单词中的单个列复制到预先存在的excel表中?

时间:2019-06-05 18:26:24

标签: excel vba ms-word

我的单词表有两列。我只想将第二列复制到excel中现有表的第二列中。我的“ for”循环正常工作,但所有数据都重复存储在同一单元格中。如何存储在列的连续单元格中?

Sub copyToExcel()
Dim xlapp As Excel.Application
Dim xlwb As Excel.Workbook
Dim xlsheet As Excel.Worksheet

Dim doc As Document
Dim tbl As Table
Dim lstrw As Long, lstcol As Integer
Dim tblrange As Range

Set doc = ThisDocument
Set xlapp = CreateObject("Excel.Application")
xlapp.Visible = True
Set xlwb = xlapp.Workbooks.Open("C:\Users\ankit\Downloads\challenges.xlsm")
Set xlsheet = xlwb.Worksheets("Challenge #2")

Set tbl = doc.Tables(1)
With tbl
 lstrw = .Rows.Count
 lstcol = .Columns.Count
 For x = 2 To lstrw
    Set tblrange = .Cell(x, 2).Range
    tblrange.End = .Cell(lstrow, lstcol).Range.End
    tblrange.Copy
    xlsheet.Paste
 Next x
End With

Set xlapp = Nothing
Set xlwb = Nothing
Set tblrange = Nothing
Set xlsheet = Nothing
Set doc = Nothing

End Sub

0 个答案:

没有答案