Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6360881
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:42:18+00:00 2026-05-24T23:42:18+00:00

Sub Merge() Dim File As String Dim AllFiles(), Filename As Variant Dim count, test,

  • 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

End Sub

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T23:42:18+00:00Added an answer on May 24, 2026 at 11:42 pm
    • You redim AllFiles but never fill it with anything. Is there missing code?
    • AllFiles is a 0 based array so if you want to start at the second element you need to use test = 1 instead of test = 2.
    • For looping through an array, try this:

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

    • Is “LastRow” a named range? If not, that’s not going to work. The following will select the last used row in a worksheet:

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

    • Your SaveAs is failing because 1) AllFiles looks like it’s never filled and 2) your save path as you wrote would be literally: C:\Allfile(1)&Allfiles(count)\.xlsm. You want:

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

    EDIT After Code Update

    • You never initialize your count variable, add count = 0 to the beginning just to be safe.

    • GetOpenFilename does in fact return the full path. Once you have that path stored in a variable (such as AllFiles()) you can get just the filename portion with mid(AllFiles(test), instrrev(AllFiles(test), "\") + 1)

    • You don’t need the ReDim AllFiles(count) prior to your main Do Loop. ReDim erases the contents of the array unless you use the Preserve keyword.

    • Change Workbooks(AllFiles(test)).Activate to Workbooks(Mid(AllFiles(test), InStrRev(AllFiles(test), "\") + 1)).Activate to strip the path information and leave just the filename.

    • Windows("Allfiles(1).xml").Activate won’t work since your sending a literal string. You want WORKBOOKS(Mid(AllFiles(1), InStrRev(AllFiles(1), "\") + 1)).Activate here again.

    • LastRow = LastRow + 1 probably isn’t what you meant. Try Set LastRow = LastRow.Offset(1, 0)

    • Change Range("LastRow").Select to LastRow.select

    • All instances of Windows( should be changed to Workbooks(

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Private Sub Command1_Click() Dim x As Integer For x = 1 To 100 List1.AddItem
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress Dim allowedChars
Private Sub Command1_Click() Dim dom As New DOMDocument Dim http As New XMLHTTP Dim
I have classX: Sub New(ByVal item_line_no As String, ByVal item_text As String) ' check
just curious if someone knows how to merge every other sub array, aka $tmp
I have a domain 'www.foo.com' and I want to create sub domain 'test.foo.com'. In
I need to find all sub-arrays which share any mutual element and merge them
I write a sub in Perl to merge 2 hashes of the same structure;
sub do_printf { printf @_ } sub do_sprintf { print sprintf @_ } do_printf(%s\n,
Given this: Public Sub timReminder_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) If DateTime.Now()

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.