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

jardenp

Active Member
Joined
May 12, 2009
Messages
367
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

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.

John_w

MrExcel MVP
Joined
Oct 15, 2007
Messages
6,751
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.
 

jardenp

Active Member
Joined
May 12, 2009
Messages
367
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!
 

Watch MrExcel Video

Forum statistics

Threads
1,129,553
Messages
5,636,989
Members
416,953
Latest member
broexc

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
Top