Update text file with Excel

mjtwo3

New Member
Joined
Apr 4, 2012
Messages
4
Hello All,

I need to copy cells B2:C8, open an already saved text file (Test.txt), highlight everything in there, paste in the info from excel, and then save the text file.

Thanks in advance
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
What's the purpose of highlighting everything? Do you want to replace the exsiting contents?

Dom
 
Upvote 0
You will have to refer this thread for information on the object used:
DataObject

The code you will have to adjust to suit your text file path.
Code:
Public Sub WriteDataToTxtFile()
Dim objDaOb As Object
Dim rngCOPY As Range
Dim strPath As String

Set objDaOb = CreateObject("New:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
Set rngCOPY = Range("B2:C8")    'Set Range Reference
strPath = "C:\Test.txt"         'Set text file path

rngCOPY.Copy
objDaOb.GetFromClipboard
Open strPath For Output As #1
Print #1, objDaOb.GetText
Close #1
Application.CutCopyMode = False

End Sub
 
Upvote 0
Domski, yes i want to replace existing context.

taurean - that didn't seem to work. when i go to my txt file it is not updated with the same data that is in excel.
 
Upvote 0
taurean - that didn't seem to work. when i go to my txt file it is not updated with the same data that is in excel.
I had tested the code with false data and it did work so not sure what is different in your setup that fails it. Can you post the code as you have now?
 
Upvote 0

Forum statistics

Threads
1,212,134
Messages
6,106,141
Members
447,998
Latest member
tippairie1

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