Help - export range with vba to txt file

mihaibantas

New Member
Joined
Oct 14, 2016
Messages
4
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
 

Some videos you may like

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number

John_w

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

Watch MrExcel Video

Forum statistics

Threads
1,109,033
Messages
5,526,362
Members
409,697
Latest member
christopherlewis1620

This Week's Hot Topics

Top