| 
    
     |  | はじめまして。VBA初心者で、プログラムが組めず非常に困っています。 
 現在あるマクロに手を加えて繰り返し操作を行いたいです。
 「内容」
 ・CSVデータが50個程度あります。
 ・CSVデータは1〜50まで全てファイル名が振られています。
 ・マクロを使ってExcelシートに1のデータの後ろに2のデータを、
 2のデータの後ろに3のデータを繰り返し貼り付けるプログラム
 があり、現在5個までの貼り付けは連続で行えます。
 ・これに繰り返しのプログラムが組めれば50個全て行える
 のでは?
 
 参考までに、現在あるプログラムでファイル名が0.csvと1.csvのファイルを
 処理した場合を載せてあります。
 
 どなたか、繰り返し処理をご存知でしたらご教授願えませんでしょうか。
 
 
 Sub データ()
 With ActiveSheet.QueryTables.Add(Connection:= _
 "TEXT;C:\0.CSV", Destination:=Range("A1" _
 ))
 .Name = "0"
 .FieldNames = True
 .RowNumbers = False
 .FillAdjacentFormulas = False
 .PreserveFormatting = True
 .RefreshOnFileOpen = False
 .RefreshStyle = xlInsertDeleteCells
 .SavePassword = False
 .SaveData = True
 .AdjustColumnWidth = True
 .RefreshPeriod = 0
 .TextFilePromptOnRefresh = False
 .TextFilePlatform = 932
 .TextFileStartRow = 1
 .TextFileParseType = xlDelimited
 .TextFileTextQualifier = xlTextQualifierDoubleQuote
 .TextFileConsecutiveDelimiter = False
 .TextFileTabDelimiter = False
 .TextFileSemicolonDelimiter = False
 .TextFileCommaDelimiter = True
 .TextFileSpaceDelimiter = False
 .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, _
 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 _
 , 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
 .TextFileTrailingMinusNumbers = True
 .Refresh BackgroundQuery:=False
 End With
 行 = Range("A1").CurrentRegion.Rows.Count + 1
 Range("A" & 行).Select
 With ActiveSheet.QueryTables.Add(Connection:= _
 "TEXT;C:\1.CSV", Destination:=Range( _
 "A" & 行))
 .Name = "1"
 .FieldNames = True
 .RowNumbers = False
 .FillAdjacentFormulas = False
 .PreserveFormatting = True
 .RefreshOnFileOpen = False
 .RefreshStyle = xlInsertDeleteCells
 .SavePassword = False
 .SaveData = True
 .AdjustColumnWidth = True
 .RefreshPeriod = 0
 .TextFilePromptOnRefresh = False
 .TextFilePlatform = 932
 .TextFileStartRow = 2
 .TextFileParseType = xlDelimited
 .TextFileTextQualifier = xlTextQualifierDoubleQuote
 .TextFileConsecutiveDelimiter = False
 .TextFileTabDelimiter = False
 .TextFileSemicolonDelimiter = False
 .TextFileCommaDelimiter = True
 .TextFileSpaceDelimiter = False
 .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, _
 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 _
 , 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
 .TextFileTrailingMinusNumbers = True
 .Refresh BackgroundQuery:=False
 End With
 
 Sheets("Sheet2").Select
 Range("B2").Select
 ActiveCell.FormulaR1C1 = "=AVERAGE(Sheet1!RC:R[10000]C)"
 Range("C2").Select
 ActiveCell.FormulaR1C1 = "=AVERAGE(Sheet1!RC[24]:R[10000]C[24])"
 Range("D2").Select
 ActiveCell.FormulaR1C1 = "=AVERAGE(Sheet1!RC[26]:R[10000]C[26])"
 
 ChDir "C:\"
 ActiveWorkbook.SaveAs Filename:= _
 "C:\データ記録.xls", FileFormat:=xlNormal, _
 Password:="", WriteResPassword:="", ReadOnlyRecommended:=False, _
 CreateBackup:=False
 
 
 End Sub
 
 |  |