Excel - Select and copy only cells with values, open an app, paste into

Asked By robzrob on 01-Jul-12 04:45 PM
Hello

Writing a little macro, got partway, now I need this bit:

I have got a worksheet (one of many in the workbook) called Adjust with formu=
lae in A1:J2000 but the formulae are such that only a certain number of row=
s have values in them and that will vary. So, for example, today only A1:J1=
082 have values in them, next week it could be A1:J1200 or A1:J1010 (the ce=
lls with values will always be in one 'block'). I want to copy all the rows=
(from column A to J) with values in them, open Notepad, paste the values i=
nto the Notepad file and Save As C:\...ABC.txt - but C:\...ABC.txt will alr=
eady exist and I will want to discard it and replace it with the new version.

Thanks.


Auric__ replied to robzrob on 02-Jul-12 12:53 AM
No need to involve notepad; VBA can handle text files quite easily. This
stops with the first all-blank line:

Sub writeNonBlanksToText()
Const OUTPUTFILENAME = "C:\ABC.txt"
Dim cell As Range, ro As Range, outP As String
fnum = FreeFile
Open OUTPUTFILENAME For Output As fnum
For Each ro In Range("A1:J" & Cells.End(xlDown).Row).Rows
outP = ""
allBlank = True
For Each cell In ro.Cells
If cell.Column <> 1 Then outP = outP & vbTab
outP = outP & cell.Value
If Len(Trim$(cell.Value)) Then allBlank = False
Next
If allBlank Then Exit For
Print #fnum, outP
Next
Close fnum
End Sub

--
What's a little aggravated assault amongst proper European scientists?
robzrob replied to Auric__ on 02-Jul-12 11:26 AM
Wonderful.  Thanks very much.