Help - export range with vba to txt file

mihaibantas

New Member
Joined
Oct 14, 2016
Messages
12
Hi to all,
I have an excel file and I want to export the contents of text cells (range C2:D5) ... in a file.gcp (is a text file)....maybe someone has a VBA code ... to export values from range C2:D5 like this:


MYTEXT A-z,1.00,2.00,MYTEXT Z-a (where 1.00=C2 and 2.00=D2)
MYTEXT NEW,4.00,5.00,MYTEXT OLD (where 4.00=C3 and 5.00=D3)
MYTEXT RED,6.00,8.00,MYTEXT GREEN (where 6.00=C4 and 8.00=D4)
MYTEXT GOOD,10.00,12.00,MYTEXT BAD (where 10.00=C5 and 12.00=D5)

... thanks in advance for your time
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Here's one way:
Code:
Public Sub Create_gcp_File()

    Dim gcpLines(1 To 4) As String
    Dim fileNum As Integer
    
    With ActiveSheet
        gcpLines(1) = Join(Array("MYTEXT A-z", Format(.Range("C2").Value, "0.00"), Format(.Range("D2").Value, "0.00"), "MYTEXT Z-a"), ",")
        gcpLines(2) = Join(Array("MYTEXT NEW", Format(.Range("C3").Value, "0.00"), Format(.Range("D3").Value, "0.00"), "MYTEXT OLD"), ",")
        gcpLines(3) = Join(Array("MYTEXT RED", Format(.Range("C4").Value, "0.00"), Format(.Range("D4").Value, "0.00"), "MYTEXT GREEN"), ",")
        gcpLines(4) = Join(Array("MYTEXT GOOD", Format(.Range("C5").Value, "0.00"), Format(.Range("D5").Value, "0.00"), "MYTEXT BAD"), ",")
    End With
    
    fileNum = FreeFile
    Open "C:\folder\path\file.gcp" For Output As [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=fileNum]#fileNum[/URL]            'CHANGE FOLDER PATH
    Print [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=fileNum]#fileNum[/URL] , gcpLines(1)
    Print [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=fileNum]#fileNum[/URL] , gcpLines(2)
    Print [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=fileNum]#fileNum[/URL] , gcpLines(3)
    Print [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=fileNum]#fileNum[/URL] , gcpLines(4)
    Close [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=fileNum]#fileNum[/URL] 
        
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,196
Members
452,616
Latest member
intern444

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