|
楼主 |
发表于 2018-3-23 09:22
|
显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
我这个基础数据量比较大,还要牵扯到一个筛选相应单位的过程,word表格里放不下也不好筛选。所以才想到这个流程。之前实现了3页的粘贴, 结果我改动了一点程序又不能粘贴了,不知道什么原因。现在的代码:
Sub 生成()
Dim dpath, Filename As String
Dim wdapp As Object, wrddoc, arr, i
Dim rng As Range
Dim s As Section
On Error Resume Next
Application.ScreenUpdating = False
arr = Worksheets("选择").Range("B31:B" & ws.Range("B65536").End(xlUp).Row)
Set wdapp = CreateObject("Word.Application") '打开word
wdapp.Visible = 0
dpath = ThisWorkbook.Path
Filename = Dir(dpath & "\正面.doc")
Set wrddoc = wdapp.Documents.Open(dpath & "\" & Filename)
i = 1
With ActiveDocument '调测
For Each s In .Sections
If s.Range.Paragraphs.Count <= 6 Then Exit For
If s.Range.Paragraphs.Count > 6 Then
Excel.Application.Sheets("选择").Activate '激活excel
If i <= UBound(arr) Then
[K4] = arr(i, 1)
Sheets("处理").Columns("A:G").AdvancedFilter Action:=xlFilterCopy, _
CriteriaRange:=Range("K3:K4"), CopyToRange:=Range("A2:G20"), Unique:= _
False
ActiveWindow.SmallScroll Down:=3
Range("B3:G20").Select
ActiveWorkbook.Worksheets("选择").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("选择").Sort.SortFields.Add Key:=Range( _
"B3:B20"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
xlSortNormal
With ActiveWorkbook.Worksheets("选择").Sort
.SetRange Range("B3:G20")
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Sheets("选择").Range("B1:G" & Sheets("选择").Range("G65536").End(xlUp).Row).Copy
wdapp.Application.Activate '回到word粘贴.
s.Range.Paragraphs(7).Range.Paste
i = i + 1
End If
End If
Next
End With
wdapp.Application.Activate '保存word文件
wdapp.Saved = True
wdapp.ActiveDocument.SaveAs "D:\插入后文件.doc"
wdapp.Application.Quit
Set wdapp = Nothing
Application.ScreenUpdating = True
End Sub
|
|