Moin,
< z.B. A B C D E F G H
hm,"z.B." klingt nach "eierlegender Wollmilchsau" :D
< Ergebnis in der Textdatei: jj oo rr ww zz ee hh mm
also, als Stream :o) - oder
ergo vielleicht so (oder so ählich) ;-)
Option Explicit
' z.B. A B C D E F G H
'1 aa bb cc dd ee ff gg hh
'2 ii jj kk ll mm nn oo pp
'3 qq rr ss tt uu vv ww xx
'4 yy zz aa bb cc dd ee ff
'5 gg hh ii jj kk ll mm nn
Sub Füllen()
'Test
Dim mRng As Range, c As Range
Dim x As Integer
'
x = 97
Cells.Clear
Set mRng = Range("A1:H5")
For Each c In mRng
c.Formula = String(2, Chr(x))
x = x + 1
If x > 122 Then x = 97
Next c
End Sub
'
Sub MachWas()
Const Spalten As String = "B, G"
Const AbZeile As Long = 2
Const ZielDatei As String = "C:\Temp\Test.txt"
'
Dim lZeile As Long, lSpalte As Long
Dim colRng As Range, datRng As Range, c As Range
Dim fso As Object
Dim tso As Object
Dim sgf As Object
Set colRng = AuswahlBereich(Spalten)
If colRng Is Nothing Then Exit Sub
Set datRng = Cells(AbZeile, colRng.Columns(1).Column)
lZeile = Cells(Rows.Count, datRng.Column).End(xlUp).Row
If lZeile < AbZeile Then Exit Sub
lSpalte = Cells(lZeile, Columns.Count).End(xlToLeft).Column
Set datRng = Range(datRng, Cells(lZeile, lSpalte))
Set fso = CreateObject("Scripting.FileSystemObject")
fso.CreateTextFile ZielDatei
Set sgf = fso.GetFile(ZielDatei)
Set tso = sgf.OpenAsTextStream(2, -2)
For Each c In datRng
'Test
'If Not Intersect(c, colRng) Is Nothing Then
'tso.write c.Address & Chr(32)
'tso.write c.Value & Chr(32)
'End If
If Not Intersect(c, colRng) Is Nothing Then tso.write c.Value & Chr(32)
Next c
tso.Close '' jj oo rr ww zz ee hh mm
End Sub
Private Function AuswahlBereich(ByVal sSpalten As String) As Range
Dim aSpalten() As String
Dim x As Long
Dim mRng As Range, nRng As Range
sSpalten = Replace(sSpalten, " ", "")
aSpalten = Split(sSpalten, ",")
On Error GoTo errorhandler
Set mRng = Columns(Columns(aSpalten(LBound(aSpalten))).Column)
For x = LBound(aSpalten) + 1 To UBound(aSpalten)
Set nRng = Columns(Columns(aSpalten(x)).Column)
Set mRng = Union(mRng, nRng)
Next x
Set AuswahlBereich = mRng
On Error GoTo 0
Exit Function
errorhandler:
End Function
|