|
各位大神们,现在有个需求:excel合并同意目录下工作簿中某一个指定的sheet,并有公式链接的单元格不要变! 在网上找了以下代码,合并工作簿可以实现合并,但是带有表间链接公式的数值就不对了。比如表1中 C1,C2,C3的值和表2中C1,C2,C3的值,合并后excel中会显示为C1,C2,C3,C4,C5,C6 按照顺序进行变化了,导致表2的数据合并后出现错误,怎么解决呢?
=====以下为目前实现合并需求的代码===========
Sub 合并当前目录下所有工作簿的sheet1工作表()
Dim MyPath, MyName, AWbName
Dim Wb As Workbook, WbN As String, G As Long, Num As Long
Application.ScreenUpdating = False
MyPath = ActiveWorkbook.Path
MyName = Dir(MyPath & "\" & "*.xls*")
AWbName = ActiveWorkbook.Name
Num = 0
Do While MyName <> ""
If MyName <> AWbName Then
Set Wb = Workbooks.Open(MyPath & "\" & MyName)
Num = Num + 1
With Workbooks(1).ActiveSheet
If Num = 1 Then
Wb.Sheets(1).UsedRange.Copy .Cells(.Range("A65536").End(xlUp).Row + 1, 1) 'Wb.Sheets(2)指定合并sheet2中的数据
Else
Wb.Sheets(1).UsedRange.Offset(1, 0).Copy .Cells(.Range("A65536").End(xlUp).Row + 1, 1) 'Wb.Sheets(2)指定合并sheet2中的数据
End If
WbN = WbN & Chr(13) & Wb.Name
Wb.Close False
End With
End If
MyName = Dir
Loop
Range("A1").Select
Application.ScreenUpdating = True
MsgBox "共合并了" & Num & "个工作薄的工作表。如下:" & Chr(13) & WbN, vbInformation, "提示"
End Sub
|
|