2013-03-17 2 views
0

각 반복마다 파일 사이에 번갈아 쓰기를 원하는 범위가 있습니다. 나는 A1있을 것입니다> B1> A2> B2 등, 예를 들어주는 :100x2 범위를 참조한 다음 파일에 인쇄하십시오.

 A | B 
1 Hello | World 
2 Whats | Up 

이 지금은 같은 텍스트 파일이있을 것입니다 : 나는 다음 코드 한

Hello 
World 
Whats 
Up 

을하는 내 첫 번째 열에서 정보를 잡아하지만 나는 모든 반복에 두 번째 열을 추가로 붙어있다 : 당신은 이미 당신이 "Microsoft 파일 스크립팅을 선택했는지 확인하지 않은 경우,

Sub mac() 
Dim fso As New FileSystemObject 
Dim stream As TextStream 
Set stream = fso.CreateTextFile("F:\Hmmmmm.txt", True) 

    Range("G2").Select 

    Do Until IsEmpty(ActiveCell) 
    stream.WriteLine ActiveCell 
    ' Here I would affectively want stream.WriteLine ActiveCell + 1 Column 
    ActiveCell.Offset(1, 0).Select 
    Loop 

End Sub 

답변

1

우선 "VBA 도구 참조 편집기 (자세한 내용은 How do I use FileSystemObject in VBA?을 참조하십시오).

다음이 작동합니다. 내 의견을 읽으십시오. 코드의 일부를 스프레드 시트/필요에 맞게 변경할 수 있습니다.

Sub mac() 
    Dim ws As Worksheet 
    Dim fso As New FileSystemObject 
    Dim stream As TextStream 
    Dim DataTable As Range 
    Dim StartCell As Range 
    Dim c As Range 

    'Edit this line to save the text file to drive\path\filename of your choice 
    Const TextFileName As String = "F:\Hmmmmm.txt"  

    Set ws = ActiveSheet 
    Set fso = CreateObject("Scripting.FileSystemObject") 
    Set stream = fso.CreateTextFile(FileName:=TextFileName, Overwrite:=True) 

    'Edit this line to adjust to your spreadsheet 
    'I assume your data starts in Cell A1 
    Set StartCell = ws.Range("A1") 

    Set DataTable = StartCell.CurrentRegion 

    For Each c In DataTable.Cells 
      stream.WriteLine c.Value 
    Next c 

    Set c = Nothing 
    Set StartCell = Nothing 
    Set DataTable = Nothing 
    Set ws = Nothing 
    Set stream = Nothing 
    Set fso = Nothing 
End Sub 
관련 문제