Concate multiple columns in VBA and save output to file

Akhilesh28

New Member
Joined
Apr 12, 2021
Messages
2
Office Version
  1. 2016
Platform
  1. Windows
Hello guys,
This us my 1st post.
Please do the needful

I am trying to concatenate 2 columns out of 7 and save those columns data in a file seperated by comma.
E.g Cell A2 has abc and Cell E2 has 123.
A3 has pqr and E3 has 789

Expected Result:
abc,123
pqr,789

But I am getting
"abc,123
","pqr,789
",


Code:

Private sub export()
Dim myfile as string, count as long, cellvalue as variant, i as integer

Myfile = location of file

Count= worksheetfunction.CountA("A1", range("a1").end(xldown))

Open Myfile for output as #1

For i =2 to count
Cellvalue = rng.cells(i,1).Value & ','& rng.cells(i,5).Value & vbCrLf
Write #1, cellvalue
Next i
Close #1

End sub

Please ignore the cases. I typed in bit hurry
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Hi,​
that is normal just reading the VBA help of Write # statement ! So use Print # instead …​
 
Upvote 0
Solution
Hi,​
that is normal just reading the VBA help of Write # statement ! So use Print # instead …​
Thanks.. it worked. But 1 othe issue I got. It is adding 1 extra blank line at end of file. Is it possible some how we can remove the last blank line? It looks like blank string is present

Abc123
Pqr789
 
Upvote 0
Here is a non-looping method to do what you asked for (and there is no blank line at the end of the file)...
Rich (BB code):
Sub Export()
  Dim LastRow As Long, MyFile As String
  MyFile = Location Of File
  LastRow = Range("A1").End(xlDown).Row
  Open MyFile For Output As #1
  Print #1, Join(Evaluate(Replace("TRANSPOSE(A2:A#&"", ""&E2:E#)", "#", LastRow)), vbNewLine);
  Close #1
End Sub

If you want to stick with your code as posted (but using Print instead of Write, of course), you would change that code line to this...
VBA Code:
Print #1, cellvalue & IIf(i = Count, ";", "")
I am pretty sure that my code will be faster than yours though.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,054
Messages
6,122,893
Members
449,097
Latest member
dbomb1414

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