以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.com/bbs/index.asp)
--  专家坐堂  (http://foxtable.com/bbs/list.asp?boardid=2)
----  EXCEL VBA复制时格式和内容一起复制问题  (http://foxtable.com/bbs/dispbbs.asp?boardid=2&id=119637)

--  作者:benwong2013
--  发布时间:2018/5/29 10:58:00
--  EXCEL VBA复制时格式和内容一起复制问题

根据之前贴的内容已经完成了表单复制之后合并,但表格的行高无法复制,请问以下代码如何修改?


Dim Book As New XLS.Book(ProjectPath & "Attachments\\出口公路舱单模板皇岗 - 副本1.xlsx")
Dim fl As String = ProjectPath & "reports\\出口公路舱单模板皇岗 - 副本.xlsx"
Book.Build() \'生成细节区
Book.Save(fl) \'保存工作簿
Dim App As New MSExcel.Application
Dim Wb As MSExcel.WorkBook = App.WorkBooks.Open(fl)
Dim Ws1 As MSExcel.WorkSheet = Wb.WorkSheets("预配(出口)舱单多票模板") \'指定要复制的工作表
Dim Ws2 As MSExcel.WorkSheet = Wb.WorkSheets("sheet1")
Dim Ws3 As MSExcel.WorkSheet = Wb.WorkSheets("sheet2")

Ws2.UsedRange.Copy
ws1.Select
ws1.Cells(Ws1.UsedRange.Rows.Count+1,2).Select \'纵向拷贝
ws1.paste

Ws3.UsedRange.Copy
ws1.Select
ws1.Cells(Ws1.UsedRange.Rows.Count+1,2).Select \'纵向拷贝
ws1.paste

\'Wb.Save
app.Visible = True
\'App.Quit



--  作者:benwong2013
--  发布时间:2018/5/29 11:00:00
--  
要求复制粘贴的时候格式不变
--  作者:有点甜
--  发布时间:2018/5/29 11:43:00
--  

比较麻烦,参考代码,细节自己调整


Dim fl As String = "d:\\test.xls"
Dim App As New MSExcel.Application
Dim Wb As MSExcel.WorkBook = App.WorkBooks.Open(fl)
Dim Ws1 As MSExcel.WorkSheet = Wb.WorkSheets("sheet3") \'指定要复制的工作表
Dim Ws2 As MSExcel.WorkSheet = Wb.WorkSheets("sheet1")
Dim Ws3 As MSExcel.WorkSheet = Wb.WorkSheets("sheet2")


Dim x As Integer = Ws1.UsedRange.Rows.Count+1

Ws2.UsedRange.Copy
ws1.Select
ws1.Cells(x,2).Select \'纵向拷贝
ws1.paste

\'设置列格式
ws2.Select
ws2.rows("1:" & ws2.UsedRange.rows.count).Select
app.CutCopyMode = False
app.Selection.Copy
ws1.Select
ws1.rows(x & ":" & x+ws2.usedRange.rows.count).Select
app.selection.PasteSpecial(Paste:=-4122, Operation:=-4142,SkipBlanks:=False, Transpose:=False)
app.CutCopyMode = False

\'设置行格式
ws2.Select
ws2.columns(ws2.cells(1,1).address.split("$")(1) & ":" & ws2.cells(1,ws2.UsedRange.columns.count).address.split("$")(1)).Select
app.CutCopyMode = False
app.Selection.Copy
ws1.Select
ws1.columns(ws1.cells(1,2).address.split("$")(1) & ":" &  ws1.cells(1,2+ws2.usedRange.columns.count).address.split("$")(1)).Select
app.selection.PasteSpecial(Paste:=-4122, Operation:=-4142,SkipBlanks:=False, Transpose:=False)
app.CutCopyMode = False

\'设置单元格格式
ws2.Select
ws2.UsedRange.Select
app.CutCopyMode = False
app.Selection.Copy
ws1.Select
ws1.range(ws1.cells(x, 2).address & ":" & ws1.cells(x+ws2.UsedRange.rows.count, 2+ws2.UsedRange.columns.count).address).Select
msgbox(ws1.cells(x, 2).address & ":" & ws1.cells(x+ws2.UsedRange.rows.count, 2+ws2.UsedRange.columns.count).address)
app.selection.PasteSpecial(Paste:=-4122, Operation:=-4142,SkipBlanks:=False, Transpose:=False)
app.CutCopyMode = False

\'Wb.Save
app.Visible = True
\'App.Quit