【发布时间】:2016-08-30 07:14:41
【问题描述】:
大家早上好。
我不想在这里活跃,但这是我正在做的一个项目(它需要大量搜索、复制、粘贴、尝试、编辑、重复) -
这是一个包含多个列的表格,如下所示:
第 1 列 |第 2 栏 |第 3 栏 |第四栏 | ... |结肠
第 1 行 |第 1 行 |第 1 行 |第 1 行 | ... |第 1 行
第 2 行 |第 2 行 |第 2 行 |第 2 行 | ... |第 2 行
...
第 n 行 |第 n 行 |第 n 行 |第 n 行 | ... |行n
Sub CopySubsectionToTable()
Dim CFsh As Worksheet
Dim lastcol As Integer
Dim lastrow As Integer
Dim i As Integer
Dim WordApp As Word.Application
Dim WordDoc As Word.Document
Dim IDQRange As Range
Dim AnswRange As Range
Dim FWTable As Range
Set CFsh = Sheets("ConsumerFireworks")
'Finding CFsh Array's end boundaries
lastcol = CFsh.Cells(4, CFsh.Columns.Count).End(xlToLeft).Column
lastrow = CFsh.Cells(CFsh.Rows.Count, 1).End(xlUp).Row
Set IDQRange = CFsh.Range(CFsh.Cells(4, 1), CFsh.Cells(lastrow, 2))
'Optimize Code
Application.ScreenUpdating = False
Application.EnableEvents = False
'Set Destination To Word Document
Set WordApp = CreateObject(class:="Word.Application")
WordApp.Visible = True
Set WordDoc = WordApp.Documents.Add
'Copy Tables
For i = 4 To lastcol
Set AnswRange = CFsh.Range(CFsh.Cells(4, i), CFsh.Cells(lastrow, i))
Set FWTable = Range(IDQRange, AnswRange)
FWTable.Resize(, i).Copy
If i > 4 Then WordDoc.Range(WordDoc.Content.End - 1).InsertBreak Type:=wdPageBreak
WordDoc.Range(WordDoc.Content.End - 1).Paste
WordDoc.Range.InsertParagraphAfter
'Feeble attempt to hide coppied cells
CFsh.Columns(i).Hidden = True
Next i
CFsh.Columns.Hidden = False
Application.CutCopyMode = False
Set AppWord = Nothing
End Sub
结果是这样的
第 1 列 |第 2 栏|第 3 栏 |第 i 列
第 1 行 |第 1 行 |第 1 行 |第 1 行
第 2 行 |第 2 行 |第 2 行 |第 2 行
...
第 n 行 |第 n 行 |第 n 行 |行n
分页符
第 1 列 |第 2 栏|第 3 栏 |第 i 列
第 1 行 |第 1 行 |第 1 行 |第 1 行
第 2 行 |第 2 行 |第 2 行 |第 2 行
...
第 n 行 |第 n 行 |第 n 行 |行n
分页符
重复到 i
为什么要复制/粘贴第 3 列?我希望它跳过庞大的表格,保留第 1 列、第 2 列,然后在第 3 列之后取每一列,在每个分页符之间制作一个表格。
任何帮助或指导将不胜感激。谢谢!
更新
这是我正在运行的控件 -
Sub CopySubsectionToTable()
Dim CFsh As Worksheet
Dim lastcol As Integer
Dim lastrow As Integer
Dim i As Integer
Dim WordApp As Word.Application
Dim WordDoc As Word.Document
Dim IDQRange As Range
Dim AnswRange As Range
Dim FWTable As Range
Dim CFTables As Range
Set CFsh = Sheets("ConsumerFireworks")
'Finding CFsh Array's end boundaries
lastcol = CFsh.Cells(4, CFsh.Columns.Count).End(xlToLeft).Column
lastrow = CFsh.Cells(CFsh.Rows.Count, 1).End(xlUp).Row
Set IDQRange = CFsh.Range(CFsh.Cells(4, 1), CFsh.Cells(lastrow, 2))
'Optimize Code
Application.ScreenUpdating = False
Application.EnableEvents = False
'Set Destination To Word Document
Set WordApp = CreateObject(class:="Word.Application")
WordApp.Visible = True
Set WordDoc = WordApp.Documents.Add
'Copy Tables
'For i = 4 To lastcol
i = 4
Set AnswRange = CFsh.Range(CFsh.Cells(4, i), CFsh.Cells(lastrow, i))
Set FWTable = Range(IDQRange, AnswRange)
Set CFTables = Union(IDQRange, AnswRange)
MsgBox ("CFTables is " & CFTables.Address)
'FWTable.Resize(, i).Copy
CFTables.Copy
If i > 4 Then WordDoc.Range(WordDoc.Content.End - 1).InsertBreak Type:=wdPageBreak
WordDoc.Range(WordDoc.Content.End - 1).Paste
'typical location for copypaste error
WordDoc.Range.InsertParagraphAfter
'Feeble attempt to hide coppied cells
CFsh.Columns(i).Hidden = True
'Next i
CFsh.Columns.Hidden = False
Application.CutCopyMode = False
Set AppWord = Nothing
End Sub
设置 CFTables Union 为我提供了正确的地址,即 $A$1:$B$50,$D$1:$D$50
除了我打算稍后清理的剪贴板的复制粘贴错误之外,它还会将一张表粘贴到带有 C 列的单词中!
我怀疑这是罪魁祸首
WordDoc.Range(WordDoc.Content.End - 1).Paste
更新#2
*#$& 我,我手动选择范围并将它们粘贴到 word 中,它做同样的事情。
【问题讨论】:
标签: excel vba copy-paste