How to append current sheet contents to existing text file - VBA

jardenp

Active Member
Joined
May 12, 2009
Messages
373
Office Version
  1. 2019
  2. 2016
  3. 2013
  4. 2011
  5. 2010
Platform
  1. Windows
Is there a VBA code that will take the current sheet's data and append it to an existing text file with no blank lines between the original data and appended data? I've found various solutions to combine existing text files, but none that starts with the data on the current sheet.

The data in the destination.txt is an undefined number (always greater than 0 though) of 84-character strings, each one on a different row.

In the Excel sheet, each 84-character string is in a cell in column A, starting with A1 and extending down, always in a contiguous block. It may be only A1 or, say, A1:A20. It will never be something like A1:A3, A7:A10.

If need be, the current sheet can be saved as origin.txt in the same directory as destination.txt. (That part I know how to do!)

I do not need to rename destination.txt

Thanks!
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
If need be, the current sheet can be saved as origin.txt in the same directory as destination.txt.
Do this, then run:
Code:
Sub Append_Files()

    Dim CopyCommand As String
    
    CopyCommand = "COPY /B ""C:\folder\path\destination.txt"" + ""C:\folder\path\origin.txt"" ""C:\folder\path\destination.txt"" "
    Shell Environ("COMSPEC") & " /c " & CopyCommand
    
End Sub
change the drive letter and folder path as needed.
 
Upvote 0
Thanks! That works great. It appends to the last character instead of starting a new row, but I added a row insert in what would become the origin.txt and that starts it on a new line. Thanks so much!
 
Upvote 0

Forum statistics

Threads
1,213,527
Messages
6,114,144
Members
448,552
Latest member
WORKINGWITHNOLEADER

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top