2016-03-29 3 views
0

わからない別のファイルへの出力には、これは...エクセル:カラム内の数は、二つの空間を超えると下のセルに二つのスペースを超えると、セルを返す場合は、

を私が持っている場合はそうでないマクロやで可能ですが、場合スプレッドシートの列B(最初のエントリはB7)に数値が存在し、列Dのすべての値を正確に、そしてその下に1つ(この場合はD7とD8)を戻したいとします。

マクロを使用してこのデータをテキストファイルに書き込んで、この形式にする方法はありますか?

D7,D8 
D14,D15 
D21,D22 

答えて

0

これは私の試みです。

Sub getDataforFile() 

Dim objFSO As Object 
Dim objFile As Object 
Dim filePath As String 
Dim myRange As Range 
Dim myCell As Range 

Set objFSO = CreateObject("Scripting.FileSystemObject") 

'this assigns your selection, everything on Colum B to a Range variable 
Set myRange = Selection 

filePath = "C:\Output.txt" 'put your own path 
Set objFile = objFSO.CreateTextFile(filePath) 

For Each myCell In myRange 

'if the value in Column B is a number and not an empty string 
If IsNumeric(myCell.Value) And Not myCell.Value = "" Then  

'write to the text file, first moving two columns over to the right, 
'no rows down, concatenating a comma, and then again moving two columns 
'right, one row down, all from the original range, on Column B 
    objFile.writeline myCell.Offset(0, 2).Value & ", " & myCell.Offset(1, 2).Value 

End If 

'continue checking each cell in the selection 
Next myCell 

objFile.Close 

Set objFile = Nothing 
Set objFSO = Nothing 

End Sub 
+0

うーん:あなたは、持ち込まれた列Bに対応する数値を持ってする必要がある値のように、あなたのポストを形成し、私は仮定の多くの操作B列のすべての値を選択します。このコードは私には大変意味がありますが、何らかの理由で、「実行時エラー '5':無効なプロシージャ呼び出しまたは引数」が表示されます。デバッガは私にこの行を指摘します: 'objFile.writeline myCell.Offset(0、2).Value&"、 "&myCell.Offset(1,2).Value' – Raider61

+0

これは問題ではないと思います。参照のWindowsスクリプトホストオブジェクトモデルを追加するほうが役に立ちます。 –

+0

問題を解明しました。私のテキストはアジア系の文字なので、VBAはそれを拾っていませんでした。あなたのコードは素晴らしいですが、ありがとう! – Raider61

関連する問題