0
Sub Merge()
Dim File      As String
Dim AllFiles(), Filename As Variant
Dim count, test, StartRow, LastRow, LastColumn As Long
Dim LastCell As Variant
test = 0
ChDir "C:\" 'Insert suitable directory for your computer ex:ChDir "C:\Users\Jerry Hou\" if file of interest is in "Jerry Hou" Folder
  ReDim AllFiles(1)
Do
    Application.EnableCancelKey = xlDisabled
    File = Application.GetOpenFilename("XML Files (*.xml),*.xml", 1, "Select File to be Merged") 'Needs to select in Order to merge files
    Application.EnableCancelKey = xlErrorHandler
    If (File = "False") Then Exit Do
    ReDim Preserve AllFiles(count) 'Preserve ?
    AllFiles(count) = File 'File== file name and directory
    count = (count + 1)
    If (MsgBox("Select Another File To be Merged With?", vbQuestion + vbOKCancel, "Merge Files") = vbCancel) Then Exit Do
Loop  'Select Cancel in MsgBox to finish merge file(s) selection

If (count = 0) Then
    MsgBox "No selection" 'If you hit Exit from open prompt window
    Exit Sub
End If

 For count = 0 To UBound(AllFiles)
    MsgBox "User selected file name: " & AllFiles(count)

Next
 test = count
 For test = UBound(AllFiles) To LBound(AllFiles) Step -1
 Workbooks.Open Filename:=AllFiles(test)
Next

ReDim AllFiles(count)
 test = 2
Do While (test <= count)
Filename = AllFiles(test)
Workbooks(AllFiles(test)).Activate 'ERROR Brings 2nd file that the user had selected to Last xml file selected in order to Front
 'Copy and Paste TMG tab
 Sheets("TMG_4 0").Activate
 StartRow = 2
 LastRow = Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
 LastColumn = Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
 LastCell = Cells(LastRow, LastColumn).Address 'Find lastcell of to be copied file
 Range("A2:" & LastCell).Select
 Selection.Copy
 Windows("Allfiles(1).xml").Activate 'ERROR
 Sheets("TMG_4 0").Activate
 LastRow = Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
 LastRow = LastRow + 1
 Range("LastRow").Select 'ERROR
 ActiveSheet.Paste

 'Copy and Paste Gamma tab
 Sheets("GammaCPS 0").Activate
 StartRow = 2
 LastRow = Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
 LastColumn = Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
 LastCell = Cells(LastRow, LastColumn).Address
 Range("A2:" & LastCell).Select
 Selection.Copy

 Windows("Allfiles(1).xml").Activate 'ERROR Windows("File_name.xlsm").activate 
 Sheets("GammaCPS 0").Activate
 LastRow = Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
 LastRow = LastRow + 1
 Range("LastRow").Select 'ERROR
 ActiveSheet.Paste
 test = test + 1
Loop

Windows("Allfiles(1).xml").Activate 'ERROR

ActiveWorkbook.SaveAs Filename:="C:\" & AllFiles(1) & AllFiles(test) & ".xlsm", FileFormat:=52

结束子

4

1 回答 1

0
  • 你 redim AllFiles 但从来没有用任何东西填充它。是否缺少代码?
  • AllFiles 是一个基于 0 的数组,所以如果你想从第二个元素开始,你需要使用 test = 1 而不是 test = 2。
  • 要遍历数组,请尝试以下操作:

    For test = 1 to ubound(AllFiles) - 1 'This loops through the array from the second element to the last

  • “LastRow”是一个命名范围吗?如果没有,那是行不通的。以下将选择工作表中最后使用的行:

    activesheet.Rows(activesheet.usedrange.rows.count).select

  • 您的 SaveAs 失败是因为 1)AllFiles 看起来从未填充过,并且 2)您编写的保存路径实际上是:C:\Allfile(1)&Allfiles(count)\.xlsm. 你要:

    ActiveWorkbook.SaveAs Filename:= "C:\" & AllFiles(1) & AllFiles(test) & ".xlsm"

代码更新后编辑

  • 您永远不会初始化您的计数变量,count = 0只是为了安全起见添加到开头。

  • GetOpenFilename实际上确实返回了完整路径。一旦将该路径存储在变量中(例如 AllFiles()),您就可以只获取文件名部分mid(AllFiles(test), instrrev(AllFiles(test), "\") + 1)

  • 你不需要ReDim AllFiles(count)在你的主循环之前。除非您使用 Preserve 关键字,否则 ReDim 会擦除数组的内容。

  • 更改Workbooks(AllFiles(test)).ActivateWorkbooks(Mid(AllFiles(test), InStrRev(AllFiles(test), "\") + 1)).Activate删除路径信息并仅保留文件名。

  • Windows("Allfiles(1).xml").Activate自从您发送文字字符串以来将无法正常工作。你想WORKBOOKS(Mid(AllFiles(1), InStrRev(AllFiles(1), "\") + 1)).Activate再来这里。

  • LastRow = LastRow + 1可能不是你的意思。尝试Set LastRow = LastRow.Offset(1, 0)

  • 更改Range("LastRow").SelectLastRow.select

  • 的所有实例Windows(都应更改为Workbooks(

于 2011-08-21T21:41:22.660 回答