VB 合并excel文件问题?

w1984721 2009-07-28 12:33:15
我想把一个Excel 文件中的两个sheet(sheet1,sheet2),合并到一个新的Excel文件中的一个sheet中(sheet1).

要合并sheet1,和sheet2的数据区域是不可知的,合并到新文件的sheet1中,
合并后在新文件的sheet1中数据区域是连续的,也就是sheet1数据区域结束后.接着下面就是sheet2的数据区域.可能有点难,哪位大哥写个范例代码看.
获取EXCEL的单元格区域的方法在下面:供大家参考.
Dim A1 As String
Dim A2 As String
Dim Columns As Long
Dim Rows As Long

Set xlApp = CreateObject("Excel.Application") '创建EXCEL应用类
xlApp.Visible = False '设置EXCEL可见
Set xlBook = xlApp.Workbooks.Open("D:\a.xls")
Set xlSheet = xlBook.Worksheets(1) '打开EXCEL工作表
Rows = xlBook.Worksheets(1).UsedRange.Rows.Count
Columns = xlBook.Worksheets(1).UsedRange.Columns.Count
A1 = xlSheet.UsedRange.Cells(1, 1).Address
A2 = xlSheet.UsedRange.Cells(Rows, Columns).Address

xlSheet.Range(A1, A2).Copy
...
下面怎么写我就不知道了,希望高手赐教.

...全文
150 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
yangguo8013 2009-08-06
  • 打赏
  • 举报
回复
我帮你试过了,是把sheet1,sheet2的内容全部放到新的sheet里面了,并且是连贯的
li_yaoming 2009-08-06
  • 打赏
  • 举报
回复
在excel中操作一下,录制成新的宏,然后把宏拷到程序中根据要求修改一下就可以了。
yangguo8013 2009-08-05
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 tiger_zhao 的回复:]
VB codeOptionExplicitSub Main()Dim xlAppAsObjectDim xlBookAsObjectDim xlBookNewAsObjectDim xlSheet1AsObjectDim xlSheet2AsObjectDim xlSheetNewAsObjectSet xlApp=CreateObject("Excel.Application")
xlApp.Visible=FalseSet xlBook= xlApp.Workbooks.Open("D:\a.xls")Set xlSheet1= xlBook.Sheets(1)Set xlSheet2= xlBook.Sheets(2)

xlSheet1.CopySet xlBookNew= xlApp.Workbooks(xlApp.Workbooks.Count)Set xlSheetNew= xlBookNew.Sheets(1)

xlSheet2.Activate
xlSheet2.UsedRange.Copy
xlSheetNew.Activate
xlSheetNew.Cells(xlSheetNew.UsedRange.Rows.Count+1,1).Select
xlSheetNew.Paste

xlBookNew.SaveAs"D:\a-new.xls"Set xlSheetNew=Nothing
xlBookNew.CloseFalseSet xlBookNew=NothingSet xlSheet1=NothingSet xlSheet2=Nothing
xlBook.CloseFalseSet xlBook=Nothing

xlApp.QuitSet xlApp=NothingEnd Sub
[/Quote]
我以前写过读取,老鸟写的可以尝试
lyserver 2009-07-31
  • 打赏
  • 举报
回复
cqq_chen 2009-07-31
  • 打赏
  • 举报
回复
有意义吗?
Excel自己做这事多快!
weiyulin 2009-07-31
  • 打赏
  • 举报
回复

帮顶.....
Tiger_Zhao 2009-07-29
  • 打赏
  • 举报
回复
Option Explicit

Sub Main()
Dim xlApp As Object
Dim xlBook As Object
Dim xlBookNew As Object
Dim xlSheet1 As Object
Dim xlSheet2 As Object
Dim xlSheetNew As Object

Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = False
Set xlBook = xlApp.Workbooks.Open("D:\a.xls")
Set xlSheet1 = xlBook.Sheets(1)
Set xlSheet2 = xlBook.Sheets(2)

xlSheet1.Copy
Set xlBookNew = xlApp.Workbooks(xlApp.Workbooks.Count)
Set xlSheetNew = xlBookNew.Sheets(1)

xlSheet2.Activate
xlSheet2.UsedRange.Copy
xlSheetNew.Activate
xlSheetNew.Cells(xlSheetNew.UsedRange.Rows.Count + 1, 1).Select
xlSheetNew.Paste

xlBookNew.SaveAs "D:\a-new.xls"
Set xlSheetNew = Nothing
xlBookNew.Close False
Set xlBookNew = Nothing

Set xlSheet1 = Nothing
Set xlSheet2 = Nothing
xlBook.Close False
Set xlBook = Nothing

xlApp.Quit
Set xlApp = Nothing
End Sub
孤独剑_LPZ 2009-07-28
  • 打赏
  • 举报
回复
两个sheet(sheet1,sheet2),数据格式(字段)是否一样?
lxq19851204 2009-07-28
  • 打赏
  • 举报
回复
不一样有难度呀,不懂,顶一下先.
w1984721 2009-07-28
  • 打赏
  • 举报
回复
不一样的!

7,763

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧